-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathdaemon-state.test.ts
More file actions
194 lines (172 loc) · 5.94 KB
/
Copy pathdaemon-state.test.ts
File metadata and controls
194 lines (172 loc) · 5.94 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { mkdtempSync, rmSync } from "node:fs";
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { describe, expect, it } from "@effect/vitest";
import { Effect, FileSystem, Layer, Path, PlatformError } from "effect";
import {
acquireDaemonStartLock,
canonicalDaemonHost,
currentDaemonScopeId,
isPidAlive,
readDaemonPointer,
readDaemonRecord,
releaseDaemonStartLock,
removeDaemonPointer,
removeDaemonRecord,
writeDaemonPointer,
writeDaemonRecord,
} from "../apps/cli/src/daemon-state";
const fileSystemError = (method: string, cause: unknown) =>
PlatformError.systemError({
_tag: "Unknown",
module: "FileSystem",
method,
description: cause instanceof Error ? cause.message : String(cause),
cause,
});
const fileSystemLayer = FileSystem.layerNoop({
makeDirectory: (path, options) =>
Effect.tryPromise({
try: () => mkdir(path, { recursive: options?.recursive, mode: options?.mode }),
catch: (cause) => fileSystemError("makeDirectory", cause),
}),
writeFileString: (path, data, options) =>
Effect.tryPromise({
try: () => writeFile(path, data, { encoding: "utf8", flag: options?.flag }),
catch: (cause) => fileSystemError("writeFileString", cause),
}),
readFileString: (path, encoding = "utf8") =>
Effect.tryPromise({
try: () => readFile(path, { encoding: encoding as BufferEncoding }),
catch: (cause) => fileSystemError("readFileString", cause),
}),
remove: (path, options) =>
Effect.tryPromise({
try: () => rm(path, { recursive: options?.recursive ?? false, force: options?.force ?? false }),
catch: (cause) => fileSystemError("remove", cause),
}),
});
const daemonStateLayer = Layer.merge(fileSystemLayer, Path.layer);
const withDaemonDataDir = <A, E>(effect: Effect.Effect<A, E, FileSystem.FileSystem | Path.Path>) =>
Effect.gen(function* () {
const prev = process.env.EXECUTOR_DATA_DIR;
const dir = mkdtempSync(join(tmpdir(), "executor-daemon-state-test-"));
process.env.EXECUTOR_DATA_DIR = dir;
try {
return yield* effect;
} finally {
if (prev === undefined) {
delete process.env.EXECUTOR_DATA_DIR;
} else {
process.env.EXECUTOR_DATA_DIR = prev;
}
rmSync(dir, { recursive: true, force: true });
}
}).pipe(Effect.provide(daemonStateLayer));
describe("daemon state", () => {
it("normalizes local host aliases", () => {
expect(canonicalDaemonHost("localhost")).toBe("localhost");
expect(canonicalDaemonHost("127.0.0.1")).toBe("localhost");
expect(canonicalDaemonHost("::1")).toBe("localhost");
expect(canonicalDaemonHost("0.0.0.0")).toBe("localhost");
expect(canonicalDaemonHost("api.example.com")).toBe("api.example.com");
});
it.effect("writes, reads, and removes daemon records", () =>
withDaemonDataDir(
Effect.gen(function* () {
yield* writeDaemonRecord({
hostname: "127.0.0.1",
port: 4788,
pid: 12345,
scopeDir: "/tmp/scope",
});
const stored = yield* readDaemonRecord({ hostname: "localhost", port: 4788 });
expect(stored).toEqual({
version: 1,
hostname: "localhost",
port: 4788,
pid: 12345,
startedAt: expect.any(String),
scopeDir: "/tmp/scope",
});
yield* removeDaemonRecord({ hostname: "localhost", port: 4788 });
const after = yield* readDaemonRecord({ hostname: "localhost", port: 4788 });
expect(after).toBeNull();
}),
),
);
it.effect("writes, reads, and removes daemon pointers", () =>
withDaemonDataDir(
Effect.gen(function* () {
yield* writeDaemonPointer({
hostname: "127.0.0.1",
port: 5799,
pid: 24680,
scopeId: "scope:/tmp/project",
scopeDir: "/tmp/project",
token: "tok_123",
});
const stored = yield* readDaemonPointer({
hostname: "localhost",
scopeId: "scope:/tmp/project",
});
expect(stored).toEqual({
version: 1,
hostname: "localhost",
port: 5799,
pid: 24680,
startedAt: expect.any(String),
scopeId: "scope:/tmp/project",
scopeDir: "/tmp/project",
token: "tok_123",
});
yield* removeDaemonPointer({
hostname: "localhost",
scopeId: "scope:/tmp/project",
});
const after = yield* readDaemonPointer({
hostname: "localhost",
scopeId: "scope:/tmp/project",
});
expect(after).toBeNull();
}),
),
);
it.effect("serializes daemon startup with lock files", () =>
withDaemonDataDir(
Effect.gen(function* () {
const lock = yield* acquireDaemonStartLock({
hostname: "localhost",
scopeId: "scope:/tmp/project",
});
const second = yield* acquireDaemonStartLock({
hostname: "localhost",
scopeId: "scope:/tmp/project",
}).pipe(Effect.flip);
expect(second.message).toContain("already in progress");
yield* releaseDaemonStartLock(lock);
const third = yield* acquireDaemonStartLock({
hostname: "localhost",
scopeId: "scope:/tmp/project",
});
yield* releaseDaemonStartLock(third);
}),
),
);
it("derives scope id from EXECUTOR_SCOPE_DIR or cwd", () => {
const prev = process.env.EXECUTOR_SCOPE_DIR;
process.env.EXECUTOR_SCOPE_DIR = "/tmp/explicit-scope";
expect(currentDaemonScopeId()).toBe("scope:/tmp/explicit-scope");
if (prev === undefined) {
delete process.env.EXECUTOR_SCOPE_DIR;
} else {
process.env.EXECUTOR_SCOPE_DIR = prev;
}
expect(currentDaemonScopeId()).toContain("cwd:");
});
it("detects live and invalid pids", () => {
expect(isPidAlive(process.pid)).toBe(true);
expect(isPidAlive(-1)).toBe(false);
});
});