testParameterSchemasStaySmall

tcmts.core.test.hash.testParameterSchemasStaySmall · #zv6zbr43 · term

A type a parameter reaches by several routes is written once (shared, and still checked wherever it's used), and an object that must hold a function can't arrive as data, so it's described as that alone: Effect's schema nodes, each with a getParser method and reached a hundred ways, made schemas of megabytes before.

import { checkArgs } from "#codebase/tcmts/core/schema";
import { analyze } from "#codebase/tcmts/core/test/hash";
import assert from "node:assert/strict";

export const testParameterSchemasStaySmall = () => {
  const [def, send] = analyze(`
    type Point = { x: number; y: number };
    type Node = { readonly tag: "node"; parse(): unknown };
    export function save(a: Point, b: Point, pair: { from: Point; to: Point }, on?: { done?: () => void }) {}
    export function send(node: Node) {}
  `).defs.filter((d) => d.nameHint === "save" || d.nameHint === "send");
  const p = def!.params!;
  assert.equal(JSON.stringify(p).match(/"x"/g)?.length, 1, "Point is written once");
  const pt = { x: 1, y: 2 };
  assert.equal(checkArgs(p, [pt, pt, { from: pt, to: pt }, {}]), undefined);
  assert.match(
    checkArgs(p, [pt, pt, { from: pt, to: { x: "1", y: 2 } }])!,
    /`pair`\.to\.x should be a number/,
  );
  assert.match(
    checkArgs(send!.params!, [{ tag: "node" }])!,
    /`node` is an object \(its `parse` is a function\), which can.t be sent/,
  );
  assert.equal(
    checkArgs(p, [pt, pt, { from: pt, to: pt }, {}]),
    undefined,
    "an optional function leaves the object data",
  );
}

uses