Stabilize dashboard tests against constructor and request-harness edge cases. - update dashboard route and service tests to use constructor-safe mock implementations and reset targeted spies between cases - make the test request helper resume its mock socket, always report successful writes, and preserve end callbacks - tighten board workflow route assertions around explicit workflow-column flag disabling and cache-bounded slim task snapshots Files changed: .../__tests__/auth-middleware-integration.test.ts | 4 ++-- .../src/__tests__/browse-directory-routes.test.ts | 4 ++-- packages/dashboard/src/__tests__/chat.test.ts | 3 ++- .../src/__tests__/discovery-routes.test.ts | 4 ++-- .../src/__tests__/docker-node-routes.test.ts | 4 ++-- .../__tests__/experiment-routes.finalize.test.ts | 2 +- .../src/__tests__/github-issue-comment.test.ts | 4 ++-- .../dashboard/src/__tests__/github-poll.test.ts | 4 ++-- .../__tests__/github-source-issue-close.test.ts | 4 ++-- .../github-source-issue-reconciler.test.ts | 4 ++-- .../src/__tests__/github-tracking-comments.test.ts | 4 ++-- .../src/__tests__/github-tracking-delete.test.ts | 4 ++-- .../src/__tests__/github-tracking-hook.test.ts | 4 ++-- ...ithub-tracking-periodic-reconcile-sweep.test.ts | 12 +++++----- .../__tests__/github-tracking-reconciler.test.ts | 4 ++-- .../src/__tests__/github-tracking-state.test.ts | 4 ++-- .../src/__tests__/github-tracking-unlink.test.ts | 4 ++-- .../src/__tests__/github-tracking.test.ts | 4 ++-- .../dashboard/src/__tests__/mesh-routes.test.ts | 4 ++-- .../dashboard/src/__tests__/node-routes.test.ts | 4 ++-- .../src/__tests__/pi-extensions-routes.test.ts | 10 ++++----- .../src/__tests__/plugin-routes.routes.test.ts | 4 ++-- .../dashboard/src/__tests__/plugin-routes.test.ts | 4 ++-- .../__tests__/project-pause-resume-routes.test.ts | 4 ++-- .../dashboard/src/__tests__/project-routes.test.ts | 4 ++-- .../dashboard/src/__tests__/routes-agents.test.ts | 4 ++-- .../dashboard/src/__tests__/routes-auth.test.ts | 6 +++-- .../src/__tests__/routes-automation.test.ts | 4 ++-- .../dashboard/src/__tests__/routes-git.test.ts | 4 ++-- .../dashboard/src/__tests__/routes-github.test.ts | 4 ++-- .../src/__tests__/routes-planning-tracking.test.ts | 1 + .../src/__tests__/routes-planning.test.ts | 4 ++-- .../dashboard/src/__tests__/routes-proxy.test.ts | 8 +++---- .../src/__tests__/routes-settings.test.ts | 4 ++-- .../dashboard/src/__tests__/routes-system.test.ts | 7 ++++-- .../src/__tests__/routes-tasks-ops.test.ts | 4 ++-- .../dashboard/src/__tests__/routes-tasks.test.ts | 4 ++-- .../src/__tests__/session-resume-history.test.ts | 1 + .../dashboard/src/__tests__/setup-routes.test.ts | 8 +++---- .../shared-branch-group-entry-points.test.ts | 4 ++-- .../routes/__tests__/board-workflows-route.test.ts | 26 +++++++++++++--------- .../src/routes/__tests__/custom-providers.test.ts | 4 ++-- .../routes/__tests__/docker-node-routes.test.ts | 4 ++-- .../__tests__/register-docker-node-routes.test.ts | 6 ++--- .../register-docker-provisioning-routes.test.ts | 10 ++++----- packages/dashboard/src/test-request.ts | 7 ++++-- 46 files changed, 126 insertions(+), 109 deletions(-) Fusion-Task-Id: FN-6146 Fusion-Task-Lineage: 989e9da3-92ed-43bd-a505-6114de4f1120
194 lines
8.7 KiB
TypeScript
194 lines
8.7 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { EventEmitter } from "node:events";
|
|
import type { Task } from "@fusion/core";
|
|
import { request } from "../test-request.js";
|
|
import { createServer } from "../server.js";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
mockInit: vi.fn().mockResolvedValue(undefined),
|
|
mockClose: vi.fn().mockResolvedValue(undefined),
|
|
mockGetNode: vi.fn(),
|
|
mockUpdateNode: vi.fn(),
|
|
mockRegisterNode: vi.fn(),
|
|
mockValidateDockerNodeConfig: vi.fn(),
|
|
mockSanitizeDockerNodeConfigForResponse: vi.fn((config) => config),
|
|
}));
|
|
|
|
vi.mock("@fusion/core", async () => {
|
|
const actual = await vi.importActual<typeof import("@fusion/core")>("@fusion/core");
|
|
return {
|
|
...actual,
|
|
CentralCore: vi.fn().mockImplementation(function () { return {
|
|
init: mocks.mockInit,
|
|
close: mocks.mockClose,
|
|
getNode: mocks.mockGetNode,
|
|
updateNode: mocks.mockUpdateNode,
|
|
registerNode: mocks.mockRegisterNode,
|
|
}; }),
|
|
validateDockerNodeConfig: mocks.mockValidateDockerNodeConfig,
|
|
sanitizeDockerNodeConfigForResponse: mocks.mockSanitizeDockerNodeConfigForResponse,
|
|
};
|
|
});
|
|
|
|
class MockStore extends EventEmitter {
|
|
getRootDir() { return "/tmp/fn-3114"; }
|
|
getFusionDir() { return "/tmp/fn-3114/.fusion"; }
|
|
getDatabase() {
|
|
return {
|
|
exec: vi.fn(),
|
|
prepare: vi.fn().mockReturnValue({
|
|
run: vi.fn().mockReturnValue({ changes: 0 }),
|
|
get: vi.fn(),
|
|
all: vi.fn().mockReturnValue([]),
|
|
}),
|
|
};
|
|
}
|
|
getMissionStore() { return { listMissions: vi.fn().mockResolvedValue([]) }; }
|
|
async listTasks(): Promise<Task[]> { return []; }
|
|
}
|
|
|
|
const app = createServer(new MockStore() as any);
|
|
|
|
const config = {
|
|
image: "runfusion/fusion:latest",
|
|
volumeMounts: [{ hostPath: "fusion-data", containerPath: "/app/.fusion", mode: "rw", type: "volume" }],
|
|
environment: { API_KEY: "x", NORMAL: "y" },
|
|
host: { tlsKey: "/secrets/key.pem" },
|
|
configVersion: 1,
|
|
};
|
|
|
|
describe("docker node config routes", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mocks.mockValidateDockerNodeConfig.mockReturnValue({ valid: true, config });
|
|
mocks.mockSanitizeDockerNodeConfigForResponse.mockImplementation((value) => ({
|
|
...value,
|
|
environment: { ...value.environment, API_KEY: "***" },
|
|
host: value.host ? { ...value.host, tlsKey: "***" } : undefined,
|
|
}));
|
|
});
|
|
|
|
it("GET /api/nodes/:id/docker-config returns 404 for missing node", async () => {
|
|
mocks.mockGetNode.mockResolvedValue(undefined);
|
|
const res = await request(app, "GET", "/api/nodes/node-1/docker-config");
|
|
expect(res.status).toBe(404);
|
|
});
|
|
|
|
it("GET /api/nodes/:id/docker-config returns null when no config", async () => {
|
|
mocks.mockGetNode.mockResolvedValue({ id: "node-1", dockerConfig: undefined });
|
|
const res = await request(app, "GET", "/api/nodes/node-1/docker-config");
|
|
expect(res.status).toBe(200);
|
|
expect(res.body).toBeNull();
|
|
});
|
|
|
|
it("GET returns sanitized config", async () => {
|
|
mocks.mockGetNode.mockResolvedValue({ id: "node-1", dockerConfig: config });
|
|
const res = await request(app, "GET", "/api/nodes/node-1/docker-config");
|
|
expect(res.status).toBe(200);
|
|
expect((res.body as any).environment.API_KEY).toBe("***");
|
|
expect((res.body as any).host.tlsKey).toBe("***");
|
|
});
|
|
|
|
it("PUT validates and returns 400 on invalid config", async () => {
|
|
mocks.mockValidateDockerNodeConfig.mockReturnValue({ valid: false, errors: ["bad"] });
|
|
const res = await request(app, "PUT", "/api/nodes/node-1/docker-config", JSON.stringify({ bad: true }), { "Content-Type": "application/json" });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it("PUT returns 404 for missing node", async () => {
|
|
mocks.mockGetNode.mockResolvedValue(undefined);
|
|
const res = await request(app, "PUT", "/api/nodes/node-1/docker-config", JSON.stringify(config), { "Content-Type": "application/json" });
|
|
expect(res.status).toBe(404);
|
|
});
|
|
|
|
it("PUT accepts raw config and returns sanitized", async () => {
|
|
mocks.mockGetNode.mockResolvedValue({ id: "node-1", dockerConfig: config });
|
|
mocks.mockUpdateNode.mockResolvedValue({ dockerConfig: { ...config, configVersion: 2 } });
|
|
const res = await request(app, "PUT", "/api/nodes/node-1/docker-config", JSON.stringify(config), { "Content-Type": "application/json" });
|
|
expect(res.status).toBe(200);
|
|
expect(mocks.mockUpdateNode).toHaveBeenCalledWith("node-1", { dockerConfig: config });
|
|
expect((res.body as any).environment.API_KEY).toBe("***");
|
|
});
|
|
|
|
it("PATCH merges partial updates with volume replacement and env null-removal", async () => {
|
|
mocks.mockGetNode.mockResolvedValue({
|
|
id: "node-1",
|
|
dockerConfig: {
|
|
...config,
|
|
environment: { KEEP: "x", DROP: "y", EMPTY: "" },
|
|
volumeMounts: [{ hostPath: "old", containerPath: "/old", mode: "rw", type: "bind" }],
|
|
},
|
|
});
|
|
mocks.mockUpdateNode.mockResolvedValue({ dockerConfig: { ...config, configVersion: 2 } });
|
|
|
|
const patch = {
|
|
volumeMounts: [{ hostPath: "new", containerPath: "/new", mode: "ro", type: "volume" }],
|
|
environment: { DROP: null, ADD: "z", EMPTY: "" },
|
|
};
|
|
const res = await request(app, "PATCH", "/api/nodes/node-1/docker-config", JSON.stringify(patch), { "Content-Type": "application/json" });
|
|
expect(res.status).toBe(200);
|
|
expect(mocks.mockValidateDockerNodeConfig).toHaveBeenCalledWith(expect.objectContaining({
|
|
volumeMounts: patch.volumeMounts,
|
|
environment: { KEEP: "x", EMPTY: "", ADD: "z" },
|
|
}));
|
|
});
|
|
|
|
it("PATCH returns 404 for missing node", async () => {
|
|
mocks.mockGetNode.mockResolvedValue(undefined);
|
|
const res = await request(app, "PATCH", "/api/nodes/node-1/docker-config", JSON.stringify({ image: "x" }), { "Content-Type": "application/json" });
|
|
expect(res.status).toBe(404);
|
|
});
|
|
|
|
it("PATCH returns 400 when node has no existing config", async () => {
|
|
mocks.mockGetNode.mockResolvedValue({ id: "node-1", dockerConfig: undefined });
|
|
const res = await request(app, "PATCH", "/api/nodes/node-1/docker-config", JSON.stringify({ image: "x" }), { "Content-Type": "application/json" });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it("GET /diff returns 404 for missing node", async () => {
|
|
mocks.mockGetNode.mockResolvedValue(undefined);
|
|
const res = await request(app, "GET", "/api/nodes/node-1/docker-config/diff");
|
|
expect(res.status).toBe(404);
|
|
});
|
|
|
|
it("GET /diff returns null config for non-docker node", async () => {
|
|
mocks.mockGetNode.mockResolvedValue({ id: "node-1", dockerConfig: undefined });
|
|
const res = await request(app, "GET", "/api/nodes/node-1/docker-config/diff");
|
|
expect(res.status).toBe(200);
|
|
expect(res.body).toEqual({ config: null });
|
|
});
|
|
|
|
it("GET /diff returns v1 diff payload", async () => {
|
|
mocks.mockGetNode.mockResolvedValue({ id: "node-1", dockerConfig: { ...config, configVersion: 3 } });
|
|
const res = await request(app, "GET", "/api/nodes/node-1/docker-config/diff");
|
|
expect(res.status).toBe(200);
|
|
expect(res.body).toEqual({ persistedVersion: 3, deployedVersion: null, needsRecreate: false });
|
|
});
|
|
|
|
it("config version increments across PUT/PATCH responses", async () => {
|
|
mocks.mockGetNode
|
|
.mockResolvedValueOnce({ id: "node-1", dockerConfig: { ...config, configVersion: 1 } })
|
|
.mockResolvedValueOnce({ id: "node-1", dockerConfig: { ...config, configVersion: 2 } });
|
|
mocks.mockUpdateNode
|
|
.mockResolvedValueOnce({ dockerConfig: { ...config, configVersion: 2 } })
|
|
.mockResolvedValueOnce({ dockerConfig: { ...config, configVersion: 3 } });
|
|
|
|
const putRes = await request(app, "PUT", "/api/nodes/node-1/docker-config", JSON.stringify(config), { "Content-Type": "application/json" });
|
|
const patchRes = await request(app, "PATCH", "/api/nodes/node-1/docker-config", JSON.stringify({ image: "runfusion/fusion:stable" }), { "Content-Type": "application/json" });
|
|
|
|
expect(putRes.status).toBe(200);
|
|
expect(patchRes.status).toBe(200);
|
|
expect((putRes.body as any).configVersion).toBe(2);
|
|
expect((patchRes.body as any).configVersion).toBe(3);
|
|
});
|
|
|
|
it("POST/PATCH /api/nodes pass through dockerConfig", async () => {
|
|
mocks.mockRegisterNode.mockResolvedValue({ id: "node-1" });
|
|
mocks.mockUpdateNode.mockResolvedValue({ id: "node-1" });
|
|
await request(app, "POST", "/api/nodes", JSON.stringify({ name: "n", type: "remote", url: "http://x", dockerConfig: config }), { "Content-Type": "application/json" });
|
|
await request(app, "PATCH", "/api/nodes/node-1", JSON.stringify({ dockerConfig: config }), { "Content-Type": "application/json" });
|
|
expect(mocks.mockRegisterNode).toHaveBeenCalledWith(expect.objectContaining({ dockerConfig: config }));
|
|
expect(mocks.mockUpdateNode).toHaveBeenCalledWith("node-1", expect.objectContaining({ dockerConfig: config }));
|
|
});
|
|
});
|