listTests

tcmts.studio.listTests · #za4e5izl · term · runs on the server

uses node:util

Every test definition the codebase names (as tcm test finds them: named test…, or under tests), each with its last outcome as kept (by hash, as tcm test keeps it: a pass, a failure, or none, not run as it is now); a suite's test (suite: one running a package's command, as its manifest declares) has its passing run, for its code as it is, if one's kept.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { suitesIn, unpackRuns } from "#codebase/tcmts/cli/suites";
import { isTestPath } from "#codebase/tcmts/cli/testing";
import { keysForResults } from "#codebase/tcmts/cli/vitest";
import { stripVTControlCharacters } from "node:util";

export function listTests(
  cb: Codebase,
): { path: string; hash: string; ok?: boolean; message?: string; ms?: number; suite?: boolean }[] {
  const tests = cb.store.names().filter((n) => n.kind === "term" && isTestPath(n.path));
  // a suite's tests (a package's command, run as a program): their passing runs kept apart, by test
  const suites = new Set(suitesIn(cb, "").map((s) => s.path));
  const runs = unpackRuns(cb.store.getSetting("suites"));
  const keys = keysForResults(
    cb,
    tests.map((t) => t.hash),
  );
  return tests
    .map((t) => {
      if (suites.has(t.path)) {
        const run = (runs[t.path] ?? []).find((x) => x.of === t.hash && x.result.ok);
        return {
          path: t.path,
          hash: t.hash,
          suite: true,
          ...(run ? { ok: true, ms: run.result.ms } : {}),
        };
      }
      const r = cb.store.testResult(t.hash, keys.get(t.hash)!);
      return {
        path: t.path,
        hash: t.hash,
        ...(r
          ? {
              ok: r.ok,
              ms: r.ms,
              ...(r.ok ? {} : { message: stripVTControlCharacters(r.message) }),
            }
          : {}),
      };
    })
    .sort((a, b) => a.path.localeCompare(b.path));
}

uses

used by