fetchResults

tcmts.cli.sync.fetchResults · #de7vstpx · term · runs on the server

uses process

Fetch the results of a branch's tests this codebase has none of, from the remote: the ones it could use, run on this runtime (and, for a test whose result depends on it, this platform). How many were received. A codebase that hasn't run tests takes the remote's version of the test framework with them.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { toolRange } from "#codebase/tcmts/cli/lint";
import { chunks, testsIn } from "#codebase/tcmts/cli/sync";
import type { Remote } from "#codebase/tcmts/cli/sync";
import type { NameRow } from "#codebase/tcmts/store";

export async function fetchResults(
  cb: Codebase,
  remote: Remote,
  names: NameRow[],
): Promise<number> {
  const platform = `${process.platform}-${process.arch}`;
  const usable = (key: string) => {
    const [node, ...rest] = key.split(" ");
    const on = rest.filter((p) => !p.includes("@"));
    return node === process.version && on.every((p) => p === platform);
  };
  const tests = testsIn(names).filter((h) => cb.store.hasDef(h));
  try {
    const want: [string, string][] = [];
    for (const c of chunks(tests))
      for (const [h, k] of (await remote.resultKeys(c)) ?? [])
        if (usable(k) && !cb.store.testResult(h, k)) want.push([h, k]);
    // a codebase that hasn't run tests yet runs them with the remote's version of the test framework, which they
    // ran with there (its results are kept under it)
    if (want.some(([, k]) => k.includes(" vitest@")) && !toolRange(cb, "vitest"))
      cb.adoptPackages((await remote.packages()).filter((e) => e.name === "vitest"));
    const wanted = new Set(want.map(([h, k]) => `${h} ${k}`));
    let got = 0;
    for (const c of chunks(want, 200)) {
      const rs = (await remote.results(c)) ?? [];
      cb.store.transaction(() => {
        for (const r of rs) {
          if (!wanted.has(`${r.hash} ${r.key}`) || cb.store.testResult(r.hash, r.key)) continue;
          cb.store.putTestResult(
            {
              hash: r.hash,
              ok: !!r.ok,
              message: String(r.message ?? ""),
              output: String(r.output ?? ""),
              ms: Number(r.ms) || 0,
            },
            r.key,
          );
          got++;
        }
      });
    }
    return got;
  } catch {
    return 0;
  }
}

uses

used by