Repository navigation
Expand file tree
/
Copy pathwrite-counts.test.ts
More file actions
77 lines (73 loc) · 2.71 KB
/
Copy pathwrite-counts.test.ts
File metadata and controls
77 lines (73 loc) · 2.71 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
import { describe, expect, it } from "vitest";
import { installWriteRecorder } from "./write-counts.mjs";
const isolatedRecorder: typeof installWriteRecorder = new Function(
`return (${installWriteRecorder.toString()})`,
)();
describe("data.json write recorder", () => {
it("counts exact-path UTF-8 writes, preserves arguments and this, and restores the descriptor", async () => {
const calls: unknown[][] = [];
const adapter = {
async write(path: string, contents: string, ...rest: unknown[]) {
expect(this).toBe(adapter);
calls.push([path, contents, ...rest]);
return "written";
},
};
const original = Object.getOwnPropertyDescriptor(adapter, "write");
const recorder = isolatedRecorder(adapter, "plugin/data.json");
expect(await adapter.write("other/data.json", "ignored")).toBe("written");
expect(await adapter.write("plugin/data.json", "雪", { mtime: 123 })).toBe("written");
expect(calls).toEqual([
["other/data.json", "ignored"],
["plugin/data.json", "雪", { mtime: 123 }],
]);
expect(recorder.stop()).toMatchObject({
attempted: 1,
completed: 1,
failed: 0,
inflight: 0,
completedBytes: 3,
complete: true,
});
expect(Object.getOwnPropertyDescriptor(adapter, "write")).toEqual(original);
});
it("records rejected and synchronous adapter failures instead of claiming success", async () => {
const failure = new Error("disk full");
const adapter = { write: (_path: string, _contents: string) => Promise.reject(failure) };
const recorder = installWriteRecorder(adapter, "data.json");
await expect(adapter.write("data.json", "{}")).rejects.toBe(failure);
expect(recorder.stop()).toMatchObject({
attempted: 1,
completed: 0,
failed: 1,
inflight: 0,
});
const synchronous = {
write: (_path: string, _contents: string): Promise<void> => {
throw failure;
},
};
const second = installWriteRecorder(synchronous, "data.json");
expect(() => synchronous.write("data.json", "{}")).toThrow(failure);
expect(second.stop()).toMatchObject({ failed: 1, inflight: 0 });
});
it("refuses unfinished counts or replacing a different recorder's writer", async () => {
let finish!: () => void;
const adapter = {
write: (_path: string, _contents: string) =>
new Promise<void>((resolve) => {
finish = resolve;
}),
};
const recorder = installWriteRecorder(adapter, "data.json");
const pending = adapter.write("data.json", "{}");
expect(recorder.report().complete).toBe(false);
expect(() => recorder.stop()).toThrow("pending writes");
finish();
await pending;
const replacement = async () => undefined;
adapter.write = replacement;
expect(() => recorder.stop()).toThrow("writer changed");
expect(adapter.write).toBe(replacement);
});
});