Repository navigation
Expand file tree
/
Copy pathfile-store.test.js
More file actions
91 lines (91 loc) · 3.07 KB
/
Copy pathfile-store.test.js
File metadata and controls
91 lines (91 loc) · 3.07 KB
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
const { test } = require("node:test");
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const os = require("node:os");
const store = require("../lib/file-store");
function fixture(t) {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "todotree-test-"));
t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
return {
dir,
filePath: path.join(dir, "tasks.md"),
backup: path.join(dir, "backups"),
};
}
test("loading an absent file never creates it; first save creates exact content", (t) => {
const { filePath, backup } = fixture(t),
loaded = store.read(filePath, "seed");
assert.equal(loaded.revision, null);
assert.equal(fs.existsSync(filePath), false);
const result = store.save(filePath, { ...loaded, content: "new" }, backup);
assert.equal(result.success, true);
assert.equal(fs.readFileSync(filePath, "utf8"), "new");
});
test("external edit or deletion prevents overwriting and leaves no temp files", (t) => {
const { filePath, dir, backup } = fixture(t);
fs.writeFileSync(filePath, "original");
const before = store.read(filePath);
fs.writeFileSync(filePath, "external");
assert.equal(
store.save(filePath, { ...before, content: "mine" }, backup).conflict,
true,
);
assert.equal(fs.readFileSync(filePath, "utf8"), "external");
fs.unlinkSync(filePath);
assert.equal(
store.save(filePath, { ...before, content: "mine" }, backup).conflict,
true,
);
assert.deepEqual(fs.readdirSync(dir), []);
});
test("content revision catches edits even if mtime is unchanged", (t) => {
const { filePath, backup } = fixture(t);
fs.writeFileSync(filePath, "first");
const stat = fs.statSync(filePath),
before = store.read(filePath);
fs.writeFileSync(filePath, "other");
fs.utimesSync(filePath, stat.atime, stat.mtime);
assert.equal(
store.save(filePath, { ...before, content: "mine" }, backup).conflict,
true,
);
});
test("backup keeps previous bytes and only the most recent twenty versions", (t) => {
const { filePath, backup } = fixture(t);
fs.writeFileSync(filePath, "0");
for (let n = 1; n <= 25; n++)
assert.equal(
store.save(
filePath,
{ ...store.read(filePath), content: String(n) },
backup,
).success,
true,
);
const dir = path.join(backup, fs.readdirSync(backup)[0]);
assert.equal(fs.readdirSync(dir).length, 20);
assert.equal(fs.readFileSync(filePath, "utf8"), "25");
});
test("failed write or switched file returns a visible error without changing original", (t) => {
const { filePath, backup } = fixture(t);
fs.writeFileSync(filePath, "safe");
assert.equal(
store.save(
filePath,
{ ...store.read(filePath), filePath: "other.md", content: "oops" },
backup,
).success,
false,
);
assert.equal(fs.readFileSync(filePath, "utf8"), "safe");
const absent = path.join(path.dirname(filePath), "missing", "tasks.md");
assert.equal(
store.save(
absent,
{ filePath: absent, revision: null, content: "oops" },
backup,
).success,
false,
);
});