@mcpjam/vitest 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -90,6 +90,7 @@ function registerEvalSuite(name, suite, options, mode = "normal") {
|
|
|
90
90
|
try {
|
|
91
91
|
result = await selectedSuite.run(executor, {
|
|
92
92
|
...options.run,
|
|
93
|
+
surface: "vitest",
|
|
93
94
|
summary: "none"
|
|
94
95
|
});
|
|
95
96
|
} finally {
|
|
@@ -157,7 +158,7 @@ function registerTestEval(test, options, mode = "normal") {
|
|
|
157
158
|
beforeAll(async () => {
|
|
158
159
|
executor = await resolveExecutor(options);
|
|
159
160
|
try {
|
|
160
|
-
run = await test.run(executor, { ...options.run, summary: "none" });
|
|
161
|
+
run = await test.run(executor, { ...options.run, surface: "vitest", summary: "none" });
|
|
161
162
|
} finally {
|
|
162
163
|
const completed = run ?? test.getResults();
|
|
163
164
|
if (completed && options.summary !== "none") {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;;AAoDO,IAAM,uBAAA,GAA0B;AAGhC,IAAM,eAAA,GAAkB;AAkCxB,SAAS,aAAA,CACd,KAAA,EACA,OAAA,GAAiC,EAAC,EACnB;AACf,EAAA,MAAM,QAAQ,KAAA,CAAM,MAAA,EAAO,CAAE,GAAA,CAAI,CAAC,IAAA,KAAuB;AACvD,IAAA,MAAM,QAAA,GAAW,KAAK,OAAA,EAAQ;AAC9B,IAAA,MAAM,MAAA,GAAS,KAAK,SAAA,EAAU;AAC9B,IAAA,MAAM,aAAa,MAAA,CAAO,cAAA;AAI1B,IAAA,MAAM,SAAU,MAAA,CAA2B,EAAA;AAC3C,IAAA,MAAM,WAAW,MAAA,KAAW,MAAA,GAAY,EAAC,GAAI,EAAE,MAA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AA,UAAA,CAAW,sBAAA,CAAuB,GAAG,CAAA,EAAG,OAAA,CAAQ,IAAkB,CAAA;AAAA,QACpE,SAAS,KAAA,EAAO;AACd,UAAA,MAAM,IAAI,KAAA,CAAM,kBAAA,CAAmB,KAAK,CAAC,CAAA;AAAA,QAC3C;AAAA,MACF,CAAC,CAAA;AAAA,IACH;AAAA,EACF,CAAC,CAAA;AACH;AAEO,IAAM,WAAW,MAAA,CAAO,MAAA;AAAA,EAC7B,CAAC,IAAA,EAAgB,OAAA,KACf,gBAAA,CAAiB,MAAM,OAAO,CAAA;AAAA,EAChC;AAAA,IACE,MAAM,CAAC,IAAA,EAAgB,YACrB,gBAAA,CAAiB,IAAA,EAAM,SAAS,MAAM,CAAA;AAAA,IACxC,MAAM,CAAC,IAAA,EAAgB,YACrB,gBAAA,CAAiB,IAAA,EAAM,SAAS,MAAM;AAAA;AAE5C","file":"index.js","sourcesContent":["/**\n * Run MCPJam eval suites inside vitest.\n *\n * --- A wrapper, not a reporter ---\n *\n * The obvious alternative was a custom vitest Reporter. This is deliberately\n * not that. A reporter observes tests; it cannot decide what a test IS. Evals\n * need the opposite: one `it` per eval case so failures land on named tests in\n * every vitest UI, watch mode and CI annotation that already exists, with no\n * bespoke output for anyone to learn. A reporter would also have no seat for\n * the gate, which is a verdict over the whole run rather than any one case.\n *\n * --- One run, many tests ---\n *\n * `suite.run()` is called ONCE, in `beforeAll`. That is not an optimization:\n * the suite uploads a single hosted run, computes one aggregate evaluation\n * config hash, and executes its cases sequentially. Calling it per `it` would\n * produce N hosted runs, N aggregate hashes, and a concurrency pattern the\n * suite explicitly avoids. The `it`s then read that one result.\n *\n * So an eval case's `it` is an ASSERTION over an already-computed result, not\n * the thing that runs it. Vitest timeouts on those tests are therefore\n * irrelevant; the timeout that matters is the hook's, which is why it defaults\n * to five minutes and is configurable.\n *\n * --- The gate is its own test ---\n *\n * The per-case tests answer \"did this case pass\". The gate answers \"does this\n * run satisfy the policy\", which is a different question with a different\n * failure mode — a run where every case passed can still breach a latency or\n * score-integrity gate. Giving it a named `it` means CI shows WHICH question\n * failed instead of a bare non-zero exit.\n */\n\nimport { afterAll, beforeAll, describe, it } from \"vitest\";\nimport {\n assertGate,\n formatGateReport,\n formatRunSummaryTable,\n gateInputFromRunResult,\n gateInputFromSuiteResult,\n type EvalRunResult,\n type EvalSuite,\n type EvalSuiteResult,\n type EvalTest,\n type EvalTestRunOptions,\n type GatePolicy,\n type GateReport,\n type HostExecutor,\n} from \"@mcpjam/sdk\";\n\n/** Five minutes. An eval suite is not a unit test. */\nexport const DEFAULT_HOOK_TIMEOUT_MS = 300_000;\n\n/** The vitest test title used for the policy gate. */\nexport const GATE_TEST_TITLE = \"eval gate\";\n\nexport type EvalCasePlan = {\n /** The vitest test title. */\n title: string;\n /** The suite key this case's result is recorded under. */\n testName: string;\n /** The hosted case id, when this case came from a hosted corpus. */\n scenarioId?: string;\n /**\n * The case's DECLARED id (`EvalTestConfig.id`).\n *\n * Surfaced so a CI reporter can key a case by its identity rather than by a\n * title that gets renamed. Deliberately NOT used in the vitest title: the\n * `[scenarioId]` suffix is the hosted-dashboard grep handle and still rides\n * `externalCaseId`, so changing what the suffix carries would break the greps\n * that exist.\n */\n caseId?: string;\n};\n\nexport type EvalSuitePlan = {\n cases: EvalCasePlan[];\n /** Present only when a gate policy was supplied. */\n gateTestTitle?: string;\n};\n\n/**\n * Decide the vitest titles for a suite, without registering anything.\n *\n * Pure and exported so the naming rules are testable on their own. A hosted\n * case's id is appended so a test title is greppable back to the dashboard row\n * it came from — explicitly, never inferred from the test name.\n */\nexport function planEvalSuite(\n suite: EvalSuite,\n options: { gate?: GatePolicy } = {},\n): EvalSuitePlan {\n const cases = suite.getAll().map((test): EvalCasePlan => {\n const testName = test.getName();\n const config = test.getConfig();\n const scenarioId = config.externalCaseId;\n // Read defensively: a user can end up with `@mcpjam/vitest` beside an older\n // `@mcpjam/sdk` copy, where `id` does not exist yet. An absent `caseId` is\n // a missing convenience; a thrown TypeError would be a broken test run.\n const caseId = (config as { id?: string }).id;\n const declared = caseId === undefined ? {} : { caseId };\n if (scenarioId === undefined) {\n return { title: testName, testName, ...declared };\n }\n // A corpus already suffixes `[id]` onto cases whose titles collide, so\n // appending unconditionally would render `Title [id] [id]`.\n const suffix = ` [${scenarioId}]`;\n return {\n title: testName.endsWith(suffix) ? testName : `${testName}${suffix}`,\n testName,\n scenarioId,\n ...declared,\n };\n });\n\n return {\n cases,\n ...(options.gate ? { gateTestTitle: GATE_TEST_TITLE } : {}),\n };\n}\n\n/**\n * Turn one case's recorded result into a pass or a thrown failure.\n *\n * Exported so the RED path is testable inside an ordinary `it` — a generated\n * failing test cannot be asserted on from within the same run without failing\n * the run itself.\n */\nexport function runAndAssertCase(\n run: EvalRunResult | undefined,\n testName: string,\n failureReport?: () => string,\n): void {\n if (!run) {\n // Reachable if a case was added to the suite after the plan was taken.\n throw new Error(\n `No result was recorded for eval case \"${testName}\". The suite ran, but ` +\n `this case was not part of it.`,\n );\n }\n if (run.iterations === 0)\n throw new Error(`No iterations executed for eval case \"${testName}\".`);\n if (run.failures === 0) return;\n\n let detail = \"\";\n try {\n detail = failureReport?.() ?? \"\";\n } catch {\n // A missing detail report must not replace the real failure with a\n // secondary one about formatting it.\n detail = \"\";\n }\n\n const summary =\n `${testName}: ${run.failures} of ${run.iterations} ` +\n `${run.iterations === 1 ? \"iteration\" : \"iterations\"} failed.`;\n throw new Error(detail ? `${summary}\\n\\n${detail}` : summary);\n}\n\n/**\n * Render a gate failure for a test message.\n *\n * Duck-typed as well as `instanceof`-checked. A published wrapper can end up\n * beside a second copy of `@mcpjam/sdk` — a mismatched transitive range, a\n * pnpm layout — and `instanceof` silently fails across copies. Falling back to\n * the message would drop the per-gate table, which is the whole reason the\n * error carries a report.\n */\nexport function gateFailureMessage(error: unknown): string {\n const report = (error as { report?: GateReport } | null)?.report;\n if (report && typeof report === \"object\" && \"outcome\" in report) {\n return formatGateReport(report);\n }\n return error instanceof Error ? error.message : String(error);\n}\n\nexport type EvalSuiteVitestOptions = {\n /** A ready executor. Mutually exclusive with `factory`. */\n executor?: HostExecutor;\n /** Built inside `beforeAll` — for an executor that must connect first. */\n factory?: () => HostExecutor | Promise<HostExecutor>;\n run: Omit<EvalTestRunOptions, \"iterations\"> & { iterations?: number };\n /** Selected case identities. Selection retains the source coverage manifest. */\n only?: readonly string[];\n /** Skips win over only; no skipped case executes. */\n skip?: readonly string[];\n /** Explicit cleanup, run even when execution or reporting fails. */\n dispose?: (executor: HostExecutor) => void | Promise<void>;\n /** Defaults to table. Formatting errors never replace evaluation results. */\n summary?: \"none\" | \"table\";\n /** Omit to register no gate test. */\n gate?: GatePolicy;\n hookTimeoutMs?: number;\n};\n\nasync function resolveExecutor(\n options: EvalSuiteVitestOptions,\n): Promise<HostExecutor> {\n if (options.executor && options.factory) {\n throw new Error(\n \"Pass either `executor` or `factory` to describeEvalSuite, not both.\",\n );\n }\n if (options.executor) return options.executor;\n if (options.factory) return await options.factory();\n throw new Error(\n \"describeEvalSuite needs an `executor` or a `factory` to build one.\",\n );\n}\n\n/**\n * Register a vitest `describe` for an eval suite: one test per case, plus the\n * gate test when a policy is given.\n */\nfunction registerEvalSuite(\n name: string,\n suite: EvalSuite,\n options: EvalSuiteVitestOptions,\n mode: \"normal\" | \"skip\" | \"only\" = \"normal\",\n): void {\n const plan = planEvalSuite(suite, options);\n const available = new Set(suite.getAll().map((test) => test.getId()));\n for (const id of [...(options.only ?? []), ...(options.skip ?? [])]) {\n if (!available.has(id)) throw new Error(`Unknown selected case ID: ${id}`);\n }\n const skipped = new Set(options.skip ?? []);\n const selectedIds = [...available].filter(\n (id) => !skipped.has(id) && (!options.only || options.only.includes(id)),\n );\n const selectedSuite =\n options.only || options.skip ? suite.subset(selectedIds) : suite;\n // Captured BEFORE the run so a case's detailed report is reachable by name\n // without re-walking the suite inside every test.\n const testsByName = new Map<string, EvalTest>(\n selectedSuite.getAll().map((test) => [test.getName(), test]),\n );\n\n const register =\n mode === \"skip\"\n ? describe.skip\n : mode === \"only\" || options.only\n ? describe.only\n : describe;\n register(name, () => {\n let executor: HostExecutor | undefined;\n let result: EvalSuiteResult | undefined;\n\n beforeAll(async () => {\n if (selectedIds.length === 0) return;\n executor = await resolveExecutor(options);\n try {\n result = await selectedSuite.run(executor, {\n ...options.run,\n summary: \"none\",\n });\n } finally {\n const completed = result ?? selectedSuite.getResults();\n if (completed && options.summary !== \"none\") {\n try {\n console.log(\n formatRunSummaryTable(\n completed,\n selectedSuite.getReportingReceipt(),\n ),\n );\n } catch {\n /* formatting is observational */\n }\n }\n }\n }, options.hookTimeoutMs ?? DEFAULT_HOOK_TIMEOUT_MS);\n\n afterAll(async () => {\n if (executor) await options.dispose?.(executor);\n });\n for (const entry of plan.cases) {\n const registerCase =\n entry.caseId && !selectedIds.includes(entry.caseId) ? it.skip : it;\n registerCase(entry.title, () => {\n const test = testsByName.get(entry.testName);\n runAndAssertCase(\n result?.tests.get(entry.testName),\n entry.testName,\n () => (test ? test.getFailureReport() : \"\"),\n );\n });\n }\n\n if (plan.gateTestTitle) {\n it(plan.gateTestTitle, () => {\n if (!result) {\n throw new Error(\"The eval suite did not run, so it cannot be gated.\");\n }\n try {\n // `assertGate` throws on `incomplete` too: a gate that could not be\n // decided has not been satisfied.\n //\n // NO WAIVER PATH HERE, and its absence is a decision rather than an\n // omission. `GateReport` gained a `waived` outcome for the HOSTED\n // gate, where a waiver is an audited platform record with an\n // authorized granter, a reason and an expiry behind it. This gate\n // runs against a local, code-first suite result: there is no such\n // record to consult, and the only way to express \"waive this\" would\n // be a flag in the same file as the assertion — a gate that turns\n // itself off, which is not a waiver at all. A caller who wants a\n // policy relaxed here edits the policy, in the open, in git.\n //\n // `formatGateReport` below renders a `waived` report correctly if\n // one ever does reach it, so the widened union is safe to pass\n // through untouched.\n assertGate(\n gateInputFromSuiteResult(result),\n options.gate as GatePolicy,\n );\n } catch (error) {\n throw new Error(gateFailureMessage(error));\n }\n });\n }\n });\n}\n\nexport const describeEvalSuite = Object.assign(\n (name: string, suite: EvalSuite, options: EvalSuiteVitestOptions) =>\n registerEvalSuite(name, suite, options),\n {\n skip: (name: string, suite: EvalSuite, options: EvalSuiteVitestOptions) =>\n registerEvalSuite(name, suite, options, \"skip\"),\n only: (name: string, suite: EvalSuite, options: EvalSuiteVitestOptions) =>\n registerEvalSuite(name, suite, options, \"only\"),\n },\n);\n\nexport type EvalTestVitestOptions = Omit<\n EvalSuiteVitestOptions,\n \"run\" | \"only\" | \"skip\"\n> & { run: EvalTestRunOptions };\n\n/**\n * The single-test seat, for a file that owns one eval and wants no suite.\n *\n * Not implemented by wrapping the test in a throwaway `EvalSuite`: a suite\n * aggregates and uploads as a unit, so a synthetic one would change what is\n * reported for a case that is not part of any suite.\n *\n * `gate` is honoured here exactly as in `describeEvalSuite` — via\n * `gateInputFromRunResult` rather than the suite variant. Accepting the option\n * and ignoring it would be the worst outcome available: a policy that reports\n * green because nothing evaluated it.\n */\nfunction registerTestEval(\n test: EvalTest,\n options: EvalTestVitestOptions,\n mode: \"normal\" | \"skip\" | \"only\" = \"normal\",\n): void {\n const scenarioId = test.getConfig().externalCaseId;\n const name = test.getName();\n const suffix = scenarioId === undefined ? \"\" : ` [${scenarioId}]`;\n const title = name.endsWith(suffix) ? name : `${name}${suffix}`;\n\n const register =\n mode === \"skip\"\n ? describe.skip\n : mode === \"only\"\n ? describe.only\n : describe;\n register(title, () => {\n let executor: HostExecutor | undefined;\n let run: EvalRunResult | undefined;\n\n beforeAll(async () => {\n executor = await resolveExecutor(options);\n try {\n run = await test.run(executor, { ...options.run, summary: \"none\" });\n } finally {\n const completed = run ?? test.getResults();\n if (completed && options.summary !== \"none\") {\n try {\n console.log(\n formatRunSummaryTable(completed, test.getReportingReceipt()),\n );\n } catch {\n /* formatting is observational */\n }\n }\n }\n }, options.hookTimeoutMs ?? DEFAULT_HOOK_TIMEOUT_MS);\n\n afterAll(async () => {\n if (executor) await options.dispose?.(executor);\n });\n it(\"passes\", () => {\n runAndAssertCase(run, name, () => test.getFailureReport());\n });\n\n if (options.gate) {\n it(GATE_TEST_TITLE, () => {\n if (!run) {\n throw new Error(\"The eval did not run, so it cannot be gated.\");\n }\n try {\n assertGate(gateInputFromRunResult(run), options.gate as GatePolicy);\n } catch (error) {\n throw new Error(gateFailureMessage(error));\n }\n });\n }\n });\n}\n\nexport const testEval = Object.assign(\n (test: EvalTest, options: EvalTestVitestOptions) =>\n registerTestEval(test, options),\n {\n skip: (test: EvalTest, options: EvalTestVitestOptions) =>\n registerTestEval(test, options, \"skip\"),\n only: (test: EvalTest, options: EvalTestVitestOptions) =>\n registerTestEval(test, options, \"only\"),\n },\n);\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;;AAoDO,IAAM,uBAAA,GAA0B;AAGhC,IAAM,eAAA,GAAkB;AAkCxB,SAAS,aAAA,CACd,KAAA,EACA,OAAA,GAAiC,EAAC,EACnB;AACf,EAAA,MAAM,QAAQ,KAAA,CAAM,MAAA,EAAO,CAAE,GAAA,CAAI,CAAC,IAAA,KAAuB;AACvD,IAAA,MAAM,QAAA,GAAW,KAAK,OAAA,EAAQ;AAC9B,IAAA,MAAM,MAAA,GAAS,KAAK,SAAA,EAAU;AAC9B,IAAA,MAAM,aAAa,MAAA,CAAO,cAAA;AAI1B,IAAA,MAAM,SAAU,MAAA,CAA2B,EAAA;AAC3C,IAAA,MAAM,WAAW,MAAA,KAAW,MAAA,GAAY,EAAC,GAAI,EAAE,MAAA,EAAO;AACtD,IAAA,IAAI,eAAe,MAAA,EAAW;AAC5B,MAAA,OAAO,EAAE,KAAA,EAAO,QAAA,EAAU,QAAA,EAAU,GAAG,QAAA,EAAS;AAAA,IAClD;AAGA,IAAA,MAAM,MAAA,GAAS,KAAK,UAAU,CAAA,CAAA,CAAA;AAC9B,IAAA,OAAO;AAAA,MACL,KAAA,EAAO,SAAS,QAAA,CAAS,MAAM,IAAI,QAAA,GAAW,CAAA,EAAG,QAAQ,CAAA,EAA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M,WAAW,MAAA,CAAO,MAAA;AAAA,EAC7B,CAAC,IAAA,EAAgB,OAAA,KACf,gBAAA,CAAiB,MAAM,OAAO,CAAA;AAAA,EAChC;AAAA,IACE,MAAM,CAAC,IAAA,EAAgB,YACrB,gBAAA,CAAiB,IAAA,EAAM,SAAS,MAAM,CAAA;AAAA,IACxC,MAAM,CAAC,IAAA,EAAgB,YACrB,gBAAA,CAAiB,IAAA,EAAM,SAAS,MAAM;AAAA;AAE5C","file":"index.js","sourcesContent":["/**\n * Run MCPJam eval suites inside vitest.\n *\n * --- A wrapper, not a reporter ---\n *\n * The obvious alternative was a custom vitest Reporter. This is deliberately\n * not that. A reporter observes tests; it cannot decide what a test IS. Evals\n * need the opposite: one `it` per eval case so failures land on named tests in\n * every vitest UI, watch mode and CI annotation that already exists, with no\n * bespoke output for anyone to learn. A reporter would also have no seat for\n * the gate, which is a verdict over the whole run rather than any one case.\n *\n * --- One run, many tests ---\n *\n * `suite.run()` is called ONCE, in `beforeAll`. That is not an optimization:\n * the suite uploads a single hosted run, computes one aggregate evaluation\n * config hash, and executes its cases sequentially. Calling it per `it` would\n * produce N hosted runs, N aggregate hashes, and a concurrency pattern the\n * suite explicitly avoids. The `it`s then read that one result.\n *\n * So an eval case's `it` is an ASSERTION over an already-computed result, not\n * the thing that runs it. Vitest timeouts on those tests are therefore\n * irrelevant; the timeout that matters is the hook's, which is why it defaults\n * to five minutes and is configurable.\n *\n * --- The gate is its own test ---\n *\n * The per-case tests answer \"did this case pass\". The gate answers \"does this\n * run satisfy the policy\", which is a different question with a different\n * failure mode — a run where every case passed can still breach a latency or\n * score-integrity gate. Giving it a named `it` means CI shows WHICH question\n * failed instead of a bare non-zero exit.\n */\n\nimport { afterAll, beforeAll, describe, it } from \"vitest\";\nimport {\n assertGate,\n formatGateReport,\n formatRunSummaryTable,\n gateInputFromRunResult,\n gateInputFromSuiteResult,\n type EvalRunResult,\n type EvalSuite,\n type EvalSuiteResult,\n type EvalTest,\n type EvalTestRunOptions,\n type GatePolicy,\n type GateReport,\n type HostExecutor,\n} from \"@mcpjam/sdk\";\n\n/** Five minutes. An eval suite is not a unit test. */\nexport const DEFAULT_HOOK_TIMEOUT_MS = 300_000;\n\n/** The vitest test title used for the policy gate. */\nexport const GATE_TEST_TITLE = \"eval gate\";\n\nexport type EvalCasePlan = {\n /** The vitest test title. */\n title: string;\n /** The suite key this case's result is recorded under. */\n testName: string;\n /** The hosted case id, when this case came from a hosted corpus. */\n scenarioId?: string;\n /**\n * The case's DECLARED id (`EvalTestConfig.id`).\n *\n * Surfaced so a CI reporter can key a case by its identity rather than by a\n * title that gets renamed. Deliberately NOT used in the vitest title: the\n * `[scenarioId]` suffix is the hosted-dashboard grep handle and still rides\n * `externalCaseId`, so changing what the suffix carries would break the greps\n * that exist.\n */\n caseId?: string;\n};\n\nexport type EvalSuitePlan = {\n cases: EvalCasePlan[];\n /** Present only when a gate policy was supplied. */\n gateTestTitle?: string;\n};\n\n/**\n * Decide the vitest titles for a suite, without registering anything.\n *\n * Pure and exported so the naming rules are testable on their own. A hosted\n * case's id is appended so a test title is greppable back to the dashboard row\n * it came from — explicitly, never inferred from the test name.\n */\nexport function planEvalSuite(\n suite: EvalSuite,\n options: { gate?: GatePolicy } = {},\n): EvalSuitePlan {\n const cases = suite.getAll().map((test): EvalCasePlan => {\n const testName = test.getName();\n const config = test.getConfig();\n const scenarioId = config.externalCaseId;\n // Read defensively: a user can end up with `@mcpjam/vitest` beside an older\n // `@mcpjam/sdk` copy, where `id` does not exist yet. An absent `caseId` is\n // a missing convenience; a thrown TypeError would be a broken test run.\n const caseId = (config as { id?: string }).id;\n const declared = caseId === undefined ? {} : { caseId };\n if (scenarioId === undefined) {\n return { title: testName, testName, ...declared };\n }\n // A corpus already suffixes `[id]` onto cases whose titles collide, so\n // appending unconditionally would render `Title [id] [id]`.\n const suffix = ` [${scenarioId}]`;\n return {\n title: testName.endsWith(suffix) ? testName : `${testName}${suffix}`,\n testName,\n scenarioId,\n ...declared,\n };\n });\n\n return {\n cases,\n ...(options.gate ? { gateTestTitle: GATE_TEST_TITLE } : {}),\n };\n}\n\n/**\n * Turn one case's recorded result into a pass or a thrown failure.\n *\n * Exported so the RED path is testable inside an ordinary `it` — a generated\n * failing test cannot be asserted on from within the same run without failing\n * the run itself.\n */\nexport function runAndAssertCase(\n run: EvalRunResult | undefined,\n testName: string,\n failureReport?: () => string,\n): void {\n if (!run) {\n // Reachable if a case was added to the suite after the plan was taken.\n throw new Error(\n `No result was recorded for eval case \"${testName}\". The suite ran, but ` +\n `this case was not part of it.`,\n );\n }\n if (run.iterations === 0)\n throw new Error(`No iterations executed for eval case \"${testName}\".`);\n if (run.failures === 0) return;\n\n let detail = \"\";\n try {\n detail = failureReport?.() ?? \"\";\n } catch {\n // A missing detail report must not replace the real failure with a\n // secondary one about formatting it.\n detail = \"\";\n }\n\n const summary =\n `${testName}: ${run.failures} of ${run.iterations} ` +\n `${run.iterations === 1 ? \"iteration\" : \"iterations\"} failed.`;\n throw new Error(detail ? `${summary}\\n\\n${detail}` : summary);\n}\n\n/**\n * Render a gate failure for a test message.\n *\n * Duck-typed as well as `instanceof`-checked. A published wrapper can end up\n * beside a second copy of `@mcpjam/sdk` — a mismatched transitive range, a\n * pnpm layout — and `instanceof` silently fails across copies. Falling back to\n * the message would drop the per-gate table, which is the whole reason the\n * error carries a report.\n */\nexport function gateFailureMessage(error: unknown): string {\n const report = (error as { report?: GateReport } | null)?.report;\n if (report && typeof report === \"object\" && \"outcome\" in report) {\n return formatGateReport(report);\n }\n return error instanceof Error ? error.message : String(error);\n}\n\nexport type EvalSuiteVitestOptions = {\n /** A ready executor. Mutually exclusive with `factory`. */\n executor?: HostExecutor;\n /** Built inside `beforeAll` — for an executor that must connect first. */\n factory?: () => HostExecutor | Promise<HostExecutor>;\n run: Omit<EvalTestRunOptions, \"iterations\"> & { iterations?: number };\n /** Selected case identities. Selection retains the source coverage manifest. */\n only?: readonly string[];\n /** Skips win over only; no skipped case executes. */\n skip?: readonly string[];\n /** Explicit cleanup, run even when execution or reporting fails. */\n dispose?: (executor: HostExecutor) => void | Promise<void>;\n /** Defaults to table. Formatting errors never replace evaluation results. */\n summary?: \"none\" | \"table\";\n /** Omit to register no gate test. */\n gate?: GatePolicy;\n hookTimeoutMs?: number;\n};\n\nasync function resolveExecutor(\n options: EvalSuiteVitestOptions,\n): Promise<HostExecutor> {\n if (options.executor && options.factory) {\n throw new Error(\n \"Pass either `executor` or `factory` to describeEvalSuite, not both.\",\n );\n }\n if (options.executor) return options.executor;\n if (options.factory) return await options.factory();\n throw new Error(\n \"describeEvalSuite needs an `executor` or a `factory` to build one.\",\n );\n}\n\n/**\n * Register a vitest `describe` for an eval suite: one test per case, plus the\n * gate test when a policy is given.\n */\nfunction registerEvalSuite(\n name: string,\n suite: EvalSuite,\n options: EvalSuiteVitestOptions,\n mode: \"normal\" | \"skip\" | \"only\" = \"normal\",\n): void {\n const plan = planEvalSuite(suite, options);\n const available = new Set(suite.getAll().map((test) => test.getId()));\n for (const id of [...(options.only ?? []), ...(options.skip ?? [])]) {\n if (!available.has(id)) throw new Error(`Unknown selected case ID: ${id}`);\n }\n const skipped = new Set(options.skip ?? []);\n const selectedIds = [...available].filter(\n (id) => !skipped.has(id) && (!options.only || options.only.includes(id)),\n );\n const selectedSuite =\n options.only || options.skip ? suite.subset(selectedIds) : suite;\n // Captured BEFORE the run so a case's detailed report is reachable by name\n // without re-walking the suite inside every test.\n const testsByName = new Map<string, EvalTest>(\n selectedSuite.getAll().map((test) => [test.getName(), test]),\n );\n\n const register =\n mode === \"skip\"\n ? describe.skip\n : mode === \"only\" || options.only\n ? describe.only\n : describe;\n register(name, () => {\n let executor: HostExecutor | undefined;\n let result: EvalSuiteResult | undefined;\n\n beforeAll(async () => {\n if (selectedIds.length === 0) return;\n executor = await resolveExecutor(options);\n try {\n result = await selectedSuite.run(executor, {\n ...options.run,\n surface: \"vitest\",\n summary: \"none\",\n });\n } finally {\n const completed = result ?? selectedSuite.getResults();\n if (completed && options.summary !== \"none\") {\n try {\n console.log(\n formatRunSummaryTable(\n completed,\n selectedSuite.getReportingReceipt(),\n ),\n );\n } catch {\n /* formatting is observational */\n }\n }\n }\n }, options.hookTimeoutMs ?? DEFAULT_HOOK_TIMEOUT_MS);\n\n afterAll(async () => {\n if (executor) await options.dispose?.(executor);\n });\n for (const entry of plan.cases) {\n const registerCase =\n entry.caseId && !selectedIds.includes(entry.caseId) ? it.skip : it;\n registerCase(entry.title, () => {\n const test = testsByName.get(entry.testName);\n runAndAssertCase(\n result?.tests.get(entry.testName),\n entry.testName,\n () => (test ? test.getFailureReport() : \"\"),\n );\n });\n }\n\n if (plan.gateTestTitle) {\n it(plan.gateTestTitle, () => {\n if (!result) {\n throw new Error(\"The eval suite did not run, so it cannot be gated.\");\n }\n try {\n // `assertGate` throws on `incomplete` too: a gate that could not be\n // decided has not been satisfied.\n //\n // NO WAIVER PATH HERE, and its absence is a decision rather than an\n // omission. `GateReport` gained a `waived` outcome for the HOSTED\n // gate, where a waiver is an audited platform record with an\n // authorized granter, a reason and an expiry behind it. This gate\n // runs against a local, code-first suite result: there is no such\n // record to consult, and the only way to express \"waive this\" would\n // be a flag in the same file as the assertion — a gate that turns\n // itself off, which is not a waiver at all. A caller who wants a\n // policy relaxed here edits the policy, in the open, in git.\n //\n // `formatGateReport` below renders a `waived` report correctly if\n // one ever does reach it, so the widened union is safe to pass\n // through untouched.\n assertGate(\n gateInputFromSuiteResult(result),\n options.gate as GatePolicy,\n );\n } catch (error) {\n throw new Error(gateFailureMessage(error));\n }\n });\n }\n });\n}\n\nexport const describeEvalSuite = Object.assign(\n (name: string, suite: EvalSuite, options: EvalSuiteVitestOptions) =>\n registerEvalSuite(name, suite, options),\n {\n skip: (name: string, suite: EvalSuite, options: EvalSuiteVitestOptions) =>\n registerEvalSuite(name, suite, options, \"skip\"),\n only: (name: string, suite: EvalSuite, options: EvalSuiteVitestOptions) =>\n registerEvalSuite(name, suite, options, \"only\"),\n },\n);\n\nexport type EvalTestVitestOptions = Omit<\n EvalSuiteVitestOptions,\n \"run\" | \"only\" | \"skip\"\n> & { run: EvalTestRunOptions };\n\n/**\n * The single-test seat, for a file that owns one eval and wants no suite.\n *\n * Not implemented by wrapping the test in a throwaway `EvalSuite`: a suite\n * aggregates and uploads as a unit, so a synthetic one would change what is\n * reported for a case that is not part of any suite.\n *\n * `gate` is honoured here exactly as in `describeEvalSuite` — via\n * `gateInputFromRunResult` rather than the suite variant. Accepting the option\n * and ignoring it would be the worst outcome available: a policy that reports\n * green because nothing evaluated it.\n */\nfunction registerTestEval(\n test: EvalTest,\n options: EvalTestVitestOptions,\n mode: \"normal\" | \"skip\" | \"only\" = \"normal\",\n): void {\n const scenarioId = test.getConfig().externalCaseId;\n const name = test.getName();\n const suffix = scenarioId === undefined ? \"\" : ` [${scenarioId}]`;\n const title = name.endsWith(suffix) ? name : `${name}${suffix}`;\n\n const register =\n mode === \"skip\"\n ? describe.skip\n : mode === \"only\"\n ? describe.only\n : describe;\n register(title, () => {\n let executor: HostExecutor | undefined;\n let run: EvalRunResult | undefined;\n\n beforeAll(async () => {\n executor = await resolveExecutor(options);\n try {\n run = await test.run(executor, { ...options.run, surface: \"vitest\", summary: \"none\" });\n } finally {\n const completed = run ?? test.getResults();\n if (completed && options.summary !== \"none\") {\n try {\n console.log(\n formatRunSummaryTable(completed, test.getReportingReceipt()),\n );\n } catch {\n /* formatting is observational */\n }\n }\n }\n }, options.hookTimeoutMs ?? DEFAULT_HOOK_TIMEOUT_MS);\n\n afterAll(async () => {\n if (executor) await options.dispose?.(executor);\n });\n it(\"passes\", () => {\n runAndAssertCase(run, name, () => test.getFailureReport());\n });\n\n if (options.gate) {\n it(GATE_TEST_TITLE, () => {\n if (!run) {\n throw new Error(\"The eval did not run, so it cannot be gated.\");\n }\n try {\n assertGate(gateInputFromRunResult(run), options.gate as GatePolicy);\n } catch (error) {\n throw new Error(gateFailureMessage(error));\n }\n });\n }\n });\n}\n\nexport const testEval = Object.assign(\n (test: EvalTest, options: EvalTestVitestOptions) =>\n registerTestEval(test, options),\n {\n skip: (test: EvalTest, options: EvalTestVitestOptions) =>\n registerTestEval(test, options, \"skip\"),\n only: (test: EvalTest, options: EvalTestVitestOptions) =>\n registerTestEval(test, options, \"only\"),\n },\n);\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mcpjam/vitest",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Run MCPJam eval suites inside vitest: one test per eval case, plus a gate test that fails the build on a policy breach.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"vitest": ">=3.2.0 <5"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@mcpjam/sdk": "^
|
|
32
|
+
"@mcpjam/sdk": "^10.0.0"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@types/node": "^26.6.3",
|