import assert from "node:assert/strict"; import { test } from "vitest"; import { createTechLogFeatureInstalledInput } from "../../../src/features/tech-log/adapters/create-tech-log-feature-input.ts"; import { FIXTURE_IDS } from "../../../src/features/tech-log/adapters/mock/fixtures.ts"; import { createMockManagementGateway } from "../../../src/features/tech-log/adapters/mock/mock-management-gateway.ts"; import { createMockStudioState } from "../../../src/features/tech-log/adapters/mock/mock-state.ts"; import { ManagementGatewayError } from "../../../src/features/tech-log/application/ports/management-gateway-error.ts"; import { MOCK_STUDIO_INSTALL_CONTEXT } from "../../helpers/studio-install-context.ts"; function key(name: string) { return { idempotencyKey: `mock-management-${name}` }; } async function rejectsWith(promise: Promise, code: string) { await assert.rejects(promise, (error: unknown) => { assert.ok(error instanceof ManagementGatewayError); assert.equal(error.code, code); return true; }); } test("MOCK Studio reads its management surface without the HTTP executor", async () => { const calls: string[] = []; const { input } = createTechLogFeatureInstalledInput({ ...MOCK_STUDIO_INSTALL_CONTEXT, http: Object.freeze({ async execute(...args: unknown[]) { calls.push(JSON.stringify(args[0])); throw new Error("a MOCK Studio call reached the HTTP executor"); }, }), }); const studio = input.createStudioApplication(); const [topics, projects, questions, focus, releases] = await Promise.all([ studio.managementQueries.listTopics(), studio.managementQueries.listProjects(), studio.managementQueries.listQuestions(), studio.managementQueries.getHomeFocus(), studio.managementQueries.listReleases(), ]); assert.deepEqual(calls, []); assert.deepEqual( topics.map((topic) => topic.slug), ["jpa", "redis", "authentication"], ); assert.deepEqual( projects.items.map((project) => project.slug), ["backend-skeleton", "auth-lab"], ); assert.deepEqual( questions.items.map((question) => question.id), [FIXTURE_IDS.edgeTokenQuestion], ); assert.equal(questions.items[0]?.primaryProject?.slug, "auth-lab"); assert.equal(focus.currentProjectId, FIXTURE_IDS.projectBackend); assert.deepEqual( releases.items.map((release) => release.versionLabel), ["0.1.0"], ); }); test("the mock refuses a stale version with the backend's codes", async () => { const gateway = createMockManagementGateway(); const project = await gateway.getProject(FIXTURE_IDS.projectBackend); await rejectsWith( gateway.updateProject( project.id, { expectedVersion: project.version + 1, name: project.name, oneLinePurpose: "", purposeMarkdown: "", boundaryMarkdown: "", phase: project.phase, technologyLabels: [], targetVisibility: "PUBLIC", }, key("stale"), ), "VERSION_CONFLICT", ); await rejectsWith(gateway.getProject("99999999-9999-4999-8999-999999999999"), "PROJECT_NOT_FOUND"); await rejectsWith(gateway.getRelease("99999999-9999-4999-8999-999999999999"), "RELEASE_NOT_FOUND"); }); test("a replayed idempotency key returns the first draft instead of a second one", async () => { const gateway = createMockManagementGateway(); const first = await gateway.createProject("새 프로젝트", key("create")); const again = await gateway.createProject("새 프로젝트", key("create")); assert.deepEqual(again, first); const projects = await gateway.listProjects(); assert.equal(projects.page.totalElements, 3); }); test("deleting a draft removes it from the shared document state", async () => { const state = createMockStudioState(); const gateway = createMockManagementGateway({ studioState: state }); const draft = state.documents.get(FIXTURE_IDS.conflictCase)!; await rejectsWith( gateway.deleteDocument("CASE", FIXTURE_IDS.fetchJoinCase, state.documents.get(FIXTURE_IDS.fetchJoinCase)!.version, key("published")), "CONFLICT", ); await rejectsWith( gateway.deleteDocument("REFERENCE", draft.id, draft.version, key("wrong-kind")), "DOCUMENT_NOT_FOUND", ); await gateway.deleteDocument("CASE", draft.id, draft.version, key("delete")); assert.equal(state.documents.has(draft.id), false); }); test("an aborted read rejects before it touches the store", async () => { const gateway = createMockManagementGateway(); const controller = new AbortController(); controller.abort(); await assert.rejects(gateway.listTopics({ signal: controller.signal }), { name: "AbortError" }); });