Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

logging.test.ts

175 lines5.9 KB

1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeFS from "node:fs";
3import * as NodeOS from "node:os";
4import * as NodePath from "node:path";
5import { afterEach, describe, expect, it } from "vite-plus/test";
6import { HostProcessPlatform } from "./hostProcess.ts";
7​
8import {
9 RotatingFileSink,
10 RotatingFileSinkConfigurationError,
11 RotatingFileSinkError,
12} from "./logging.ts";
13​
14const windowsHost = HostProcessPlatform.defaultValue() === "win32";
15const tempDirectories: string[] = [];
16​
17const makeTempDirectory = (): string => {
18 const directory = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3code-logging-"));
19 tempDirectories.push(directory);
20 return directory;
21};
22​
23const captureError = (run: () => unknown): unknown => {
24 try {
25 run();
26 } catch (cause) {
27 return cause;
28 }
29 throw new Error("Expected operation to throw");
30};
31​
32afterEach(() => {
33 for (const directory of tempDirectories.splice(0)) {
34 NodeFS.rmSync(directory, { recursive: true, force: true });
35 }
36});
37​
38describe("RotatingFileSink", () => {
39 it.each([
40 { option: "maxBytes" as const, maxBytes: 0, maxFiles: 1 },
41 { option: "maxFiles" as const, maxBytes: 1, maxFiles: 0 },
42 ])("reports invalid $option configuration structurally", (input) => {
43 const thrown = captureError(
44 () =>
45 new RotatingFileSink({
46 filePath: "/unused/log.ndjson",
47 maxBytes: input.maxBytes,
48 maxFiles: input.maxFiles,
49 }),
50 );
51​
52 expect(thrown).toBeInstanceOf(RotatingFileSinkConfigurationError);
53 expect(thrown).toMatchObject({
54 option: input.option,
55 received: 0,
56 minimum: 1,
57 });
58 expect((thrown as Error).message).toBe(`${input.option} must be >= 1 (received 0)`);
59 });
60​
61 it("preserves directory initialization failures", () => {
62 const directory = makeTempDirectory();
63 const parentFile = NodePath.join(directory, "not-a-directory");
64 const filePath = NodePath.join(parentFile, "log.ndjson");
65 NodeFS.writeFileSync(parentFile, "occupied");
66​
67 const thrown = captureError(() => new RotatingFileSink({ filePath, maxBytes: 1, maxFiles: 1 }));
68​
69 expect(thrown).toBeInstanceOf(RotatingFileSinkError);
70 expect(thrown).toMatchObject({ operation: "initialize", filePath });
71 expect((thrown as RotatingFileSinkError).cause).toBeInstanceOf(Error);
72 });
73​
74 // An over-long name is the one stat failure that is neither ENOENT nor a
75 // permission problem on posix. Windows reports it as ENOENT, so the sink
76 // correctly treats it as an absent file and there is nothing to assert.
77 it.skipIf(windowsHost)("only treats a missing log file as an empty current size", () => {
78 const directory = makeTempDirectory();
79 const filePath = NodePath.join(directory, "a".repeat(300));
80​
81 const thrown = captureError(() => new RotatingFileSink({ filePath, maxBytes: 1, maxFiles: 1 }));
82​
83 expect(thrown).toBeInstanceOf(RotatingFileSinkError);
84 expect(thrown).toMatchObject({ operation: "read", filePath });
85 expect((thrown as RotatingFileSinkError).cause).toMatchObject({ code: "ENAMETOOLONG" });
86 });
87​
88 it("starts an absent log file at zero bytes", () => {
89 const directory = makeTempDirectory();
90 const filePath = NodePath.join(directory, "log.ndjson");
91 const sink = new RotatingFileSink({ filePath, maxBytes: 100, maxFiles: 1 });
92​
93 sink.write("entry");
94​
95 expect(NodeFS.readFileSync(filePath, "utf8")).toBe("entry");
96 });
97​
98 it("preserves write failures", () => {
99 const directory = makeTempDirectory();
100 const filePath = NodePath.join(directory, "log.ndjson");
101 NodeFS.mkdirSync(filePath);
102 const sink = new RotatingFileSink({
103 filePath,
104 maxBytes: Number.MAX_SAFE_INTEGER,
105 maxFiles: 1,
106 throwOnError: true,
107 });
108​
109 const thrown = captureError(() => sink.write("entry"));
110​
111 expect(thrown).toBeInstanceOf(RotatingFileSinkError);
112 expect(thrown).toMatchObject({ operation: "write", filePath });
113 expect((thrown as RotatingFileSinkError).cause).toMatchObject({ code: "EISDIR" });
114 });
115​
116 it("preserves rotation failures without an artificial write wrapper", () => {
117 const directory = makeTempDirectory();
118 const filePath = NodePath.join(directory, "log.ndjson");
119 NodeFS.writeFileSync(filePath, "a");
120 NodeFS.mkdirSync(`${filePath}.1`);
121 const sink = new RotatingFileSink({
122 filePath,
123 maxBytes: 1,
124 maxFiles: 1,
125 throwOnError: true,
126 });
127​
128 const thrown = captureError(() => sink.write("b"));
129​
130 expect(thrown).toBeInstanceOf(RotatingFileSinkError);
131 expect(thrown).toMatchObject({ operation: "rotate", filePath });
132 expect((thrown as RotatingFileSinkError).cause).toBeInstanceOf(Error);
133 });
134​
135 it("never reports a rotation failure after successfully appending a chunk", () => {
136 const directory = makeTempDirectory();
137 const filePath = NodePath.join(directory, "log.ndjson");
138 NodeFS.mkdirSync(`${filePath}.1`);
139 const sink = new RotatingFileSink({
140 filePath,
141 maxBytes: 1,
142 maxFiles: 1,
143 throwOnError: true,
144 });
145​
146 sink.write("oversized");
147​
148 expect(NodeFS.readFileSync(filePath, "utf8")).toBe("oversized");
149 const thrown = captureError(() => sink.write("next"));
150 expect(thrown).toMatchObject({ operation: "rotate", filePath });
151 expect(NodeFS.readFileSync(filePath, "utf8")).toBe("oversized");
152 });
153​
154 it("preserves backup pruning failures", () => {
155 const directory = makeTempDirectory();
156 const filePath = NodePath.join(directory, "log.ndjson");
157 const overflowBackup = `${filePath}.2`;
158 NodeFS.mkdirSync(overflowBackup);
159 NodeFS.writeFileSync(NodePath.join(overflowBackup, "entry"), "occupied");
160​
161 const thrown = captureError(
162 () =>
163 new RotatingFileSink({
164 filePath,
165 maxBytes: 1,
166 maxFiles: 1,
167 throwOnError: true,
168 }),
169 );
170​
171 expect(thrown).toBeInstanceOf(RotatingFileSinkError);
172 expect(thrown).toMatchObject({ operation: "prune", filePath });
173 expect((thrown as RotatingFileSinkError).cause).toBeInstanceOf(Error);
174 });
175});