testModuleStatePlacement

tcmts.cli.test.e2e.prod.testModuleStatePlacement · #2vcpfae5 · term

module-level state is placed by who uses it, and split state is flagged

import { setUp } from "#codebase/tcmts/cli/test/e2e/prod";
import assert from "node:assert/strict";

export const testModuleStatePlacement = () => {
  const { tcm, scratch } = setUp();
  scratch(`// tcm: namespace st
import { hostname } from "node:os";
export const visits = new Map<string, number>();
export const recent: string[] = [];
export const shared = { hits: 0 };
export const frozen = { a: 1 } as const;
export async function whoami(name: string): Promise<string> { visits.set(name, 1); shared.hits++; return hostname() + String(frozen.a); }
export async function app() { recent.push("x"); shared.hits++; document.title = await whoami("ada"); }
`);
  tcm("add");
  const out = tcm("build", "st.app");
  assert.match(out, /state\s+st\.visits on the server, shared by every client/);
  assert.match(out, /state\s+st\.recent in the browser, one copy per tab/);
  assert.match(out, /warning\s+st\.shared is used by both browser and server code/);
  assert.doesNotMatch(out, /st\.frozen/, "an `as const` value isn't mutable state");
}

uses