|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { createApp } from "../../src/api/routes"; |
| 3 | +import { buildTaskGraph, IDEA_TITLE_MAX_CHARS, validateIdeaSubmission } from "../../src/idea-intake"; |
| 4 | +import { createTestEnv } from "../helpers/d1"; |
| 5 | + |
| 6 | +// #6755: POST /v1/loop/intake-idea — the REST mirror bringing loopover_intake_idea to the same parity its |
| 7 | +// same-tier sibling loopover_check_slop_risk (/v1/lint/slop-risk) already has. The route delegates to the pure |
| 8 | +// validateIdeaSubmission/buildTaskGraph (covered by their own unit tests), so these pin the ROUTE contract: the |
| 9 | +// task-graph and verdict are returned unmodified, a malformed/empty submission comes back as the engine's |
| 10 | +// actionable error list rather than a silent failure, and the deliberately-loose schema still lets the engine |
| 11 | +// (not zod) own the real bounds — e.g. an out-of-range `priority` is a string, so only the engine rejects it. |
| 12 | +const apiHeaders = (env: Env) => ({ authorization: `Bearer ${env.LOOPOVER_API_TOKEN}`, "content-type": "application/json" }); |
| 13 | +const PATH = "/v1/loop/intake-idea"; |
| 14 | + |
| 15 | +const post = (env: Env, body: unknown) => |
| 16 | + createApp().request(PATH, { method: "POST", headers: apiHeaders(env), body: JSON.stringify(body) }, env); |
| 17 | + |
| 18 | +const VALID = { id: "idea-1", title: "Retry uploads on 5xx", body: "Uploads fail silently on 5xx.", targetRepo: "acme/widgets" }; |
| 19 | + |
| 20 | +describe("POST /v1/loop/intake-idea (#6755)", () => { |
| 21 | + it("turns a valid submission into a scored task-graph", async () => { |
| 22 | + const env = createTestEnv(); |
| 23 | + const response = await post(env, VALID); |
| 24 | + expect(response.status).toBe(200); |
| 25 | + const payload = (await response.json()) as { ok: boolean; verdict: string; taskGraph: { ideaId: string; issues: unknown[] } }; |
| 26 | + expect(payload.ok).toBe(true); |
| 27 | + expect(["go", "raise", "avoid"]).toContain(payload.verdict); |
| 28 | + expect(payload.taskGraph.ideaId).toBe("idea-1"); |
| 29 | + // No decomposition supplied => the single-issue baseline. |
| 30 | + expect(payload.taskGraph.issues).toHaveLength(1); |
| 31 | + }); |
| 32 | + |
| 33 | + it("assembles the caller-supplied decomposition instead of the baseline", async () => { |
| 34 | + const env = createTestEnv(); |
| 35 | + const response = await post(env, { |
| 36 | + ...VALID, |
| 37 | + decomposition: [ |
| 38 | + { key: "a", title: "Add retry helper", body: "Introduce the helper." }, |
| 39 | + { key: "b", title: "Wire the helper in", body: "Use it in the upload client.", dependsOn: ["a"] }, |
| 40 | + ], |
| 41 | + }); |
| 42 | + expect(response.status).toBe(200); |
| 43 | + const payload = (await response.json()) as { ok: boolean; taskGraph: { issues: Array<{ key: string }> } }; |
| 44 | + expect(payload.ok).toBe(true); |
| 45 | + expect(payload.taskGraph.issues.map((i) => i.key)).toEqual(["a", "b"]); |
| 46 | + }); |
| 47 | + |
| 48 | + it("returns exactly what the pure bridge returns for every accepted shape", async () => { |
| 49 | + const env = createTestEnv(); |
| 50 | + const cases: unknown[] = [ |
| 51 | + VALID, |
| 52 | + { ...VALID, priority: "high" }, |
| 53 | + { ...VALID, priority: "normal" }, |
| 54 | + { ...VALID, constraints: ["no new deps"], acceptanceHints: ["covered by a unit test"] }, |
| 55 | + { ...VALID, decomposition: [{ key: "a", title: "Only issue", body: "Body." }] }, |
| 56 | + { ...VALID, decomposition: [{ key: "a", title: "First", body: "Body." }, { key: "b", title: "Second", body: "Body.", dependsOn: ["a"] }] }, |
| 57 | + ]; |
| 58 | + for (const body of cases) { |
| 59 | + const response = await post(env, body); |
| 60 | + expect(response.status, JSON.stringify(body)).toBe(200); |
| 61 | + // PARITY: the route must return exactly what the pure functions the MCP tool calls return. |
| 62 | + const validated = validateIdeaSubmission(body); |
| 63 | + expect(validated.ok, JSON.stringify(body)).toBe(true); |
| 64 | + if (!validated.ok) continue; |
| 65 | + const graph = buildTaskGraph(validated.idea, (body as { decomposition?: never }).decomposition); |
| 66 | + await expect(response.json(), JSON.stringify(body)).resolves.toEqual( |
| 67 | + JSON.parse(JSON.stringify({ ok: true, verdict: graph.rubric.verdict, taskGraph: graph })), |
| 68 | + ); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + it("returns the engine's actionable error list for a malformed or empty submission", async () => { |
| 73 | + const env = createTestEnv(); |
| 74 | + // Each of these passes the deliberately-loose zod schema and is rejected by the engine instead. |
| 75 | + const cases: Array<[unknown, string]> = [ |
| 76 | + [{}, "id_required"], |
| 77 | + [{ ...VALID, id: "" }, "id_required"], |
| 78 | + [{ ...VALID, title: "" }, "title_required"], |
| 79 | + [{ ...VALID, body: "" }, "body_required"], |
| 80 | + [{ ...VALID, targetRepo: "" }, "target_repo_required"], |
| 81 | + [{ ...VALID, targetRepo: "not-a-repo" }, "target_repo_malformed"], |
| 82 | + [{ ...VALID, title: "x".repeat(IDEA_TITLE_MAX_CHARS + 1) }, "title_too_long"], |
| 83 | + [{ ...VALID, priority: "urgent" }, "priority_invalid"], |
| 84 | + ]; |
| 85 | + for (const [body, expectedError] of cases) { |
| 86 | + const response = await post(env, body); |
| 87 | + expect(response.status, JSON.stringify(body)).toBe(400); |
| 88 | + const payload = (await response.json()) as { ok: boolean; errors: string[] }; |
| 89 | + expect(payload.ok, JSON.stringify(body)).toBe(false); |
| 90 | + expect(payload.errors, JSON.stringify(body)).toContain(expectedError); |
| 91 | + } |
| 92 | + // An empty submission reports every missing field at once, not just the first. |
| 93 | + const all = (await (await post(env, {})).json()) as { errors: string[] }; |
| 94 | + expect(all.errors).toEqual(expect.arrayContaining(["id_required", "title_required", "body_required", "target_repo_required"])); |
| 95 | + }); |
| 96 | + |
| 97 | + it("rejects a schema-invalid or unparseable body with 400", async () => { |
| 98 | + const env = createTestEnv(); |
| 99 | + // These cannot reach the engine: the mirrored shape rejects them, exactly as the MCP tool's does. |
| 100 | + for (const body of [ |
| 101 | + { ...VALID, title: 7 }, |
| 102 | + { ...VALID, constraints: [7] }, |
| 103 | + { ...VALID, constraints: "no new deps" }, |
| 104 | + { ...VALID, decomposition: [{ key: "a", title: "Missing body" }] }, |
| 105 | + { ...VALID, decomposition: Array.from({ length: 51 }, (_, i) => ({ key: `k${i}`, title: "T", body: "B" })) }, |
| 106 | + ]) { |
| 107 | + const response = await post(env, body); |
| 108 | + expect(response.status, JSON.stringify(body)).toBe(400); |
| 109 | + await expect(response.json()).resolves.toMatchObject({ error: "invalid_intake_idea_request" }); |
| 110 | + } |
| 111 | + const malformed = await createApp().request(PATH, { method: "POST", headers: apiHeaders(createTestEnv()), body: "{not json" }, createTestEnv()); |
| 112 | + expect(malformed.status).toBe(400); |
| 113 | + }); |
| 114 | + |
| 115 | + it("never emits the maintainer-only gittensor:priority label, and leaks no wallet/hotkey terms", async () => { |
| 116 | + const env = createTestEnv(); |
| 117 | + const text = JSON.stringify(await (await post(env, { ...VALID, priority: "high" })).json()); |
| 118 | + expect(text).not.toContain("gittensor:priority"); |
| 119 | + expect(text).not.toMatch(/wallet|hotkey|coldkey|trust score/i); |
| 120 | + }); |
| 121 | +}); |
0 commit comments