testDeclarationOrder

tcmts.cli.main.testDeclarationOrder · #2jww6r7q · term

import { declarationOrder } from "#codebase/tcmts/cli/main";
import type { DefRecord } from "#codebase/tcmts/core/types";

export const testDeclarationOrder = () => {
  const rec = (hash: string, deps: [string, boolean][]) =>
    ({
      hash,
      deps: deps.map(([ref, strict]) => ({ ref: `#${ref}`, strict })),
    }) as unknown as DefRecord;
  // b's value is a's, used as it's initialized; c only calls a (later), d uses what isn't here
  const recs = [
    rec("b", [["a", true]]),
    rec("c", [["a", false]]),
    rec("a", [["x", true]]),
    rec("d", []),
  ];
  const order = declarationOrder(recs, (r) => [r]).map((r) => r.hash);
  if (order.join() !== "a,b,c,d") throw new Error(order.join());
  // a cycle (which no order typechecks) is broken where it's come to: what it's come to from goes after
  const loop = [rec("p", [["q", true]]), rec("q", [["p", true]])];
  const kept = declarationOrder(loop, (r) => [r]).map((r) => r.hash);
  if (kept.join() !== "q,p") throw new Error(kept.join());
}

uses