testComponentBatchesSendWhatsUsedFirst

tcmts.cli.sync.testComponentBatchesSendWhatsUsedFirst · #iitscayc · term

Batches hold whole components, each after what it uses, other texts last, at most so many records each.

import { componentBatches } from "#codebase/tcmts/cli/sync";
import type { DefRecord } from "#codebase/tcmts/core/types";
import assert from "node:assert/strict";

export const testComponentBatchesSendWhatsUsedFirst = () => {
  const rec = (hash: string, comp: string, uses: string[], size = 1, index = 0): DefRecord =>
    ({
      hash,
      componentHash: comp,
      componentIndex: index,
      componentSize: size,
      deps: uses.map((u) => ({ ref: `#${u}`, pos: "value", strict: false })),
      sourceText: hash,
    }) as unknown as DefRecord;
  // c uses the a/b component, which uses d; e uses c and something not sent (z); a's other text
  const records = [
    rec("e", "E", ["c", "z"]),
    rec("c", "C", ["a"]),
    rec("a", "AB", ["b", "d"], 2, 0),
    rec("b", "AB", ["a"], 2, 1),
    rec("d", "D", []),
    { ...rec("a", "AB", ["b", "d"], 2, 0), sourceText: "a, commented" },
  ];
  const order = componentBatches(records).flat();
  const at = (h: string) => order.findIndex((r) => r.hash === h);
  assert.equal(order.length, 6);
  assert.ok(at("d") < at("a") && at("a") < at("c") && at("c") < at("e"));
  assert.equal(order.at(-1)!.sourceText, "a, commented");
  // a component isn't split, even where a batch would be full
  const small = componentBatches(records, Infinity, 1);
  assert.deepEqual(
    small.map((b) => b.map((r) => r.hash)),
    [["d"], ["a", "b"], ["c"], ["e"], ["a"]],
  );
}

uses