@herdctl/core 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (275) hide show
  1. package/dist/config/__tests__/agent.test.js +31 -13
  2. package/dist/config/__tests__/agent.test.js.map +1 -1
  3. package/dist/config/__tests__/merge.test.js +9 -2
  4. package/dist/config/__tests__/merge.test.js.map +1 -1
  5. package/dist/config/__tests__/schema.test.js +350 -1
  6. package/dist/config/__tests__/schema.test.js.map +1 -1
  7. package/dist/config/index.d.ts +1 -1
  8. package/dist/config/index.d.ts.map +1 -1
  9. package/dist/config/index.js +3 -1
  10. package/dist/config/index.js.map +1 -1
  11. package/dist/config/schema.d.ts +828 -24
  12. package/dist/config/schema.d.ts.map +1 -1
  13. package/dist/config/schema.js +118 -6
  14. package/dist/config/schema.js.map +1 -1
  15. package/dist/fleet-manager/__tests__/coverage.test.js +11 -332
  16. package/dist/fleet-manager/__tests__/coverage.test.js.map +1 -1
  17. package/dist/fleet-manager/__tests__/errors.test.js +1 -49
  18. package/dist/fleet-manager/__tests__/errors.test.js.map +1 -1
  19. package/dist/fleet-manager/__tests__/integration.test.js +109 -0
  20. package/dist/fleet-manager/__tests__/integration.test.js.map +1 -1
  21. package/dist/fleet-manager/__tests__/reload.test.js +1 -1
  22. package/dist/fleet-manager/__tests__/reload.test.js.map +1 -1
  23. package/dist/fleet-manager/config-reload.d.ts +164 -0
  24. package/dist/fleet-manager/config-reload.d.ts.map +1 -0
  25. package/dist/fleet-manager/config-reload.js +445 -0
  26. package/dist/fleet-manager/config-reload.js.map +1 -0
  27. package/dist/fleet-manager/context.d.ts +76 -0
  28. package/dist/fleet-manager/context.d.ts.map +1 -0
  29. package/dist/fleet-manager/context.js +11 -0
  30. package/dist/fleet-manager/context.js.map +1 -0
  31. package/dist/fleet-manager/errors.d.ts +0 -25
  32. package/dist/fleet-manager/errors.d.ts.map +1 -1
  33. package/dist/fleet-manager/errors.js +0 -38
  34. package/dist/fleet-manager/errors.js.map +1 -1
  35. package/dist/fleet-manager/event-emitters.d.ts +123 -0
  36. package/dist/fleet-manager/event-emitters.d.ts.map +1 -0
  37. package/dist/fleet-manager/event-emitters.js +136 -0
  38. package/dist/fleet-manager/event-emitters.js.map +1 -0
  39. package/dist/fleet-manager/event-types.d.ts +0 -15
  40. package/dist/fleet-manager/event-types.d.ts.map +1 -1
  41. package/dist/fleet-manager/fleet-manager.d.ts +40 -653
  42. package/dist/fleet-manager/fleet-manager.d.ts.map +1 -1
  43. package/dist/fleet-manager/fleet-manager.js +95 -1720
  44. package/dist/fleet-manager/fleet-manager.js.map +1 -1
  45. package/dist/fleet-manager/index.d.ts +13 -2
  46. package/dist/fleet-manager/index.d.ts.map +1 -1
  47. package/dist/fleet-manager/index.js +19 -6
  48. package/dist/fleet-manager/index.js.map +1 -1
  49. package/dist/fleet-manager/job-control.d.ts +64 -0
  50. package/dist/fleet-manager/job-control.d.ts.map +1 -0
  51. package/dist/fleet-manager/job-control.js +296 -0
  52. package/dist/fleet-manager/job-control.js.map +1 -0
  53. package/dist/fleet-manager/log-streaming.d.ts +171 -0
  54. package/dist/fleet-manager/log-streaming.d.ts.map +1 -0
  55. package/dist/fleet-manager/log-streaming.js +503 -0
  56. package/dist/fleet-manager/log-streaming.js.map +1 -0
  57. package/dist/fleet-manager/schedule-executor.d.ts +63 -0
  58. package/dist/fleet-manager/schedule-executor.d.ts.map +1 -0
  59. package/dist/fleet-manager/schedule-executor.js +209 -0
  60. package/dist/fleet-manager/schedule-executor.js.map +1 -0
  61. package/dist/fleet-manager/schedule-management.d.ts +71 -0
  62. package/dist/fleet-manager/schedule-management.d.ts.map +1 -0
  63. package/dist/fleet-manager/schedule-management.js +171 -0
  64. package/dist/fleet-manager/schedule-management.js.map +1 -0
  65. package/dist/fleet-manager/status-queries.d.ts +105 -0
  66. package/dist/fleet-manager/status-queries.d.ts.map +1 -0
  67. package/dist/fleet-manager/status-queries.js +247 -0
  68. package/dist/fleet-manager/status-queries.js.map +1 -0
  69. package/dist/fleet-manager/types.d.ts +0 -39
  70. package/dist/fleet-manager/types.d.ts.map +1 -1
  71. package/dist/runner/__tests__/job-executor.test.js +206 -1
  72. package/dist/runner/__tests__/job-executor.test.js.map +1 -1
  73. package/dist/runner/job-executor.d.ts +9 -0
  74. package/dist/runner/job-executor.d.ts.map +1 -1
  75. package/dist/runner/job-executor.js +78 -4
  76. package/dist/runner/job-executor.js.map +1 -1
  77. package/dist/runner/types.d.ts +2 -0
  78. package/dist/runner/types.d.ts.map +1 -1
  79. package/dist/scheduler/__tests__/cron.test.d.ts +2 -0
  80. package/dist/scheduler/__tests__/cron.test.d.ts.map +1 -0
  81. package/dist/scheduler/__tests__/cron.test.js +867 -0
  82. package/dist/scheduler/__tests__/cron.test.js.map +1 -0
  83. package/dist/scheduler/__tests__/scheduler.test.js +164 -5
  84. package/dist/scheduler/__tests__/scheduler.test.js.map +1 -1
  85. package/dist/scheduler/cron.d.ts +126 -0
  86. package/dist/scheduler/cron.d.ts.map +1 -0
  87. package/dist/scheduler/cron.js +390 -0
  88. package/dist/scheduler/cron.js.map +1 -0
  89. package/dist/scheduler/errors.d.ts +81 -1
  90. package/dist/scheduler/errors.d.ts.map +1 -1
  91. package/dist/scheduler/errors.js +81 -6
  92. package/dist/scheduler/errors.js.map +1 -1
  93. package/dist/scheduler/index.d.ts +1 -0
  94. package/dist/scheduler/index.d.ts.map +1 -1
  95. package/dist/scheduler/index.js +2 -0
  96. package/dist/scheduler/index.js.map +1 -1
  97. package/dist/scheduler/schedule-runner.d.ts +2 -2
  98. package/dist/scheduler/schedule-runner.d.ts.map +1 -1
  99. package/dist/scheduler/schedule-runner.js +20 -8
  100. package/dist/scheduler/schedule-runner.js.map +1 -1
  101. package/dist/scheduler/scheduler.d.ts +4 -4
  102. package/dist/scheduler/scheduler.d.ts.map +1 -1
  103. package/dist/scheduler/scheduler.js +86 -20
  104. package/dist/scheduler/scheduler.js.map +1 -1
  105. package/dist/scheduler/types.d.ts +1 -1
  106. package/dist/scheduler/types.d.ts.map +1 -1
  107. package/dist/state/schemas/job-metadata.d.ts +2 -2
  108. package/package.json +33 -8
  109. package/.turbo/turbo-build.log +0 -4
  110. package/.turbo/turbo-test.log +0 -219
  111. package/.turbo/turbo-typecheck.log +0 -4
  112. package/coverage/base.css +0 -224
  113. package/coverage/block-navigation.js +0 -87
  114. package/coverage/coverage-final.json +0 -51
  115. package/coverage/favicon.png +0 -0
  116. package/coverage/index.html +0 -251
  117. package/coverage/prettify.css +0 -1
  118. package/coverage/prettify.js +0 -2
  119. package/coverage/sort-arrow-sprite.png +0 -0
  120. package/coverage/sorter.js +0 -210
  121. package/coverage/src/config/index.html +0 -191
  122. package/coverage/src/config/index.ts.html +0 -442
  123. package/coverage/src/config/interpolate.ts.html +0 -652
  124. package/coverage/src/config/loader.ts.html +0 -1501
  125. package/coverage/src/config/merge.ts.html +0 -823
  126. package/coverage/src/config/parser.ts.html +0 -1213
  127. package/coverage/src/config/schema.ts.html +0 -1123
  128. package/coverage/src/fleet-manager/errors.ts.html +0 -2326
  129. package/coverage/src/fleet-manager/event-types.ts.html +0 -1219
  130. package/coverage/src/fleet-manager/fleet-manager.ts.html +0 -7030
  131. package/coverage/src/fleet-manager/index.html +0 -206
  132. package/coverage/src/fleet-manager/index.ts.html +0 -469
  133. package/coverage/src/fleet-manager/job-manager.ts.html +0 -2074
  134. package/coverage/src/fleet-manager/job-queue.ts.html +0 -2479
  135. package/coverage/src/fleet-manager/types.ts.html +0 -2602
  136. package/coverage/src/index.html +0 -116
  137. package/coverage/src/index.ts.html +0 -181
  138. package/coverage/src/runner/errors.ts.html +0 -1006
  139. package/coverage/src/runner/index.html +0 -191
  140. package/coverage/src/runner/index.ts.html +0 -256
  141. package/coverage/src/runner/job-executor.ts.html +0 -1429
  142. package/coverage/src/runner/message-processor.ts.html +0 -1150
  143. package/coverage/src/runner/sdk-adapter.ts.html +0 -658
  144. package/coverage/src/runner/types.ts.html +0 -559
  145. package/coverage/src/scheduler/errors.ts.html +0 -388
  146. package/coverage/src/scheduler/index.html +0 -206
  147. package/coverage/src/scheduler/index.ts.html +0 -244
  148. package/coverage/src/scheduler/interval.ts.html +0 -652
  149. package/coverage/src/scheduler/schedule-runner.ts.html +0 -1411
  150. package/coverage/src/scheduler/schedule-state.ts.html +0 -718
  151. package/coverage/src/scheduler/scheduler.ts.html +0 -1795
  152. package/coverage/src/scheduler/types.ts.html +0 -733
  153. package/coverage/src/state/directory.ts.html +0 -736
  154. package/coverage/src/state/errors.ts.html +0 -376
  155. package/coverage/src/state/fleet-state.ts.html +0 -937
  156. package/coverage/src/state/index.html +0 -221
  157. package/coverage/src/state/index.ts.html +0 -322
  158. package/coverage/src/state/job-metadata.ts.html +0 -1420
  159. package/coverage/src/state/job-output.ts.html +0 -1033
  160. package/coverage/src/state/schemas/fleet-state.ts.html +0 -445
  161. package/coverage/src/state/schemas/index.html +0 -176
  162. package/coverage/src/state/schemas/index.ts.html +0 -286
  163. package/coverage/src/state/schemas/job-metadata.ts.html +0 -628
  164. package/coverage/src/state/schemas/job-output.ts.html +0 -616
  165. package/coverage/src/state/schemas/session-info.ts.html +0 -361
  166. package/coverage/src/state/session.ts.html +0 -844
  167. package/coverage/src/state/types.ts.html +0 -262
  168. package/coverage/src/state/utils/atomic.ts.html +0 -748
  169. package/coverage/src/state/utils/index.html +0 -146
  170. package/coverage/src/state/utils/index.ts.html +0 -103
  171. package/coverage/src/state/utils/reads.ts.html +0 -1621
  172. package/coverage/src/work-sources/adapters/github.ts.html +0 -3583
  173. package/coverage/src/work-sources/adapters/index.html +0 -131
  174. package/coverage/src/work-sources/adapters/index.ts.html +0 -277
  175. package/coverage/src/work-sources/errors.ts.html +0 -298
  176. package/coverage/src/work-sources/index.html +0 -176
  177. package/coverage/src/work-sources/index.ts.html +0 -529
  178. package/coverage/src/work-sources/manager.ts.html +0 -1324
  179. package/coverage/src/work-sources/registry.ts.html +0 -619
  180. package/coverage/src/work-sources/types.ts.html +0 -568
  181. package/dist/fleet-manager/__tests__/event-helpers.test.d.ts +0 -7
  182. package/dist/fleet-manager/__tests__/event-helpers.test.d.ts.map +0 -1
  183. package/dist/fleet-manager/__tests__/event-helpers.test.js +0 -368
  184. package/dist/fleet-manager/__tests__/event-helpers.test.js.map +0 -1
  185. package/src/config/__tests__/agent.test.ts +0 -864
  186. package/src/config/__tests__/interpolate.test.ts +0 -644
  187. package/src/config/__tests__/loader.test.ts +0 -784
  188. package/src/config/__tests__/merge.test.ts +0 -751
  189. package/src/config/__tests__/parser.test.ts +0 -533
  190. package/src/config/__tests__/schema.test.ts +0 -873
  191. package/src/config/index.ts +0 -119
  192. package/src/config/interpolate.ts +0 -189
  193. package/src/config/loader.ts +0 -472
  194. package/src/config/merge.ts +0 -246
  195. package/src/config/parser.ts +0 -376
  196. package/src/config/schema.ts +0 -346
  197. package/src/fleet-manager/__tests__/coverage.test.ts +0 -2869
  198. package/src/fleet-manager/__tests__/errors.test.ts +0 -660
  199. package/src/fleet-manager/__tests__/event-helpers.test.ts +0 -448
  200. package/src/fleet-manager/__tests__/integration.test.ts +0 -1209
  201. package/src/fleet-manager/__tests__/job-control.test.ts +0 -283
  202. package/src/fleet-manager/__tests__/job-manager.test.ts +0 -869
  203. package/src/fleet-manager/__tests__/job-queue.test.ts +0 -401
  204. package/src/fleet-manager/__tests__/reload.test.ts +0 -751
  205. package/src/fleet-manager/__tests__/status-queries.test.ts +0 -595
  206. package/src/fleet-manager/__tests__/trigger.test.ts +0 -601
  207. package/src/fleet-manager/errors.ts +0 -747
  208. package/src/fleet-manager/event-types.ts +0 -378
  209. package/src/fleet-manager/fleet-manager.ts +0 -2315
  210. package/src/fleet-manager/index.ts +0 -128
  211. package/src/fleet-manager/job-manager.ts +0 -663
  212. package/src/fleet-manager/job-queue.ts +0 -798
  213. package/src/fleet-manager/types.ts +0 -839
  214. package/src/index.ts +0 -32
  215. package/src/runner/__tests__/errors.test.ts +0 -382
  216. package/src/runner/__tests__/job-executor.test.ts +0 -1708
  217. package/src/runner/__tests__/message-processor.test.ts +0 -960
  218. package/src/runner/__tests__/sdk-adapter.test.ts +0 -626
  219. package/src/runner/errors.ts +0 -307
  220. package/src/runner/index.ts +0 -57
  221. package/src/runner/job-executor.ts +0 -448
  222. package/src/runner/message-processor.ts +0 -355
  223. package/src/runner/sdk-adapter.ts +0 -191
  224. package/src/runner/types.ts +0 -158
  225. package/src/scheduler/__tests__/errors.test.ts +0 -159
  226. package/src/scheduler/__tests__/interval.test.ts +0 -515
  227. package/src/scheduler/__tests__/schedule-runner.test.ts +0 -798
  228. package/src/scheduler/__tests__/schedule-state.test.ts +0 -671
  229. package/src/scheduler/__tests__/scheduler.test.ts +0 -1280
  230. package/src/scheduler/errors.ts +0 -101
  231. package/src/scheduler/index.ts +0 -53
  232. package/src/scheduler/interval.ts +0 -189
  233. package/src/scheduler/schedule-runner.ts +0 -442
  234. package/src/scheduler/schedule-state.ts +0 -211
  235. package/src/scheduler/scheduler.ts +0 -570
  236. package/src/scheduler/types.ts +0 -216
  237. package/src/state/__tests__/directory.test.ts +0 -595
  238. package/src/state/__tests__/fleet-state.test.ts +0 -868
  239. package/src/state/__tests__/job-metadata-schema.test.ts +0 -414
  240. package/src/state/__tests__/job-metadata.test.ts +0 -831
  241. package/src/state/__tests__/job-output.test.ts +0 -856
  242. package/src/state/__tests__/session-schema.test.ts +0 -378
  243. package/src/state/__tests__/session.test.ts +0 -604
  244. package/src/state/directory.ts +0 -217
  245. package/src/state/errors.ts +0 -97
  246. package/src/state/fleet-state.ts +0 -284
  247. package/src/state/index.ts +0 -79
  248. package/src/state/job-metadata.ts +0 -445
  249. package/src/state/job-output.ts +0 -316
  250. package/src/state/schemas/__tests__/job-output.test.ts +0 -338
  251. package/src/state/schemas/fleet-state.ts +0 -120
  252. package/src/state/schemas/index.ts +0 -67
  253. package/src/state/schemas/job-metadata.ts +0 -181
  254. package/src/state/schemas/job-output.ts +0 -177
  255. package/src/state/schemas/session-info.ts +0 -92
  256. package/src/state/session.ts +0 -253
  257. package/src/state/types.ts +0 -59
  258. package/src/state/utils/__tests__/atomic.test.ts +0 -723
  259. package/src/state/utils/__tests__/reads.test.ts +0 -1071
  260. package/src/state/utils/atomic.ts +0 -221
  261. package/src/state/utils/index.ts +0 -6
  262. package/src/state/utils/reads.ts +0 -512
  263. package/src/work-sources/__tests__/github.test.ts +0 -1800
  264. package/src/work-sources/__tests__/manager.test.ts +0 -529
  265. package/src/work-sources/__tests__/registry.test.ts +0 -477
  266. package/src/work-sources/__tests__/types.test.ts +0 -479
  267. package/src/work-sources/adapters/github.ts +0 -1166
  268. package/src/work-sources/adapters/index.ts +0 -64
  269. package/src/work-sources/errors.ts +0 -71
  270. package/src/work-sources/index.ts +0 -148
  271. package/src/work-sources/manager.ts +0 -413
  272. package/src/work-sources/registry.ts +0 -178
  273. package/src/work-sources/types.ts +0 -161
  274. package/tsconfig.json +0 -9
  275. package/vitest.config.ts +0 -19
