@mmmnt/feat-emit-ts
v0.2.1
Published
Feat emit: TestTopology + resolved schemas/goldens -> complete deterministic Vitest test file (ADR-0006 header, ADR-0001 inlining). Owns no runtime code.
Readme
@mmmnt/feat-emit-ts
The TypeScript emitter of Feature: a test topology in, one complete vitest file out.
emit(inputs) takes the cases that @mmmnt/feat-derive produced, the resolved schemas and golden
fixtures, and writes the text of a generated test file. The file imports only vitest, a few Node
built-ins and @mmmnt/feat-runtime. It holds no logic of its own: every test calls the runtime's
harness.
The output is deterministic. The same inputs give the same bytes. There is no timestamp, no random
value and no machine-specific path in it, which is what lets feat verify byte-compare committed
tests against a fresh generation.
Where it sits
BuiltSpec IR -> feat-derive -> TestTopology
|
feat-emit-ts (this package)
|
specs/foo.test.ts (committed, generated)
|
run by vitest through feat-runner; imports feat-runtimeMost users get this package through @mmmnt/feature,
whose feat generate calls emit once per spec. Depend on it directly when you build your own
generator: you hold a topology and want the file Feature would write. It does not parse, does not
derive and does not write to disk.
Install
npm install @mmmnt/feat-emit-ts- Node 20 or later. The package is ESM only.
- Runtime dependencies are
@mmmnt/feat-derive(for theTestTopologytype) and@mmmnt/feat-types. - The package emits text. To run what it emits, the project needs
vitestand@mmmnt/feat-runtime. This repository tests against vitest 3.2.
Use
This uses the spec shown in the @mmmnt/feat-derive
README, saved as specs/add-item.feat, and a feat.config.json whose only service is store.
import { readFileSync } from "node:fs";
import { contextFromConfig, parseSource } from "@mmmnt/feat-core";
import { derive } from "@mmmnt/feat-derive";
import { EMITTER_VERSION, emit, timingEnvelope } from "@mmmnt/feat-emit-ts";
import type { BuiltSpec, FeatConfig } from "@mmmnt/feat-types";
const config = JSON.parse(readFileSync("feat.config.json", "utf8")) as FeatConfig;
const specText = readFileSync("specs/add-item.feat", "utf8");
const parsed = parseSource(specText, contextFromConfig(config));
if (parsed.status === "ERR") throw new Error(JSON.stringify(parsed.body));
const topology = derive(parsed.body as unknown as BuiltSpec, config);
const source = emit({
topology,
schemas: {},
goldens: {},
hashInputs: {
specText,
contractText: "{}",
configSlice: { featVersion: "1.0", commands: ["AddItem"], actors: [], services: ["store"] },
},
specPath: "specs/add-item.feat",
namespacePath: "specs/add-item.feat",
configPath: "../feat.config.json",
emitterVersion: EMITTER_VERSION,
featVersion: "1.0",
});
console.log(source.split("\n").slice(0, 4).join("\n"));
console.log(timingEnvelope(topology.cases, { services: { store: { consistency: "eventual", convergenceTimeout: 2500 } } }));The first four lines of source are the header. The last line prints
{ testTimeoutMs: 62500, hookTimeoutMs: 120000 }. In a real run schemas is the spec's resolved
contract registry and contractText is the contract file's text.
The generated file
In order:
Header. Four comment lines:
// AUTO-GENERATED by feat emit — do not edit manually // Source: specs/add-item.feat (SPEC-ITM-002) // Language: feat 1.0 · Emitter: @mmmnt/[email protected] // Inputs-hash: sha256:<64 hex digits>Imports.
describe,it,beforeAllandafterAllfromvitest;pathandfileURLToPath; andcreateHarnesswith theHarness,HarnessCaseandInlineDatatypes from@mmmnt/feat-runtime.CONFIG_PATH.process.env.FEAT_CONFIGwhen set, resolved from the working directory. OtherwiseconfigPath, resolved relative to the generated file.feat runsetsFEAT_CONFIGto the active tier's config, so one file serves every tier (ADR-0024).SPEC_PATH. ThenamespacePathinput. The harness derives the file's namespace from this string alone (SPEC-RT-006).INLINE. The schemas and golden fixtures as JSON constants. The file reads no schema or fixture at run time (ADR-0001, ADR-0013).CASES. The topology's cases as JSON, typedHarnessCase[].The suite. One
describe("<specId>: <specName>").beforeAllcallscreateHarness({ configPath: CONFIG_PATH, specPath: SPEC_PATH, cases: CASES }), so the response adapter is set up with the commands the cases invoke (SPEC-RT-008). Each case becomesit(c.name, ...)callingh.runCase(c, INLINE).afterAllcallsh?.teardown().
The optional chain in teardown is deliberate. createHarness throws on a configuration error, and
then there is no harness to tear down. Without the ?. that refusal would end in a TypeError
that hides it.
Inputs-hash
The header's hash is the SHA-256 of three values joined by a NUL byte (\0):
hashInputs.specText, the spec source;hashInputs.contractText, the contract file's text;hashInputs.configSlice, serialized with sorted keys at every depth, so key order never changes the hash.
Change any of the three and the hash changes. The emitter version is in the header but not in the
hash. The hash fingerprints the inputs, not the output: the topology, the schemas and the goldens
are not hashed. feat verify compares the whole file byte for byte, so the hash is a quick way to
tell which input moved.
What goes in configSlice is the caller's choice. feat generate passes only what can change the
parse or the derived structure: featVersion and the sorted names of the services, commands and
actors (without default). Adapters, consistency, timeouts and options stay out, so a tier's
config can change without regenerating anything (ADR-0024).
API
function emit(inputs: EmitInputs): string;
function timingEnvelope(
cases: readonly { given?: { executes?: readonly unknown[] }; delivers?: readonly unknown[] }[],
config: {
services: Record<string, { consistency?: string; convergenceTimeout?: number }>;
response?: { invoke?: Record<string, unknown> };
},
): { testTimeoutMs: number; hookTimeoutMs: number };
const EMITTER_VERSION: string; // this package's version, read from its package.jsonEmitInputs:
| Field | Meaning |
| --- | --- |
| topology | The TestTopology from @mmmnt/feat-derive. |
| schemas | Resolved schema registry for this spec: name to JSON Schema, $refs already resolved. |
| goldens | Resolved golden fixtures: spec-relative path to parsed JSON. |
| hashInputs | { specText, contractText, configSlice }, the three values hashed. |
| specPath | Repo-root-relative path of the .feat file, for the header. |
| namespacePath | The spec file's path relative to the config directory, with / separators. It is the input of the file's namespace. |
| configPath | Path to feat.config.json from the generated file's directory. feat generate passes the base config's, so the file does not depend on the tier. |
| emitterVersion | Written into the header. Pass EMITTER_VERSION. |
| featVersion | The language version, written into the header. |
emit does not validate its inputs. Hand it a topology from derive and schemas that match it.
timingEnvelope
The generated file carries no timeouts, because they depend on the active tier. feat run computes
them from the active config with timingEnvelope and hands them to vitest as testTimeout and
hookTimeout:
testTimeoutMs = invoke * steps + window + 30000
hookTimeoutMs = 120000invokeisconfig.response.invoke.timeoutwhen that is a number, else30000(ms).stepsis the largest, over the cases, of thegiven.executescount plus the greater of 1 and thedeliverscount. It is never below 1.windowis the largestconvergenceTimeoutamong services whoseconsistencyiseventual, else 0.
Run a generated file under plain vitest and it gets vitest's default timeouts. A scenario that
waits out an eventual capture window needs feat run, or a config you write with the same numbers.
Errors
The emitter has no error codes and defines no refusals. A bad topology fails earlier, in the parser
or in derive. A bad run fails in the harness, with the codes listed in
@mmmnt/feat-runtime.
What changed in 0.2.1
- The file's cases reach the harness (SPEC-RT-008).
beforeAllpassescases: CASEStocreateHarness, so the response adapter loads what those cases invoke at setup, under the hook timeout. Needs@mmmnt/feat-runtime0.2.2. Every generated file changes once: that line and the emitter version in its header.
What changed in 0.2.0
- One file for every tier (ADR-0024). The file no longer bakes a timing envelope. It resolves
its config from
FEAT_CONFIGfirst. The hash covers only the spec, the contract and the closed spaces, so changing a timeout, an adapter or an option regenerates nothing. This changes the header hash of every generated file once. - Namespace input (SPEC-RT-006). New
namespacePathinput and aSPEC_PATHconstant in the file.createHarnessnow receivesspecPath, and the harness derives the file's namespace from it. - Safe teardown.
afterAlltears down only a harness that was created, so a refusedcreateHarnessno longer ends in aTypeError. timingEnvelopeis exported, sofeat runand your own tooling share one formula.
Specs and decisions
- SPEC-FEAT-001: the corpus exemplar for the emitter. It names
src/emit.tsas its handler. The code lives insrc/index.ts. - SPEC-RT-006: the namespace each generated file's harness derives.
- ADR-0023: adapter interface v2.
- ADR-0024: tier bindings and environment-agnostic generation.
- Grammar reference: the language the topology comes from.
License
MIT
Part of Feature — the .feat execution specification
language. Docs: https://github.com/mmmnt/feature/wiki
