File size: 2,337 Bytes
77374bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { readFile } from "node:fs/promises";
import { describe, expect, it } from "vitest";

describe("embedded MongoDB packaging", () => {
	it("keeps mongodb-memory-server as a runtime dependency", async () => {
		const packageJson = JSON.parse(
			await readFile(new URL("../../../package.json", import.meta.url), "utf8")
		) as {
			dependencies?: Record<string, string>;
			devDependencies?: Record<string, string>;
		};

		// adapter-node externalizes runtime dependencies but bundles dev dependencies.
		// Bundling this CommonJS package into an ESM chunk drops its __dirname and
		// crashes the no-MONGODB_URL fallback before the app can start.
		expect(packageJson.dependencies?.["mongodb-memory-server"]).toBe("^10.1.2");
		expect(packageJson.devDependencies?.["mongodb-memory-server"]).toBeUndefined();
	});

	it("runs the trap-bearing entrypoint shell as PID 1", async () => {
		const [dockerfile, entrypoint] = await Promise.all([
			readFile(new URL("../../../Dockerfile", import.meta.url), "utf8"),
			readFile(new URL("../../../entrypoint.sh", import.meta.url), "utf8"),
		]);

		// A `bash -c` wrapper receives the container SIGTERM instead of the shell whose
		// traps flush the final MongoDB snapshot during a Space restart.
		expect(dockerfile).toContain('CMD ["/bin/bash", "/app/entrypoint.sh"]');
		expect(dockerfile).not.toContain('CMD ["/bin/bash", "-c", "/app/entrypoint.sh"]');
		expect(dockerfile).not.toContain("npm install -g dotenv-cli");
		expect(entrypoint).toContain("--env-file-if-exists=/app/.env");
		expect(entrypoint).not.toMatch(/\bdotenv\s+-e\b/u);
	});

	it("packages the MongoDB handoff gate at the entrypoint default path", async () => {
		const [dockerfile, entrypoint] = await Promise.all([
			readFile(new URL("../../../Dockerfile", import.meta.url), "utf8"),
			readFile(new URL("../../../entrypoint.sh", import.meta.url), "utf8"),
		]);

		expect(dockerfile).toContain(
			"COPY --chown=1000 mongo-handoff-gate.mjs /app/mongo-handoff-gate.mjs"
		);
		expect(entrypoint).toContain("MONGO_HANDOFF_GATE_EXECUTABLE");
		expect(entrypoint).toContain("MONGO_HANDOFF_GATE_SCRIPT");
		expect(entrypoint).toContain("/app/mongo-handoff-gate.mjs");
		expect(entrypoint).not.toContain("MONGO_HANDOFF_GATE_COMMAND");
		expect(entrypoint).not.toMatch(/bash\s+-c\s+[^\n]*mongo_handoff_gate/u);
	});
});