-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathdaemon-bootstrap.test.ts
More file actions
161 lines (145 loc) · 4.6 KB
/
Copy pathdaemon-bootstrap.test.ts
File metadata and controls
161 lines (145 loc) · 4.6 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
import { describe, expect, it } from "@effect/vitest";
import { createServer } from "node:net";
import * as Effect from "effect/Effect";
import {
buildDaemonSpawnSpec,
canAutoStartLocalDaemonForHost,
chooseDaemonPort,
isDevCliEntrypoint,
parseDaemonBaseUrl,
} from "../apps/cli/src/daemon";
describe("daemon bootstrap helpers", () => {
it("parses default port when none is provided", () => {
const parsed = parseDaemonBaseUrl("http://localhost", 4788);
expect(parsed).toEqual({ hostname: "localhost", port: 4788 });
});
it("parses explicit port from base url", () => {
const parsed = parseDaemonBaseUrl("http://127.0.0.1:9001", 4788);
expect(parsed).toEqual({ hostname: "127.0.0.1", port: 9001 });
});
it("rejects non-http schemes for auto-start", () => {
expect(() => parseDaemonBaseUrl("https://localhost:4788", 4788)).toThrow(
"Only http:// base URLs are supported",
);
});
it("only auto-starts for local hosts", () => {
expect(canAutoStartLocalDaemonForHost("localhost")).toBe(true);
expect(canAutoStartLocalDaemonForHost("127.0.0.1")).toBe(true);
expect(canAutoStartLocalDaemonForHost("::1")).toBe(true);
expect(canAutoStartLocalDaemonForHost("api.example.com")).toBe(false);
});
it("treats source entrypoints as dev mode but excludes bun embedded paths", () => {
expect(isDevCliEntrypoint("/repo/apps/cli/src/main.ts")).toBe(true);
expect(isDevCliEntrypoint("/repo/apps/cli/src/main.js")).toBe(true);
expect(isDevCliEntrypoint("/$bunfs/root/main.js")).toBe(false);
expect(isDevCliEntrypoint("/usr/local/bin/executor")).toBe(false);
expect(isDevCliEntrypoint(undefined)).toBe(false);
});
it("builds bun-run spec in dev mode", () => {
const spec = buildDaemonSpawnSpec({
port: 4788,
hostname: "localhost",
isDevMode: true,
scriptPath: "/repo/apps/cli/src/main.ts",
executablePath: "/ignored",
});
expect(spec.command).toBe("bun");
expect(spec.args).toEqual([
"run",
"/repo/apps/cli/src/main.ts",
"daemon",
"run",
"--port",
"4788",
"--hostname",
"localhost",
"--foreground",
]);
});
it("builds executable spec outside dev mode", () => {
const spec = buildDaemonSpawnSpec({
port: 5000,
hostname: "127.0.0.1",
isDevMode: false,
scriptPath: undefined,
executablePath: "/usr/local/bin/executor",
});
expect(spec.command).toBe("/usr/local/bin/executor");
expect(spec.args).toEqual([
"daemon",
"run",
"--port",
"5000",
"--hostname",
"127.0.0.1",
"--foreground",
]);
});
it("propagates allowed hosts as repeated flags", () => {
const spec = buildDaemonSpawnSpec({
port: 4788,
hostname: "0.0.0.0",
isDevMode: false,
scriptPath: undefined,
executablePath: "/usr/local/bin/executor",
allowedHosts: ["my.box", "other.host"],
});
expect(spec.args).toEqual([
"daemon",
"run",
"--port",
"4788",
"--hostname",
"0.0.0.0",
"--foreground",
"--allowed-host",
"my.box",
"--allowed-host",
"other.host",
]);
});
it("fails in dev mode when script path is missing", () => {
expect(() =>
buildDaemonSpawnSpec({
port: 4788,
hostname: "localhost",
isDevMode: true,
scriptPath: undefined,
executablePath: "/usr/local/bin/executor",
}),
).toThrow("Cannot auto-start daemon in dev mode");
});
it("falls back when preferred daemon port is occupied", async () => {
const blocker = createServer();
await Effect.runPromise(
Effect.scoped(
Effect.gen(function* () {
yield* Effect.acquireRelease(
Effect.callback<void, Error>((resume) => {
blocker.once("error", (error) => resume(Effect.fail(error)));
blocker.listen({ port: 0, host: "127.0.0.1" }, () =>
resume(Effect.succeed(undefined)),
);
}),
() =>
Effect.promise(
() =>
new Promise<void>((resolve) => {
blocker.close(() => resolve());
}),
),
);
const occupied = (() => {
const address = blocker.address();
return typeof address === "object" && address !== null ? address.port : 0;
})();
const picked = yield* chooseDaemonPort({
preferredPort: occupied,
hostname: "127.0.0.1",
});
expect(picked).not.toBe(occupied);
}),
),
);
});
});