-
Notifications
You must be signed in to change notification settings - Fork 0
/
brain.test.ts
96 lines (73 loc) · 2.07 KB
/
brain.test.ts
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
import { getAllFiles } from "./files";
import { resolve } from "path";
import { writeBrain, readBrain, resolveBrain } from "./brain";
import { promises } from "fs";
const { rmdir, unlink, writeFile } = promises;
import { getAllMetaData, getMetaData } from "./parse";
import { join } from "path";
const testDir = resolve("./test");
const brainDir = join(testDir, ".brain");
const brainFile = join(testDir, ".brain", "brain");
const infoFile = join(testDir, ".brain", "info");
const testFile = join(testDir, "test_fingerprint1.md");
async function setup(): Promise<void> {
try {
await unlink(brainFile);
} catch (e) {}
try {
await unlink(infoFile);
} catch (e) {}
try {
await unlink(testFile);
} catch (e) {}
try {
await rmdir(brainDir);
} catch (e) {}
return;
}
async function createTestFile(): Promise<void> {
const fm = [
"---",
"title: Happy Number",
"number: 202",
"difficulty: easy",
"links:",
"- https://leetcode.com/problems/happy-number/",
"---",
].join("\n");
try {
await writeFile(testFile, fm, "utf-8");
} catch (e) {
console.error(e, "Write error front-matter");
}
return;
}
async function deleteTestFile(): Promise<void> {
try {
await unlink(testFile);
} catch (e) {}
return;
}
beforeEach(setup);
afterEach(deleteTestFile);
test("we should be able to write and read a brain", async () => {
expect.assertions(1);
const files = await getAllFiles(testDir, [], true);
const meta = await getAllMetaData(files);
await writeBrain(meta);
const readMeta = await readBrain();
expect(readMeta).toEqual(meta);
});
test("we should be able to detect new files and update brain", async () => {
expect.assertions(2);
await resolveBrain(testDir);
let brain = await readBrain();
const files = await getAllFiles(testDir, [], true);
const meta = await getAllMetaData(files);
expect(brain).toEqual(meta);
await createTestFile();
const newBrain = await resolveBrain(testDir);
const newMeta = await getMetaData(testFile);
brain[testFile] = newMeta;
expect(brain).toEqual(newBrain);
});