@@ -1,723 +0,0 @@
1
- import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
2
- import { mkdir, readFile, rm, realpath, writeFile, stat } from "node:fs/promises";
3
- import { join, dirname, basename } from "node:path";
4
- import { tmpdir } from "node:os";
5
- import {
6
- atomicWriteFile,
7
- atomicWriteYaml,
8
- atomicWriteJson,
9
- appendJsonl,
10
- generateTempPath,
11
- AtomicWriteError,
12
- renameWithRetry,
13
- } from "../atomic.js";
14
- import { parse as parseYaml } from "yaml";
15
-
16
- // Helper to create a temp directory
17
- async function createTempDir(): Promise<string> {
18
- const baseDir = join(
19
- tmpdir(),
20
- `herdctl-atomic-test-${Date.now()}-${Math.random().toString(36).slice(2)}`
21
- );
22
- await mkdir(baseDir, { recursive: true });
23
- // Resolve to real path to handle macOS /var -> /private/var symlink
24
- return await realpath(baseDir);
25
- }
26
-
27
- describe("generateTempPath", () => {
28
- it("generates temp path in the same directory as target", () => {
29
- const targetPath = "/some/dir/file.yaml";
30
- const tempPath = generateTempPath(targetPath);
31
-
32
- expect(dirname(tempPath)).toBe("/some/dir");
33
- });
34
-
35
- it("uses .<filename>.tmp.<random> pattern", () => {
36
- const targetPath = "/some/dir/state.yaml";
37
- const tempPath = generateTempPath(targetPath);
38
- const tempFilename = basename(tempPath);
39
-
40
- expect(tempFilename).toMatch(/^\.state\.yaml\.tmp\.[a-f0-9]{16}$/);
41
- });
42
-
43
- it("generates unique temp paths on each call", () => {
44
- const targetPath = "/some/dir/file.yaml";
45
- const tempPath1 = generateTempPath(targetPath);
46
- const tempPath2 = generateTempPath(targetPath);
47
-
48
- expect(tempPath1).not.toBe(tempPath2);
49
- });
50
-
51
- it("handles filenames with multiple dots", () => {
52
- const targetPath = "/some/dir/file.backup.yaml";
53
- const tempPath = generateTempPath(targetPath);
54
- const tempFilename = basename(tempPath);
55
-
56
- expect(tempFilename).toMatch(/^\.file\.backup\.yaml\.tmp\.[a-f0-9]{16}$/);
57
- });
58
-
59
- it("handles filenames without extensions", () => {
60
- const targetPath = "/some/dir/Makefile";
61
- const tempPath = generateTempPath(targetPath);
62
- const tempFilename = basename(tempPath);
63
-
64
- expect(tempFilename).toMatch(/^\.Makefile\.tmp\.[a-f0-9]{16}$/);
65
- });
66
- });
67
-
68
- describe("AtomicWriteError", () => {
69
- it("creates error with correct properties", () => {
70
- const cause = new Error("Original error");
71
- const error = new AtomicWriteError(
72
- "Failed to write",
73
- "/path/to/file.yaml",
74
- "/path/to/.file.yaml.tmp.abc123",
75
- cause
76
- );
77
-
78
- expect(error.name).toBe("AtomicWriteError");
79
- expect(error.message).toBe("Failed to write");
80
- expect(error.path).toBe("/path/to/file.yaml");
81
- expect(error.tempPath).toBe("/path/to/.file.yaml.tmp.abc123");
82
- expect(error.cause).toBe(cause);
83
- });
84
-
85
- it("creates error without temp path", () => {
86
- const error = new AtomicWriteError("Failed to write", "/path/to/file.yaml");
87
-
88
- expect(error.tempPath).toBeUndefined();
89
- expect(error.cause).toBeUndefined();
90
- });
91
- });
92
-
93
- describe("atomicWriteFile", () => {
94
- let tempDir: string;
95
-
96
- beforeEach(async () => {
97
- tempDir = await createTempDir();
98
- });
99
-
100
- afterEach(async () => {
101
- await rm(tempDir, { recursive: true, force: true });
102
- });
103
-
104
- it("writes content to a new file", async () => {
105
- const filePath = join(tempDir, "test.txt");
106
- const content = "Hello, World!";
107
-
108
- await atomicWriteFile(filePath, content);
109
-
110
- const result = await readFile(filePath, "utf-8");
111
- expect(result).toBe(content);
112
- });
113
-
114
- it("overwrites existing file content", async () => {
115
- const filePath = join(tempDir, "test.txt");
116
- await writeFile(filePath, "old content", "utf-8");
117
-
118
- await atomicWriteFile(filePath, "new content");
119
-
120
- const result = await readFile(filePath, "utf-8");
121
- expect(result).toBe("new content");
122
- });
123
-
124
- it("cleans up temp file on successful write", async () => {
125
- const filePath = join(tempDir, "test.txt");
126
- await atomicWriteFile(filePath, "content");
127
-
128
- // List all files in temp dir
129
- const { readdir } = await import("node:fs/promises");
130
- const files = await readdir(tempDir);
131
-
132
- // Should only have the target file, no temp files
133
- expect(files).toEqual(["test.txt"]);
134
- });
135
-
136
- it("writes with specified encoding", async () => {
137
- const filePath = join(tempDir, "test.txt");
138
- const content = "Unicode: \u4e2d\u6587";
139
-
140
- await atomicWriteFile(filePath, content, "utf-8");
141
-
142
- const result = await readFile(filePath, "utf-8");
143
- expect(result).toBe(content);
144
- });
145
-
146
- it("throws AtomicWriteError when directory does not exist", async () => {
147
- const filePath = join(tempDir, "nonexistent", "test.txt");
148
-
149
- await expect(atomicWriteFile(filePath, "content")).rejects.toThrow(
150
- AtomicWriteError
151
- );
152
- });
153
-
154
- it("preserves original file on write failure", async () => {
155
- const filePath = join(tempDir, "test.txt");
156
- const originalContent = "original content";
157
- await writeFile(filePath, originalContent, "utf-8");
158
-
159
- // Try to write to a location that will fail
160
- const badPath = join(tempDir, "nonexistent", "test.txt");
161
- await expect(atomicWriteFile(badPath, "new content")).rejects.toThrow();
162
-
163
- // Original file should be unchanged
164
- const result = await readFile(filePath, "utf-8");
165
- expect(result).toBe(originalContent);
166
- });
167
-
168
- it("cleans up temp file on failure when directory does not exist", async () => {
169
- // Try to write to a path where the directory doesn't exist
170
- // This will fail at the writeFile stage, and we verify no temp files are left
171
- const badPath = join(tempDir, "nonexistent", "subdir", "test.txt");
172
-
173
- await expect(atomicWriteFile(badPath, "content")).rejects.toThrow(
174
- AtomicWriteError
175
- );
176
-
177
- // The temp file would be in the nonexistent directory, so there's nothing to clean up
178
- // This tests that the error is properly thrown and the function handles the case
179
- // where the temp file couldn't even be created
180
- const { readdir } = await import("node:fs/promises");
181
- const files = await readdir(tempDir);
182
- // Should be empty - no orphaned temp files in the parent
183
- expect(files.filter((f) => f.includes(".tmp."))).toHaveLength(0);
184
- });
185
-
186
- it("handles large file content", async () => {
187
- const filePath = join(tempDir, "large.txt");
188
- // Create 1MB of content
189
- const content = "x".repeat(1024 * 1024);
190
-
191
- await atomicWriteFile(filePath, content);
192
-
193
- const result = await readFile(filePath, "utf-8");
194
- expect(result.length).toBe(content.length);
195
- });
196
-
197
- it("handles empty content", async () => {
198
- const filePath = join(tempDir, "empty.txt");
199
-
200
- await atomicWriteFile(filePath, "");
201
-
202
- const result = await readFile(filePath, "utf-8");
203
- expect(result).toBe("");
204
- });
205
-
206
- it("handles content with newlines", async () => {
207
- const filePath = join(tempDir, "multiline.txt");
208
- const content = "line1\nline2\nline3\n";
209
-
210
- await atomicWriteFile(filePath, content);
211
-
212
- const result = await readFile(filePath, "utf-8");
213
- expect(result).toBe(content);
214
- });
215
- });
216
-
217
- describe("atomicWriteYaml", () => {
218
- let tempDir: string;
219
-
220
- beforeEach(async () => {
221
- tempDir = await createTempDir();
222
- });
223
-
224
- afterEach(async () => {
225
- await rm(tempDir, { recursive: true, force: true });
226
- });
227
-
228
- it("writes object as YAML", async () => {
229
- const filePath = join(tempDir, "config.yaml");
230
- const data = {
231
- version: 1,
232
- fleet: { name: "test-fleet" },
233
- };
234
-
235
- await atomicWriteYaml(filePath, data);
236
-
237
- const content = await readFile(filePath, "utf-8");
238
- const parsed = parseYaml(content);
239
- expect(parsed).toEqual(data);
240
- });
241
-
242
- it("writes array as YAML", async () => {
243
- const filePath = join(tempDir, "list.yaml");
244
- const data = ["item1", "item2", "item3"];
245
-
246
- await atomicWriteYaml(filePath, data);
247
-
248
- const content = await readFile(filePath, "utf-8");
249
- const parsed = parseYaml(content);
250
- expect(parsed).toEqual(data);
251
- });
252
-
253
- it("respects custom indent option", async () => {
254
- const filePath = join(tempDir, "config.yaml");
255
- const data = { nested: { value: 1 } };
256
-
257
- await atomicWriteYaml(filePath, data, { indent: 4 });
258
-
259
- const content = await readFile(filePath, "utf-8");
260
- // With indent 4, nested keys should be indented by 4 spaces
261
- expect(content).toContain(" value:");
262
- });
263
-
264
- it("handles complex nested structures", async () => {
265
- const filePath = join(tempDir, "complex.yaml");
266
- const data = {
267
- version: 1,
268
- agents: [
269
- {
270
- name: "agent1",
271
- config: {
272
- model: "claude-sonnet",
273
- permissions: ["read", "write"],
274
- },
275
- },
276
- {
277
- name: "agent2",
278
- config: {
279
- model: "claude-opus",
280
- permissions: ["read"],
281
- },
282
- },
283
- ],
284
- metadata: {
285
- created: "2024-01-01",
286
- tags: ["production", "test"],
287
- },
288
- };
289
-
290
- await atomicWriteYaml(filePath, data);
291
-
292
- const content = await readFile(filePath, "utf-8");
293
- const parsed = parseYaml(content);
294
- expect(parsed).toEqual(data);
295
- });
296
-
297
- it("handles null and undefined values", async () => {
298
- const filePath = join(tempDir, "nullable.yaml");
299
- const data = {
300
- present: "value",
301
- absent: null,
302
- };
303
-
304
- await atomicWriteYaml(filePath, data);
305
-
306
- const content = await readFile(filePath, "utf-8");
307
- const parsed = parseYaml(content);
308
- expect(parsed.present).toBe("value");
309
- expect(parsed.absent).toBeNull();
310
- });
311
-
312
- it("throws AtomicWriteError on failure", async () => {
313
- const filePath = join(tempDir, "nonexistent", "config.yaml");
314
-
315
- await expect(atomicWriteYaml(filePath, { key: "value" })).rejects.toThrow(
316
- AtomicWriteError
317
- );
318
- });
319
- });
320
-
321
- describe("atomicWriteJson", () => {
322
- let tempDir: string;
323
-
324
- beforeEach(async () => {
325
- tempDir = await createTempDir();
326
- });
327
-
328
- afterEach(async () => {
329
- await rm(tempDir, { recursive: true, force: true });
330
- });
331
-
332
- it("writes object as JSON with trailing newline", async () => {
333
- const filePath = join(tempDir, "data.json");
334
- const data = { key: "value", number: 42 };
335
-
336
- await atomicWriteJson(filePath, data);
337
-
338
- const content = await readFile(filePath, "utf-8");
339
- expect(content.endsWith("\n")).toBe(true);
340
- const parsed = JSON.parse(content);
341
- expect(parsed).toEqual(data);
342
- });
343
-
344
- it("uses default indentation of 2 spaces", async () => {
345
- const filePath = join(tempDir, "data.json");
346
- const data = { nested: { value: 1 } };
347
-
348
- await atomicWriteJson(filePath, data);
349
-
350
- const content = await readFile(filePath, "utf-8");
351
- expect(content).toContain(' "nested":');
352
- expect(content).toContain(' "value":');
353
- });
354
-
355
- it("respects custom indent option", async () => {
356
- const filePath = join(tempDir, "data.json");
357
- const data = { nested: { value: 1 } };
358
-
359
- await atomicWriteJson(filePath, data, { indent: 4 });
360
-
361
- const content = await readFile(filePath, "utf-8");
362
- expect(content).toContain(' "nested":');
363
- expect(content).toContain(' "value":');
364
- });
365
-
366
- it("handles arrays", async () => {
367
- const filePath = join(tempDir, "array.json");
368
- const data = [1, 2, 3, { key: "value" }];
369
-
370
- await atomicWriteJson(filePath, data);
371
-
372
- const content = await readFile(filePath, "utf-8");
373
- const parsed = JSON.parse(content);
374
- expect(parsed).toEqual(data);
375
- });
376
-
377
- it("throws AtomicWriteError on failure", async () => {
378
- const filePath = join(tempDir, "nonexistent", "data.json");
379
-
380
- await expect(atomicWriteJson(filePath, { key: "value" })).rejects.toThrow(
381
- AtomicWriteError
382
- );
383
- });
384
- });
385
-
386
- describe("appendJsonl", () => {
387
- let tempDir: string;
388
-
389
- beforeEach(async () => {
390
- tempDir = await createTempDir();
391
- });
392
-
393
- afterEach(async () => {
394
- await rm(tempDir, { recursive: true, force: true });
395
- });
396
-
397
- it("appends single JSON object as line", async () => {
398
- const filePath = join(tempDir, "log.jsonl");
399
- const data = { event: "test", timestamp: "2024-01-01" };
400
-
401
- await appendJsonl(filePath, data);
402
-
403
- const content = await readFile(filePath, "utf-8");
404
- expect(content).toBe('{"event":"test","timestamp":"2024-01-01"}\n');
405
- });
406
-
407
- it("appends multiple objects as separate lines", async () => {
408
- const filePath = join(tempDir, "log.jsonl");
409
-
410
- await appendJsonl(filePath, { id: 1 });
411
- await appendJsonl(filePath, { id: 2 });
412
- await appendJsonl(filePath, { id: 3 });
413
-
414
- const content = await readFile(filePath, "utf-8");
415
- const lines = content.trim().split("\n");
416
- expect(lines).toHaveLength(3);
417
- expect(JSON.parse(lines[0])).toEqual({ id: 1 });
418
- expect(JSON.parse(lines[1])).toEqual({ id: 2 });
419
- expect(JSON.parse(lines[2])).toEqual({ id: 3 });
420
- });
421
-
422
- it("creates file if it does not exist", async () => {
423
- const filePath = join(tempDir, "new.jsonl");
424
-
425
- await appendJsonl(filePath, { event: "created" });
426
-
427
- const exists = await stat(filePath)
428
- .then(() => true)
429
- .catch(() => false);
430
- expect(exists).toBe(true);
431
- });
432
-
433
- it("appends to existing file", async () => {
434
- const filePath = join(tempDir, "existing.jsonl");
435
- await writeFile(filePath, '{"id":1}\n', "utf-8");
436
-
437
- await appendJsonl(filePath, { id: 2 });
438
-
439
- const content = await readFile(filePath, "utf-8");
440
- const lines = content.trim().split("\n");
441
- expect(lines).toHaveLength(2);
442
- expect(JSON.parse(lines[0])).toEqual({ id: 1 });
443
- expect(JSON.parse(lines[1])).toEqual({ id: 2 });
444
- });
445
-
446
- it("handles complex objects", async () => {
447
- const filePath = join(tempDir, "complex.jsonl");
448
- const data = {
449
- type: "tool_result",
450
- content: { output: "Hello\nWorld", exitCode: 0 },
451
- metadata: { tags: ["test", "prod"] },
452
- };
453
-
454
- await appendJsonl(filePath, data);
455
-
456
- const content = await readFile(filePath, "utf-8");
457
- const parsed = JSON.parse(content.trim());
458
- expect(parsed).toEqual(data);
459
- });
460
-
461
- it("throws AtomicWriteError when directory does not exist", async () => {
462
- const filePath = join(tempDir, "nonexistent", "log.jsonl");
463
-
464
- await expect(appendJsonl(filePath, { event: "test" })).rejects.toThrow(
465
- AtomicWriteError
466
- );
467
- });
468
-
469
- it("handles arrays as JSON lines", async () => {
470
- const filePath = join(tempDir, "arrays.jsonl");
471
-
472
- await appendJsonl(filePath, [1, 2, 3]);
473
- await appendJsonl(filePath, ["a", "b"]);
474
-
475
- const content = await readFile(filePath, "utf-8");
476
- const lines = content.trim().split("\n");
477
- expect(JSON.parse(lines[0])).toEqual([1, 2, 3]);
478
- expect(JSON.parse(lines[1])).toEqual(["a", "b"]);
479
- });
480
-
481
- it("handles primitive values", async () => {
482
- const filePath = join(tempDir, "primitives.jsonl");
483
-
484
- await appendJsonl(filePath, "string");
485
- await appendJsonl(filePath, 42);
486
- await appendJsonl(filePath, true);
487
- await appendJsonl(filePath, null);
488
-
489
- const content = await readFile(filePath, "utf-8");
490
- const lines = content.trim().split("\n");
491
- expect(JSON.parse(lines[0])).toBe("string");
492
- expect(JSON.parse(lines[1])).toBe(42);
493
- expect(JSON.parse(lines[2])).toBe(true);
494
- expect(JSON.parse(lines[3])).toBeNull();
495
- });
496
- });
497
-
498
- describe("renameWithRetry", () => {
499
- let tempDir: string;
500
-
501
- beforeEach(async () => {
502
- tempDir = await createTempDir();
503
- });
504
-
505
- afterEach(async () => {
506
- await rm(tempDir, { recursive: true, force: true });
507
- });
508
-
509
- it("succeeds on first try when rename succeeds", async () => {
510
- const oldPath = join(tempDir, "old.txt");
511
- const newPath = join(tempDir, "new.txt");
512
- await writeFile(oldPath, "content", "utf-8");
513
-
514
- await renameWithRetry(oldPath, newPath);
515
-
516
- const content = await readFile(newPath, "utf-8");
517
- expect(content).toBe("content");
518
- });
519
-
520
- it("retries on EACCES error", async () => {
521
- const oldPath = join(tempDir, "old.txt");
522
- const newPath = join(tempDir, "new.txt");
523
-
524
- let attempts = 0;
525
- const mockRename = async () => {
526
- attempts++;
527
- if (attempts < 3) {
528
- const err = new Error("Access denied") as NodeJS.ErrnoException;
529
- err.code = "EACCES";
530
- throw err;
531
- }
532
- // Success on third attempt
533
- };
534
-
535
- await renameWithRetry(oldPath, newPath, {
536
- renameFn: mockRename,
537
- baseDelayMs: 1, // Fast delays for testing
538
- });
539
-
540
- expect(attempts).toBe(3);
541
- });
542
-
543
- it("retries on EPERM error", async () => {
544
- const oldPath = join(tempDir, "old.txt");
545
- const newPath = join(tempDir, "new.txt");
546
-
547
- let attempts = 0;
548
- const mockRename = async () => {
549
- attempts++;
550
- if (attempts < 2) {
551
- const err = new Error("Permission denied") as NodeJS.ErrnoException;
552
- err.code = "EPERM";
553
- throw err;
554
- }
555
- };
556
-
557
- await renameWithRetry(oldPath, newPath, {
558
- renameFn: mockRename,
559
- baseDelayMs: 1,
560
- });
561
-
562
- expect(attempts).toBe(2);
563
- });
564
-
565
- it("throws immediately on non-retryable error", async () => {
566
- const oldPath = join(tempDir, "old.txt");
567
- const newPath = join(tempDir, "new.txt");
568
-
569
- let attempts = 0;
570
- const mockRename = async () => {
571
- attempts++;
572
- const err = new Error("No such file") as NodeJS.ErrnoException;
573
- err.code = "ENOENT";
574
- throw err;
575
- };
576
-
577
- await expect(
578
- renameWithRetry(oldPath, newPath, { renameFn: mockRename })
579
- ).rejects.toThrow("No such file");
580
-
581
- expect(attempts).toBe(1);
582
- });
583
-
584
- it("throws after max retries exhausted", async () => {
585
- const oldPath = join(tempDir, "old.txt");
586
- const newPath = join(tempDir, "new.txt");
587
-
588
- let attempts = 0;
589
- const mockRename = async () => {
590
- attempts++;
591
- const err = new Error("Access denied") as NodeJS.ErrnoException;
592
- err.code = "EACCES";
593
- throw err;
594
- };
595
-
596
- await expect(
597
- renameWithRetry(oldPath, newPath, {
598
- renameFn: mockRename,
599
- maxRetries: 2,
600
- baseDelayMs: 1,
601
- })
602
- ).rejects.toThrow("Access denied");
603
-
604
- // Initial attempt + 2 retries = 3 total attempts
605
- expect(attempts).toBe(3);
606
- });
607
-
608
- it("uses exponential backoff for delays", async () => {
609
- const oldPath = join(tempDir, "old.txt");
610
- const newPath = join(tempDir, "new.txt");
611
-
612
- const delays: number[] = [];
613
- let lastTime = Date.now();
614
- let attempts = 0;
615
-
616
- const mockRename = async () => {
617
- const now = Date.now();
618
- if (attempts > 0) {
619
- delays.push(now - lastTime);
620
- }
621
- lastTime = now;
622
- attempts++;
623
-
624
- if (attempts <= 3) {
625
- const err = new Error("Access denied") as NodeJS.ErrnoException;
626
- err.code = "EACCES";
627
- throw err;
628
- }
629
- };
630
-
631
- await renameWithRetry(oldPath, newPath, {
632
- renameFn: mockRename,
633
- maxRetries: 3,
634
- baseDelayMs: 10,
635
- });
636
-
637
- // With baseDelayMs=10: delays should be ~10, ~20, ~40
638
- // Allow some tolerance for timing
639
- expect(delays[0]).toBeGreaterThanOrEqual(8);
640
- expect(delays[1]).toBeGreaterThanOrEqual(15);
641
- expect(delays[2]).toBeGreaterThanOrEqual(30);
642
- });
643
-
644
- it("respects custom maxRetries", async () => {
645
- const oldPath = join(tempDir, "old.txt");
646
- const newPath = join(tempDir, "new.txt");
647
-
648
- let attempts = 0;
649
- const mockRename = async () => {
650
- attempts++;
651
- const err = new Error("Access denied") as NodeJS.ErrnoException;
652
- err.code = "EACCES";
653
- throw err;
654
- };
655
-
656
- await expect(
657
- renameWithRetry(oldPath, newPath, {
658
- renameFn: mockRename,
659
- maxRetries: 5,
660
- baseDelayMs: 1,
661
- })
662
- ).rejects.toThrow();
663
-
664
- // Initial attempt + 5 retries = 6 total attempts
665
- expect(attempts).toBe(6);
666
- });
667
- });
668
-
669
- describe("concurrent write safety", () => {
670
- let tempDir: string;
671
-
672
- beforeEach(async () => {
673
- tempDir = await createTempDir();
674
- });
675
-
676
- afterEach(async () => {
677
- await rm(tempDir, { recursive: true, force: true });
678
- });
679
-
680
- it("handles concurrent atomic writes to same file (last write wins)", async () => {
681
- const filePath = join(tempDir, "concurrent.yaml");
682
-
683
- // Start multiple writes concurrently
684
- const writes = [];
685
- for (let i = 0; i < 10; i++) {
686
- writes.push(atomicWriteYaml(filePath, { version: i }));
687
- }
688
-
689
- // Wait for all writes to complete
690
- await Promise.all(writes);
691
-
692
- // File should exist and be valid YAML (one of the versions)
693
- const content = await readFile(filePath, "utf-8");
694
- const parsed = parseYaml(content);
695
- expect(typeof parsed.version).toBe("number");
696
- expect(parsed.version).toBeGreaterThanOrEqual(0);
697
- expect(parsed.version).toBeLessThan(10);
698
- });
699
-
700
- it("handles concurrent JSONL appends", async () => {
701
- const filePath = join(tempDir, "concurrent.jsonl");
702
-
703
- // Start multiple appends concurrently
704
- const appends = [];
705
- for (let i = 0; i < 100; i++) {
706
- appends.push(appendJsonl(filePath, { id: i }));
707
- }
708
-
709
- // Wait for all appends to complete
710
- await Promise.all(appends);
711
-
712
- // All entries should be valid JSON lines
713
- const content = await readFile(filePath, "utf-8");
714
- const lines = content.trim().split("\n");
715
-
716
- // Should have all 100 entries
717
- expect(lines).toHaveLength(100);
718
-
719
- // All lines should be valid JSON
720
- const ids = lines.map((line) => JSON.parse(line).id).sort((a, b) => a - b);
721
- expect(ids).toEqual(Array.from({ length: 100 }, (_, i) => i));
722
- });
723
- });