-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdiscover-containers.test.ts
More file actions
267 lines (199 loc) · 9.95 KB
/
Copy pathdiscover-containers.test.ts
File metadata and controls
267 lines (199 loc) · 9.95 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { Project } from "ts-morph";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { discoverContainers } from "../src/discover-containers";
import { emitContainers, emitServer, emitShard } from "../src/emit";
import type { SchemaIR } from "../src/ir";
let workdir: string;
const newProject = (): Project => new Project({ skipAddingFilesFromTsConfig: true, useInMemoryFileSystem: false });
const writeContainers = (source: string): void => {
writeFileSync(join(workdir, "containers.ts"), source);
};
const EMPTY_SCHEMA: SchemaIR = { tables: [], vectorIndexes: [] };
describe("discover-containers", () => {
beforeEach(() => {
workdir = mkdtempSync(join(tmpdir(), "lunora-container-disco-"));
});
afterEach(() => {
rmSync(workdir, { force: true, recursive: true });
});
it("returns [] when lunora/containers.ts does not exist", () => {
expect.assertions(1);
expect(discoverContainers(newProject(), workdir)).toEqual([]);
});
it("lifts exported defineContainer declarations into IR, sorted by export name", () => {
expect.assertions(1);
writeContainers(`
import { defineContainer } from "@lunora/container";
export const transcoder = defineContainer({
image: "./containers/transcoder",
defaultPort: 8080,
instanceType: "standard-1",
maxInstances: 5,
sleepAfter: "5m",
});
export const imageResizer = defineContainer({
image: { registry: "docker.io/acme/resizer:2.0" },
instanceType: { vcpu: 1, memoryMib: 4096 },
name: "resizer-pool",
});
`);
expect(discoverContainers(newProject(), workdir)).toEqual([
{
bindingName: "CONTAINER_IMAGE_RESIZER",
className: "ImageResizerContainer",
exportName: "imageResizer",
image: { kind: "registry", reference: "docker.io/acme/resizer:2.0" },
instanceType: { memoryMib: 4096, vcpu: 1 },
name: "resizer-pool",
},
{
bindingName: "CONTAINER_TRANSCODER",
className: "TranscoderContainer",
exportName: "transcoder",
image: { buildContext: "./containers/transcoder", dockerfilePath: "./containers/transcoder/Dockerfile", kind: "dockerfile" },
instanceType: "standard-1",
maxInstances: 5,
sleepAfter: "5m",
},
]);
});
it("ignores non-defineContainer exports and unexported definitions", () => {
expect.assertions(1);
writeContainers(`
import { defineContainer } from "@lunora/container";
export const notAContainer = { image: "./x" };
const internalOnly = defineContainer({ image: "./internal" });
export const worker = defineContainer({ image: "./containers/worker" });
`);
expect(discoverContainers(newProject(), workdir).map((container) => container.exportName)).toEqual(["worker"]);
});
it("resolves an aliased defineContainer import", () => {
expect.assertions(1);
writeContainers(`
import { defineContainer as dc } from "@lunora/container";
export const worker = dc({ image: "./containers/worker" });
`);
expect(discoverContainers(newProject(), workdir).map((container) => container.className)).toEqual(["WorkerContainer"]);
});
it("rejects a non-literal image with a located diagnostic", () => {
expect.assertions(1);
writeContainers(`
import { defineContainer } from "@lunora/container";
const path = "./containers/worker";
export const worker = defineContainer({ image: path });
`);
expect(() => discoverContainers(newProject(), workdir)).toThrow("`image` must be a static string path");
});
it("rejects a missing image", () => {
expect.assertions(1);
writeContainers(`
import { defineContainer } from "@lunora/container";
export const worker = defineContainer({ defaultPort: 8080 });
`);
expect(() => discoverContainers(newProject(), workdir)).toThrow("requires a static `image` property");
});
it("rejects a non-literal maxInstances", () => {
expect.assertions(1);
writeContainers(`
import { defineContainer } from "@lunora/container";
const n = 5;
export const worker = defineContainer({ image: "./w", maxInstances: n });
`);
expect(() => discoverContainers(newProject(), workdir)).toThrow("`maxInstances` must be a static number literal");
});
it("allows non-literal runtime-only fields (env, sleepAfter)", () => {
expect.assertions(2);
writeContainers(`
import { defineContainer } from "@lunora/container";
const level = process.env.LOG_LEVEL ?? "info";
export const worker = defineContainer({ image: "./w", env: { LOG_LEVEL: level }, sleepAfter: 60 * 5 });
`);
const [container] = discoverContainers(newProject(), workdir);
expect(container).toBeDefined();
// sleepAfter was a non-literal expression — lifted as undefined, not an error.
expect(container?.sleepAfter).toBeUndefined();
});
it("lifts a Railpack { build } image source", () => {
expect.assertions(1);
writeContainers(`
import { defineContainer } from "@lunora/container";
export const worker = defineContainer({ image: { build: "./services/worker/" } });
`);
expect(discoverContainers(newProject(), workdir)[0]?.image).toStrictEqual({ buildDir: "./services/worker", kind: "build" });
});
it("lifts literal enableInternet and sleepAfter for the advisor", () => {
expect.assertions(2);
writeContainers(`
import { defineContainer } from "@lunora/container";
export const worker = defineContainer({ image: "./w", enableInternet: false, sleepAfter: "30s" });
`);
const [container] = discoverContainers(newProject(), workdir);
expect(container?.enableInternet).toBe(false);
expect(container?.sleepAfter).toBe("30s");
});
});
describe("emit (containers)", () => {
beforeEach(() => {
workdir = mkdtempSync(join(tmpdir(), "lunora-container-emit-"));
});
afterEach(() => {
rmSync(workdir, { force: true, recursive: true });
});
const discover = (): ReturnType<typeof discoverContainers> => {
writeContainers(`
import { defineContainer } from "@lunora/container";
export const transcoder = defineContainer({ image: "./containers/transcoder", maxInstances: 5 });
`);
return discoverContainers(newProject(), workdir);
};
it("emitContainers renders one thin DO class per definition", () => {
expect.assertions(6);
const content = emitContainers(discover());
expect(content).toContain('import { LunoraContainer } from "@lunora/container/do";');
expect(content).toContain('import { transcoder } from "../containers.js";');
expect(content).toContain('export { ContainerProxy } from "@lunora/container/do";');
expect(content).toContain("export class TranscoderContainer extends LunoraContainer {");
expect(content).toContain('super(ctx, env, transcoder, "transcoder");');
expect(content).toContain("Re-export them from your worker entry");
});
it('emitContainers returns "" without containers', () => {
expect.assertions(1);
expect(emitContainers([])).toBe("");
});
it("emitContainers passes the schema jurisdiction to the base class when declared", () => {
expect.assertions(1);
expect(emitContainers(discover(), "us")).toContain('super(ctx, env, transcoder, "transcoder", "us");');
});
it("emitContainers omits the jurisdiction arg when undeclared (unchanged output)", () => {
expect.assertions(1);
expect(emitContainers(discover())).toContain('super(ctx, env, transcoder, "transcoder");');
});
it("emitServer types ctx.containers on ActionCtx only when containers exist", () => {
expect.assertions(4);
const withContainers = emitServer({ schema: EMPTY_SCHEMA, containers: discover() });
expect(withContainers).toContain('import type { ContainerAccessor } from "@lunora/container";');
expect(withContainers).toContain("readonly containers: {");
expect(withContainers).toContain("readonly transcoder: ContainerAccessor;");
expect(emitServer({ schema: EMPTY_SCHEMA })).not.toContain("readonly containers: {");
});
it("emitShard wires createContainerContext into the built ctx", () => {
expect.assertions(4);
const shard = emitShard({ schema: EMPTY_SCHEMA, containers: discover() });
expect(shard).toContain('import { createContainerContext } from "@lunora/container";');
expect(shard).toContain('{ binding: "CONTAINER_TRANSCODER", exportName: "transcoder", maxInstances: 5 },');
expect(shard).toContain("const containers = createContainerContext(env, LUNORA_CONTAINERS);");
expect(shard).toContain("containers,");
});
it("emitShard stays container-free without definitions", () => {
expect.assertions(1);
expect(emitShard({ schema: EMPTY_SCHEMA })).not.toContain("LUNORA_CONTAINERS");
});
it("emitShard pins ctx.containers to the schema jurisdiction when declared", () => {
expect.assertions(1);
const shard = emitShard({ schema: { ...EMPTY_SCHEMA, jurisdiction: "us" }, containers: discover() });
expect(shard).toContain('const containers = createContainerContext(env, LUNORA_CONTAINERS, "us");');
});
});