-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
195 lines (147 loc) · 4.65 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const { USHINBase } = require("./");
const test = require("tape");
// Pre-initialize the plugin and avoid persistence
USHINBase.init({ persist: false });
async function getNew(url = "hyper://example") {
const db = new USHINBase({ url });
await db.init();
return db;
}
const EXAMPLE_POINT_ID = "Example-Point";
const EXAMPLE_POINT = {
_id: EXAMPLE_POINT_ID,
content: "Cats bring me joy",
};
const EXAMPLE_MESSAGE = {
main: EXAMPLE_POINT_ID,
shapes: {
feelings: [EXAMPLE_POINT_ID],
},
};
const EXAMPLE_POINT_STORE = {
[EXAMPLE_POINT_ID]: EXAMPLE_POINT,
};
test.onFinish(() => {
USHINBase.close();
});
test("Able to initialize and set author metadata", async (t) => {
t.plan(3);
try {
var db = await getNew("hyper://t1");
t.pass("Able to create the DB");
await db.setAuthorInfo({ name: "Example" });
t.pass("Able to set author info");
const { name } = await db.getAuthorInfo();
t.equal(name, "Example", "name got set and can be retrieved");
} catch (e) {
t.error(e);
} finally {
if (db) await db.close();
}
});
test("Able to add and get messages", async (t) => {
t.plan(8);
try {
var db = await getNew("hyper://t2");
const id = await db.addMessage(EXAMPLE_MESSAGE, EXAMPLE_POINT_STORE);
t.pass("Able to add message");
const message = await db.getMessage(id);
const { author, shapes, createdAt, main } = message;
const { feelings } = shapes;
const [pointId] = feelings;
t.equal(pointId, EXAMPLE_POINT_ID, "Got saved point");
const pointStore = await db.getPointsForMessage(message);
const point = pointStore[pointId];
t.equal(author, db.authorURL, "Author got set");
t.equal(feelings.length, 1, "Feelings got set");
t.ok(
createdAt instanceof Date,
"Timestamp got auto-generated and is a Date"
);
t.equal(main, EXAMPLE_MESSAGE.main, "main id got set");
t.ok(point, "Got point from store");
t.equal(point.content, "Cats bring me joy", "Point content got set");
} catch (e) {
t.error(e);
} finally {
if (db) db.close();
}
});
test.skip("Able to search for messages in a time range", async (t) => {
t.plan(6);
try {
var db = await getNew("hyper://t3");
await db.addPoint(EXAMPLE_POINT);
const point = await db.getPoint(EXAMPLE_POINT_ID);
const pointStore = { [EXAMPLE_POINT_ID]: point };
await db.addMessage(
{ createdAt: new Date(10), ...EXAMPLE_MESSAGE },
pointStore
);
await db.addMessage(
{ createdAt: new Date(2000), ...EXAMPLE_MESSAGE },
pointStore
);
await db.addMessage(
{ createdAt: new Date(3000), ...EXAMPLE_MESSAGE },
pointStore
);
t.pass("Able to add several messages");
const results = await db.searchMessages({ createdAt: { $gt: 100 } });
t.equal(results.length, 2, "Got expected number of results");
const [message] = results;
const { author, shapes, createdAt } = message;
const { feelings } = shapes;
const [pointId] = feelings;
t.equal(pointId, EXAMPLE_POINT_ID, "Got point ID");
t.equal(author, "test", "Author got set");
t.equal(feelings.length, 1, "Feelings got set");
t.ok(
createdAt instanceof Date,
"Timestamp got auto-generated and is a Date"
);
} catch (e) {
t.error(e);
} finally {
if (db) await db.close();
}
});
test("Able to search for messages that contain a point ID", async (t) => {
t.plan(1);
try {
var db = await getNew("hyper://t4");
await db.addMessage(
{ ...EXAMPLE_MESSAGE, focus: EXAMPLE_POINT_ID },
EXAMPLE_POINT_STORE
);
const results = await db.searchMessagesForPoints([EXAMPLE_POINT]);
t.equal(results.length, 1, "Found message in search");
} catch (e) {
t.error(e);
} finally {
if (db) await db.close();
}
});
test("Able to search for points by their text contents", async (t) => {
t.plan(2);
try {
var db = await getNew("hyper://t5");
await db.addPoint({ content: "Hello world", _id: "one" });
await db.addPoint({ content: "Goodbye world", _id: "two" });
const results1 = await db.searchPointsByContent("world");
const results1Ids = results1.map(({ _id }) => _id);
// Note that the sort order has newer points first
t.deepEqual(results1Ids, ["two", "one"], "Got expected point IDs");
const results2 = await db.searchPointsByContent("hello");
const results2Ids = results2.map(({ _id }) => _id);
t.deepEqual(results2Ids, ["one"], "Got just the matching points");
} catch (e) {
t.error(e);
} finally {
if (db) await db.close();
}
});
function makePoint(point = {}) {
const _id = Date.now() + "";
return { _id, ...EXAMPLE_POINT, ...point };
}