From 9d5d804dedda9cd96431f6ecbbe52faaa8032c37 Mon Sep 17 00:00:00 2001
From: Nanocryk <nanocryk@gmail.com>
Date: Mon, 13 Nov 2017 15:09:01 +0100
Subject: [PATCH] tests

---
 .gitignore        |  2 ++
 lib/index.ts      | 10 +++++++---
 native/src/lib.rs | 19 ++++++++++++++-----
 tests/test.js     |  2 +-
 4 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/.gitignore b/.gitignore
index 072503d..6b20e9f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -10,3 +10,5 @@ native/artifacts.json
 *.wot
 
 build/
+
+\.vscode/
diff --git a/lib/index.ts b/lib/index.ts
index ba7465d..a59971e 100644
--- a/lib/index.ts
+++ b/lib/index.ts
@@ -114,7 +114,7 @@ export class MemoryWebOfTrust implements WebOfTrust {
     }
 
     resetWoT(): void {
-
+        this.wot = new NativeWebOfTrust(this.wot.getMaxCert())
     }
 }
 
@@ -123,8 +123,10 @@ export class AtomicFileWebOfTrust implements WebOfTrust {
     filePath: String;
 
     constructor (path: String) {
+        console.log("before")
         this.wot = new NativeWebOfTrust(path);
         this.filePath = path;
+        console.log("after")
     }
 
     toFile(path: String): Boolean {
@@ -217,6 +219,8 @@ export class AtomicFileWebOfTrust implements WebOfTrust {
     }
     
     resetWoT(): void {
-
+        this.wot = new NativeWebOfTrust(this.wot.getMaxCert())
     }
-}
\ No newline at end of file
+}
+
+var test = new AtomicFileWebOfTrust("test.wot");
\ No newline at end of file
diff --git a/native/src/lib.rs b/native/src/lib.rs
index 9403a50..61476e3 100644
--- a/native/src/lib.rs
+++ b/native/src/lib.rs
@@ -4,7 +4,10 @@
 extern crate neon;
 extern crate duniter_rs_wotb;
 
-use neon::js::{Value, JsInteger, JsString, JsBoolean, JsFunction, JsArray, JsNumber, JsObject, Object};
+use std::path::Path;
+
+use neon::js::{Value, JsInteger, JsString, JsBoolean, JsFunction, JsArray, JsNumber, JsObject,
+               Object};
 use neon::js::class::{Class, JsClass};
 use neon::mem::Handle;
 use neon::vm::{Throw, Lock};
@@ -29,10 +32,16 @@ declare_types! {
             } else if let Some(path) = arg0.downcast::<JsString>() {
                 let path = path.value();
 
-                match WebOfTrust::from_file(path.as_str()) {
-                    Some(wot) => Ok(wot),
-                    None => Err(Throw),
-                }
+                if !Path::new(&path).exists() {
+                    let wot = WebOfTrust::new(0);
+                    wot.to_file(&path);
+                    Ok(wot)
+                } else {
+                    match WebOfTrust::from_file(path.as_str()) {
+                        Some(wot) => Ok(wot),
+                        None => Err(Throw),
+                    }
+                }                
             } else {
                 Err(Throw)
             }
diff --git a/tests/test.js b/tests/test.js
index 1e99c73..165c4b7 100644
--- a/tests/test.js
+++ b/tests/test.js
@@ -6,7 +6,7 @@ var assert = require('assert');
 var path = require('path');
 var fs = require('fs');
 
-const FILE = path.join(__dirname, 'wot.bin');
+const FILE = path.join(__dirname, 'wot.wot');
 const X_PERCENT = 1.0;
 const _100_PERCENT = 1.0;
 const MAX_DISTANCE_1 = 1;
-- 
GitLab