fix: harden runtime isolation and regression tests

This commit is contained in:
ajnart committed 2026-07-30 00:38:34 +02:00
1 parent 5d9f05e083
commit 9963ba0274
8 files changed
+151 -100

No files matched your search

+13 -4
View File
@@ -3,10 +3,15 @@
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
const registerAsync = async (nodeEnv: string, nextRuntime: string) => {
const loggerLoaded = vi.fn();
const tasksStarted = vi.fn();
const websocketStarted = vi.fn();
vi.stubEnv("NODE_ENV", nodeEnv);
vi.stubEnv("NEXT_RUNTIME", nextRuntime);
vi.doMock("@homarr/core/infrastructure/logs", () => {
loggerLoaded();
return { createLogger: () => ({ error: vi.fn() }) };
});
vi.doMock("@homarr/tasks", () => {
tasksStarted();
return {};
@@ -18,7 +23,7 @@ const registerAsync = async (nodeEnv: string, nextRuntime: string) => {
const { register } = await import("./instrumentation");
await register();
return { tasksStarted, websocketStarted };
return { loggerLoaded, tasksStarted, websocketStarted };
};
describe("Next instrumentation", () => {
@@ -27,28 +32,32 @@ describe("Next instrumentation", () => {
});
afterEach(() => {
vi.doUnmock("@homarr/core/infrastructure/logs");
vi.doUnmock("@homarr/tasks");
vi.doUnmock("@homarr/websocket");
vi.unstubAllEnvs();
});
test("embeds background services in the production Node runtime", async () => {
const { tasksStarted, websocketStarted } = await registerAsync("production", "nodejs");
const { loggerLoaded, tasksStarted, websocketStarted } = await registerAsync("production", "nodejs");
expect(loggerLoaded).toHaveBeenCalledOnce();
expect(tasksStarted).toHaveBeenCalledOnce();
expect(websocketStarted).toHaveBeenCalledOnce();
});
test("does not embed background services in the development server", async () => {
const { tasksStarted, websocketStarted } = await registerAsync("development", "nodejs");
const { loggerLoaded, tasksStarted, websocketStarted } = await registerAsync("development", "nodejs");
expect(loggerLoaded).not.toHaveBeenCalled();
expect(tasksStarted).not.toHaveBeenCalled();
expect(websocketStarted).not.toHaveBeenCalled();
});
test("does not embed Node services in the edge runtime", async () => {
const { tasksStarted, websocketStarted } = await registerAsync("production", "edge");
const { loggerLoaded, tasksStarted, websocketStarted } = await registerAsync("production", "edge");
expect(loggerLoaded).not.toHaveBeenCalled();
expect(tasksStarted).not.toHaveBeenCalled();
expect(websocketStarted).not.toHaveBeenCalled();
});
+2 -4
View File
@@ -1,11 +1,9 @@
import { createLogger } from "@homarr/core/infrastructure/logs";
const logger = createLogger({ module: "instrumentation" });
export async function register() {
if (process.env.NEXT_RUNTIME !== "nodejs") return;
if (process.env.NODE_ENV !== "production") return;
const { createLogger } = await import("@homarr/core/infrastructure/logs");
const logger = createLogger({ module: "instrumentation" });
const services = [
{ name: "tasks", start: () => import("@homarr/tasks") },
{ name: "websocket", start: () => import("@homarr/websocket") },