classify.test.ts
This file
Path
packages/checks/test/classify.test.ts
230 lines · 7.9 KiB · TypeScript
Last changed by
Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 1–230why, one step back1import { describe, expect, it } from "@effect/vitest";2import { LaneName } from "@repo/protocol/ids";3import { Option } from "effect";45import { classifyRun, failureSignature } from "../src/classify.ts";6import type { RunExit } from "../src/classify.ts";7import bunAssertion from "./fixtures/runs/bun-assertion.json" with { type: "json" };8import bunLoadError from "./fixtures/runs/bun-load-error.json" with { type: "json" };9import bunNoTests from "./fixtures/runs/bun-no-tests.json" with { type: "json" };10import nodeSyntaxError from "./fixtures/runs/node-syntax-error.json" with { type: "json" };11import vitestAssertion from "./fixtures/runs/vitest-assertion.json" with { type: "json" };12import vitestLoadError from "./fixtures/runs/vitest-load-error.json" with { type: "json" };13import vitestNoTests from "./fixtures/runs/vitest-no-tests.json" with { type: "json" };1415const unit = LaneName.make("unit");1617// pytest is not installed here, so this run is written out by hand in18// pytest 8's plain-`assert` failure format, not recorded.19const pytestAssertion = (path: string, address: string, seconds: string) => ({20 code: 1,21 stderr: "",22 stdout: [23 "============================= test session starts ==============================",24 `rootdir: ${path}`,25 "collected 1 item",26 "",27 "test_week.py F [100%]",28 "",29 "=================================== FAILURES ===================================",30 "_______________________________ test_first_week ________________________________",31 "",32 " def test_first_week():",33 '> assert week_of("2026-W01") == 1',34 `E assert 0 == 1`,35 `E + where 0 = week_of(<datekit.Week object at ${address}>)`,36 "",37 `${path}/test_week.py:5: AssertionError`,38 "=========================== short test summary info ============================",39 "FAILED test_week.py::test_first_week - assert 0 == 1",40 `============================== 1 failed in ${seconds} ===============================`,41 ].join("\n"),42});4344// vitest before 4 reported a missing import this way; 5 says "Cannot find module".45const failedToLoadUrl: RunExit = {46 code: 1,47 stderr:48 "Error: Failed to load url ../src/week.ts (resolved id: ../src/week.ts) in /work/datekit/test/week.test.ts. Does the file exist?",49 stdout: " Test Files 1 failed (1)\n Tests no tests",50};5152const exit = (path: string, ms: string, address: string): RunExit => ({53 code: 1,54 stderr: `(fail) parses [${ms}]\nAssertionError: ${path}/week.ts took ${ms} at ${address}`,55 stdout: "",56});5758const withoutReport = ({ code, stderr, stdout }: RunExit): RunExit => ({59 code,60 stderr,61 stdout,62});6364describe(classifyRun, () => {65 describe("with vitest's JSON report", () => {66 it("is failed when a test fails on an assertion", () => {67 expect(classifyRun(unit, vitestAssertion)).toBe("failed");68 });6970 it("is loadError when the test file fails to load", () => {71 expect(classifyRun(unit, vitestLoadError)).toBe("loadError");72 });7374 it("is loadError when no test file is found", () => {75 expect(classifyRun(unit, vitestNoTests)).toBe("loadError");76 });7778 it("is passed on exit 0, even when the report found no tests", () => {79 expect(classifyRun(unit, { ...vitestNoTests, code: 0 })).toBe("passed");80 });81 });8283 describe("from vitest's output, with the report left out", () => {84 it("is failed on an assertion", () => {85 expect(classifyRun(unit, withoutReport(vitestAssertion))).toBe("failed");86 expect(failureSignature(withoutReport(vitestAssertion))).toStrictEqual(87 Option.some(88 "test/week.test.js > parseWeek > reads the first week of a year: AssertionError: expected { week: +0, year: 2026 } to strictly equal { week: 1, year: 2026 }"89 )90 );91 });9293 it("is loadError when the test file fails to load, or none is found", () => {94 expect(classifyRun(unit, withoutReport(vitestLoadError))).toBe(95 "loadError"96 );97 expect(classifyRun(unit, withoutReport(vitestNoTests))).toBe("loadError");98 });99 });100101 describe("from the output, when there is no report", () => {102 it("is failed on bun's expect(...).toBe failure", () => {103 expect(classifyRun(unit, bunAssertion)).toBe("failed");104 });105106 it("is failed on pytest's AssertionError", () => {107 expect(108 classifyRun(unit, pytestAssertion("/work/datekit", "0x7f3a", "0.02s"))109 ).toBe("failed");110 });111112 it("is loadError on a SyntaxError", () => {113 expect(classifyRun(unit, nodeSyntaxError)).toBe("loadError");114 });115116 it("is loadError on Cannot find module", () => {117 expect(classifyRun(unit, bunLoadError)).toBe("loadError");118 });119120 it("is loadError on Failed to load url", () => {121 expect(classifyRun(unit, failedToLoadUrl)).toBe("loadError");122 });123124 it("is loadError when bun finds no test files", () => {125 expect(classifyRun(unit, bunNoTests)).toBe("loadError");126 });127128 it("is failed when a nonzero exit says nothing more", () => {129 expect(130 classifyRun(LaneName.make("lint"), {131 code: 1,132 stderr: "",133 stdout: "Found 3 warnings and 2 errors.",134 })135 ).toBe("failed");136 });137 });138139 it("is passed on exit 0", () => {140 expect(141 classifyRun(unit, { code: 0, stderr: "", stdout: "1 pass\n0 fail" })142 ).toBe("passed");143 });144145 it("is blocked when the runner was killed at the time limit", () => {146 expect(147 classifyRun(unit, {148 code: 1,149 stderr: "",150 stdout: "AssertionError: expected 0 to be 1",151 timedOut: true,152 })153 ).toBe("blocked");154 });155156 it("is blocked when the lane's command could not start", () => {157 expect(158 classifyRun(unit, {159 code: 127,160 stderr: "sh: 1: vitest: not found",161 stdout: "",162 })163 ).toBe("blocked");164 });165});166167describe(failureSignature, () => {168 it("names the failing test and the first line of its assertion, from the report", () => {169 expect(failureSignature(vitestAssertion)).toStrictEqual(170 Option.some(171 "parseWeek reads the first week of a year: AssertionError: expected { week: +0, year: 2026 } to strictly equal { week: 1, year: 2026 }"172 )173 );174 });175176 it("names the failing test and its expectation, from bun's output", () => {177 expect(failureSignature(bunAssertion)).toStrictEqual(178 Option.some(179 "weekOf > reads the first week of a year: error: expect(received).toBe(expected) Expected: 1 Received: 0"180 )181 );182 });183184 it("names the failing test and its assertion, from pytest's output", () => {185 expect(186 failureSignature(pytestAssertion("/work/datekit", "0x7f3a", "0.02s"))187 ).toStrictEqual(188 Option.some("test_week.py::test_first_week: E assert 0 == 1")189 );190 });191192 it("is the same for two runs that differ only in paths, durations and addresses", () => {193 const first = failureSignature(194 pytestAssertion("/home/ci/a", "0x7f3a2c", "0.02s")195 );196197 const second = failureSignature(198 pytestAssertion("/tmp/run-9/b", "0x55d0e1", "1.31s")199 );200201 expect(first).toStrictEqual(202 Option.some("test_week.py::test_first_week: E assert 0 == 1")203 );204 expect(second).toStrictEqual(first);205 });206207 it("strips durations, absolute paths and addresses from the assertion line", () => {208 const first = failureSignature(209 exit("/home/ci/datekit", "12.5ms", "0xdeadbeef")210 );211212 expect(first).toStrictEqual(213 Option.some("parses: AssertionError: <path> took <duration> at <address>")214 );215 expect(216 failureSignature(exit("/work/datekit", "3ms", "0x7f00aa"))217 ).toStrictEqual(first);218 });219220 it("is none for a run that failed to load", () => {221 expect(failureSignature(vitestLoadError)).toStrictEqual(Option.none());222 expect(failureSignature(bunLoadError)).toStrictEqual(Option.none());223 });224225 it("is none for a run that passed", () => {226 expect(failureSignature({ code: 0, stderr: "", stdout: "" })).toStrictEqual(227 Option.none()228 );229 });230});