Files
mcpctl/src/mcpd/tests/bootstrap-ungated-global.test.ts
Michal fe00f42bce
Some checks failed
CI/CD / typecheck (pull_request) Successful in 1m19s
CI/CD / test (pull_request) Failing after 27s
CI/CD / lint (pull_request) Successful in 2m50s
CI/CD / smoke (pull_request) Has been skipped
CI/CD / build (pull_request) Has been skipped
CI/CD / publish (pull_request) Has been skipped
fix(ungated-global): create settings secret with a valid backend FK
bootstrapUngatedGlobal wrote its seed marker into the mcpctl-system-settings
Secret, creating the row with the default backendId '' when it did not exist.
The Secret_backendId_fkey constraint is enforced at insert time, so on a DB
where that secret was absent, mcpd crash-looped on startup (P2003).

Fix: run the seed after bootstrapSecretBackends (so the default plaintext
backend exists) and set backendId to the resolved default on create. If no
backend exists at all, skip writing the marker rather than crash.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-03 01:57:37 +01:00

118 lines
4.0 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest';
import {
bootstrapUngatedGlobal,
UNGATED_GLOBAL_PROJECT_NAME,
SETTING_UNGATED_GLOBAL_SEEDED,
} from '../src/bootstrap/ungated-global.js';
import { SYSTEM_SETTINGS_SECRET } from '../src/bootstrap/system-settings.js';
import type { PrismaClient } from '@prisma/client';
function mockPrisma(opts: { settingsData?: Record<string, unknown> | null } = {}): PrismaClient {
const secretRow = opts.settingsData
? { id: 'secret-1', name: SYSTEM_SETTINGS_SECRET, data: opts.settingsData, version: 3 }
: null;
return {
secret: {
findUnique: vi.fn(async () => secretRow),
update: vi.fn(async () => ({})),
create: vi.fn(async () => ({})),
},
secretBackend: {
findUnique: vi.fn(async () => ({ id: 'backend-default', name: 'default', isDefault: true })),
findFirst: vi.fn(async () => ({ id: 'backend-default', name: 'default', isDefault: true })),
},
user: {
upsert: vi.fn(async () => ({ id: 'sys-user-id', email: 'system@mcpctl.local' })),
},
project: {
upsert: vi.fn(async (args: { where: { name: string } }) => ({ id: 'proj-ug', name: args.where.name })),
},
mcpServer: {
upsert: vi.fn(async (args: { where: { name: string } }) => ({ id: 'srv-calc', name: args.where.name })),
},
projectServer: {
upsert: vi.fn(async () => ({})),
},
} as unknown as PrismaClient;
}
describe('bootstrapUngatedGlobal', () => {
let prisma: PrismaClient;
beforeEach(() => {
prisma = mockPrisma();
});
it('seeds the ungated-global project with the calcmcp server and attaches it', async () => {
await bootstrapUngatedGlobal(prisma);
expect(prisma.project.upsert).toHaveBeenCalledWith(
expect.objectContaining({
where: { name: UNGATED_GLOBAL_PROJECT_NAME },
create: expect.objectContaining({ gated: false, prompt: expect.stringContaining('calcmcp') }),
}),
);
expect(prisma.mcpServer.upsert).toHaveBeenCalledWith(
expect.objectContaining({
where: { name: 'calcmcp' },
create: expect.objectContaining({
dockerImage: expect.stringContaining('calcmcp'),
transport: 'STDIO',
}),
}),
);
expect(prisma.projectServer.upsert).toHaveBeenCalledWith(
expect.objectContaining({
where: { projectId_serverId: { projectId: 'proj-ug', serverId: 'srv-calc' } },
}),
);
});
it('marks the seed as done in the system settings secret', async () => {
await bootstrapUngatedGlobal(prisma);
// Settings secret didn't exist → created with the marker key and a valid backend FK
expect(prisma.secret.create).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({
name: SYSTEM_SETTINGS_SECRET,
backendId: 'backend-default',
data: expect.objectContaining({ [SETTING_UNGATED_GLOBAL_SEEDED]: true }),
}),
}),
);
});
it('does NOT re-seed when the marker is set (deletions stay deleted)', async () => {
prisma = mockPrisma({ settingsData: { [SETTING_UNGATED_GLOBAL_SEEDED]: true } });
await bootstrapUngatedGlobal(prisma);
expect(prisma.project.upsert).not.toHaveBeenCalled();
expect(prisma.mcpServer.upsert).not.toHaveBeenCalled();
expect(prisma.projectServer.upsert).not.toHaveBeenCalled();
expect(prisma.secret.update).not.toHaveBeenCalled();
expect(prisma.secret.create).not.toHaveBeenCalled();
});
it('preserves existing settings keys when writing the marker', async () => {
prisma = mockPrisma({ settingsData: { allowSelfPasswordChange: false } });
await bootstrapUngatedGlobal(prisma);
expect(prisma.secret.update).toHaveBeenCalledWith(
expect.objectContaining({
where: { name: SYSTEM_SETTINGS_SECRET },
data: expect.objectContaining({
data: {
allowSelfPasswordChange: false,
[SETTING_UNGATED_GLOBAL_SEEDED]: true,
},
version: 4,
}),
}),
);
});
});