/** * Unit tests for POST /api/combos/duplicate (Rule #18). * * Coverage: auth gate, input validation (400), success response shape (201) * including weight distribution and naming convention, config fields, * and error sanitization (no stack traces in responses). */ import test from "node:test"; import assert from "node:assert/strict"; import fs from "node:fs"; import os from "os"; import path from "node:path"; // ── DB / auth setup ─────────────────────────────────────────────────────────── const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-combos-duplicate-")); process.env.DATA_DIR = TEST_DATA_DIR; process.env.API_KEY_SECRET = process.env.API_KEY_SECRET ?? "combos-duplicate-test-secret"; const core = await import("../../src/lib/db/core.ts"); const settingsDb = await import("../../src/lib/db/settings.ts"); const apiKeysDb = await import("../../src/lib/db/apiKeys.ts"); // Route loaded AFTER env is set const duplicateRoute = await import("../../src/app/api/combos/duplicate/route.ts"); function makePostRequest(url: string, body: unknown, apiKey?: string): Request { return new Request(url, { method: "POST", headers: { "Content-Type": "application/json", ...(apiKey ? { authorization: `Bearer ${apiKey}` } : {}), }, body: JSON.stringify(body), }); } test.after(() => { core.resetDbInstance(); try { fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true }); } catch { // best-effort cleanup } }); // ── Auth gate ───────────────────────────────────────────────────────────────── test("POST /api/combos/duplicate returns 401/403 when auth is required and no token", async () => { await settingsDb.updateSettings({ requireLogin: true }); process.env.INITIAL_PASSWORD = "test-password-dup"; const req = makePostRequest("http://localhost/api/combos/duplicate", { name: "auto/best-coding", }); const res = await duplicateRoute.POST(req as never); assert.ok( res.status === 401 || res.status === 403, `Expected 401 or 403 without auth, got ${res.status}` ); await settingsDb.updateSettings({ requireLogin: false }); delete process.env.INITIAL_PASSWORD; }); test("POST /api/combos/duplicate passes auth with valid management API key", async () => { await settingsDb.updateSettings({ requireLogin: true }); process.env.INITIAL_PASSWORD = "test-password-dup-key"; const { key } = await apiKeysDb.createApiKey("dup-test", "machine-dup", ["manage"]); // Invalid body (empty name) — auth should pass, business logic rejects const req = makePostRequest("http://localhost/api/combos/duplicate", { name: "" }, key); const res = await duplicateRoute.POST(req as never); assert.ok( !(res.status === 401 || res.status === 403), `Auth should have passed with valid key, got ${res.status}` ); assert.equal(res.status, 400); await settingsDb.updateSettings({ requireLogin: false }); delete process.env.INITIAL_PASSWORD; }); // ── Input validation ──────────────────────────────────────────────────────── test("POST /api/combos/duplicate returns 400 when name is missing", async () => { await settingsDb.updateSettings({ requireLogin: false }); const req = makePostRequest("http://localhost/api/combos/duplicate", {}); const res = await duplicateRoute.POST(req as never); const body = await res.json(); assert.equal(res.status, 400); assert.ok( typeof body.error === "string" && body.error.length > 0, "should return an error message" ); }); test("POST /api/combos/duplicate returns 400 when name is not a string", async () => { await settingsDb.updateSettings({ requireLogin: false }); const req = makePostRequest("http://localhost/api/combos/duplicate", { name: 123, }); const res = await duplicateRoute.POST(req as never); assert.equal(res.status, 400); }); test("POST /api/combos/duplicate returns 400 when name is empty string", async () => { await settingsDb.updateSettings({ requireLogin: false }); const req = makePostRequest("http://localhost/api/combos/duplicate", { name: "", }); const res = await duplicateRoute.POST(req as never); assert.equal(res.status, 400); }); // ── Success response shape (when models match) ─────────────────────────────── test("POST /api/combos/duplicate returns valid combo with correct naming and weights on success", async () => { await settingsDb.updateSettings({ requireLogin: false }); const req = makePostRequest("http://localhost/api/combos/duplicate", { name: "auto/best-coding", }); const res = await duplicateRoute.POST(req as never); const body = await res.json(); if (res.status === 201) { // --- Naming convention: starts with static- and ends with "copy" (or "copy N") --- assert.ok( typeof body.name === "string" && body.name.length > 0, "response should contain combo name" ); assert.ok( body.name.startsWith("static-"), `Combo name should start with 'static-', got: ${body.name}` ); assert.ok( /^static-[\w-]+(\s+\d+)?$/.test(body.name), `Combo name should be 'static-