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