packagesKey

tcmts.cli.vitest.packagesKey · #tqkdldn5 · term · runs on the server

uses node:crypto

The npm packages a run's browser tests reach (through all the code they use), and Vitest's: what names the bundle Vite makes of them for the browser (vitestChild), kept from one run to the next, so an edit that imports nothing new reuses it.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import type { Request } from "#codebase/tcmts/cli/vitest";
import type { DefRecord } from "#codebase/tcmts/core/types";
import { createHash } from "node:crypto";

export function packagesKey(cb: Codebase, request: Request): string {
  const unsaved = new Map(request.unsaved.map((r) => [r.hash, r]));
  const get = (h: string): DefRecord | undefined => unsaved.get(h) ?? cb.store.getDef(h);
  const seen = new Set<string>();
  const modules = new Set<string>();
  const stack = request.browserTests.map((t) => t.hash);
  while (stack.length) {
    const h = stack.pop()!;
    if (seen.has(h)) continue;
    seen.add(h);
    const rec = get(h);
    if (!rec) continue;
    for (const s of rec.spans) {
      if (s.ext) modules.add(s.ext.module);
      if (s.hash) stack.push(s.hash);
    }
  }
  return createHash("sha256")
    .update(`${request.vitest}\n${[...modules].sort().join("\n")}`)
    .digest("hex")
    .slice(0, 16);
}

uses

used by