Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/gate/handoffs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Report } from "../report/types.ts";
import { runGate } from "./runner.ts";
import { buildRegistry } from "./registry.ts";
import { cfg, fakeLayer, noExec, off, opts, tempProject, tickingClock } from "./testkit.ts";
import type { GateConfig, GateOptions, Layer } from "./types.ts";
import type { Consumed, GateConfig, GateOptions, Layer } from "./types.ts";

/** R29 N5: layers declare the artifacts they produce and consume; a missing input is `not-run`, a present one is judged. */

Expand Down Expand Up @@ -307,12 +307,19 @@ describe("G2c the real registry declares its hand-offs", () => {
const layers = buildRegistry(cfg());
const byId = new Map(layers.map((layer) => [layer.id, layer]));

it("never consumes from a layer it does not require, and the producer declares what is consumed", () => {
for (const layer of layers) {
for (const need of layer.consumes) {
expect(layer.requires).toContain(need.from);
expect(byId.get(need.from)?.produces).toContain(need.artifact);
}
const isRequired = (need: Consumed): boolean => need.optional !== true;

it("never consumes from a layer it does not require", () => {
const needs = layers.flatMap((layer) => layer.consumes.map((need) => [layer, need] as const));
for (const [layer, need] of needs) {
expect(layer.requires).toContain(need.from);
}
});

it("the producer declares what a layer consumes, but an optional input may be one it does not write (R92)", () => {
const needs = layers.flatMap((layer) => layer.consumes.filter(isRequired));
for (const need of needs) {
expect(byId.get(need.from)?.produces).toContain(need.artifact);
}
});

Expand Down
71 changes: 71 additions & 0 deletions src/gate/plan-edges.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { describe, expect, it } from "vitest";
import { planGate } from "./plan.ts";
import { cfg, fakeLayer, opts } from "./testkit.ts";
import type { GateConfig } from "./types.ts";

/** The plan's edges: a switch with no reason, layers that require each other, and ids nothing registers. */

const enabled = (id: string): GateConfig => cfg({ toggles: { [id]: { enabled: true } } });

describe("the reason a layer is skipped", () => {
it("is the config's own words, and says so when the config gave none", () => {
const layers = [fakeLayer({ id: "hygiene" })];
const bare = cfg({ toggles: { hygiene: { enabled: false } } });
expect(planGate(layers, bare, opts())[0]?.skip).toBe("disabled in web-tops.yaml");
const said = cfg({ toggles: { hygiene: { enabled: false, reason: "lands later" } } });
expect(planGate(layers, said, opts())[0]?.skip).toBe("lands later");
});
});

describe("--only follows what a layer requires", () => {
it("ends when two layers require each other", () => {
const layers = [
fakeLayer({ id: "hygiene", gate: 1, requires: ["tests"] }),
fakeLayer({ id: "tests", gate: 2, requires: ["hygiene"] }),
fakeLayer({ id: "coverage", gate: 3 }),
];
const plan = planGate(layers, cfg(), opts({ only: "hygiene" }));
expect(plan.map((item) => [item.id, item.skip, item.requiredBy])).toEqual([
["hygiene", undefined, undefined],
["tests", undefined, "hygiene"],
["coverage", "not selected by --only hygiene", undefined],
]);
});

it("pulls a layer in once, by the first that needs it", () => {
const layers = [
fakeLayer({ id: "hygiene", gate: 1 }),
fakeLayer({ id: "tests", gate: 2, requires: ["hygiene"] }),
fakeLayer({ id: "coverage", gate: 3, requires: ["tests", "hygiene"] }),
];
const plan = planGate(layers, cfg(), opts({ only: "coverage" }));
expect(plan.map((item) => item.requiredBy)).toEqual(["coverage", "coverage", undefined]);
});

it("names a layer nothing registers and pulls nothing else in", () => {
const layers = [fakeLayer({ id: "hygiene", gate: 1 })];
const plan = planGate(layers, cfg(), opts({ only: "ghost" }));
expect(plan.map((item) => [item.id, item.skip])).toEqual([
["hygiene", "not selected by --only ghost"],
]);
});
});

describe("an id the config enables and nothing registers", () => {
const layers = [fakeLayer({ id: "hygiene", gate: 1 })];

it("is in the plan as a layer of its own, and not when --only picks a layer", () => {
expect(planGate(layers, enabled("ghost"), opts()).map((item) => item.id)).toEqual([
"hygiene",
"ghost",
]);
expect(
planGate(layers, enabled("ghost"), opts({ only: "hygiene" })).map((item) => item.id),
).toEqual(["hygiene"]);
});

it("is skipped for the reason any layer would be, such as a tier above the one asked", () => {
const plan = planGate(layers, enabled("ghost"), opts({ maxTier: 0 }));
expect(plan.find((item) => item.id === "ghost")?.skip).toBe("tier 1 is above --max-tier 0");
});
});
2 changes: 1 addition & 1 deletion src/gate/plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface Scope {
}

function requiresOf(layers: readonly Layer[], id: string): readonly string[] {
return layers.find((layer) => layer.id === id)?.requires ?? [];
return layers.filter((layer) => layer.id === id).flatMap((layer) => layer.requires);
}

function select(
Expand Down
61 changes: 61 additions & 0 deletions src/gate/ratchet-mode.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import type { Baseline } from "../baseline/types.ts";
import { runGate } from "./runner.ts";
import { cfg, fakeLayer, noExec, opts, tickingClock } from "./testkit.ts";

/** Only the ratchet mode reads the baseline a config names. */

const made: string[] = [];

afterEach(() => {
for (const dir of made.splice(0)) rmSync(dir, { recursive: true, force: true });
});

const BASELINE = ".web-tops/baseline.json";

const BASE: Baseline = {
report_version: "2.0.0",
subject: { package: "fixture", root: "." },
crap: { "src/a.ts::f": { complexity: 3, coverage: 1, crap: 3 } },
coverage: {},
findings: {},
fingerprints: [],
census: {},
pages: {},
};

function above(enforce: "hard"): ReturnType<typeof runGate> {
const dir = mkdtempSync(join(tmpdir(), "wt-ratchet-mode-"));
made.push(dir);
mkdirSync(join(dir, ".web-tops"));
writeFileSync(join(dir, BASELINE), JSON.stringify(BASE));
const crap = fakeLayer({
id: "crap",
gate: 4,
metrics: {
crap: [{ file: "src/a.ts", name: "f", line: 1, complexity: 3, coverage: 1, crap: 9 }],
},
});
return runGate({
layers: [crap],
config: cfg({ enforce, baselinePath: BASELINE }),
options: opts(),
cwd: dir,
env: {},
exec: noExec,
outDir: join(dir, ".web-tops", "out"),
now: tickingClock(),
});
}

describe("G9b: a run above its baseline", () => {
it("is left alone under hard, though the config names a baseline", async () => {
const hard = await above("hard");
expect(hard.layers[0]?.verdict).toBe("pass");
expect(hard.layers[0]?.findings).toEqual([]);
expect(hard.passed).toBe(true);
});
});
69 changes: 69 additions & 0 deletions src/gate/registry-order.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { describe, expect, it } from "vitest";
import { buildRegistry } from "./registry.ts";
import { compareGates } from "./order.ts";
import { customLayer } from "./stubs.ts";
import { cfg, noExec } from "./testkit.ts";
import type { LayerContext } from "./types.ts";

describe("the registry lists the layers in gate order", () => {
it("with the config's custom layers between gate 6 and laws", () => {
const config = cfg({
custom: [
{ id: "extra", tier: 1, command: "echo" },
{ id: "more", tier: 2 },
],
});
const layers = buildRegistry(config);
expect(layers.map((layer) => layer.id)).toEqual([
"hygiene",
"tests",
"coverage",
"crap",
"mutation",
"build-output",
"extra",
"more",
"laws",
]);
const gates = layers.map((layer) => layer.gate);
expect(gates).toEqual([...gates].sort(compareGates));
});
});

describe("a custom layer is a gate 6 layer that is not run yet", () => {
it("needs and gives nothing, for every launch class, and is not-run with the reason it has no command", async () => {
const mapped = customLayer("extra", 2, "echo");
expect(mapped).toMatchObject({
id: "extra",
gate: 6,
tier: 2,
requires: [],
produces: [],
consumes: [],
});
expect(mapped.classes).toEqual([
"ts-library",
"ui-components",
"astro-site",
"web-app",
"node-service",
]);
const ctx: LayerContext = {
cwd: ".",
config: cfg(),
base: "origin/main",
enforce: "hard",
env: {},
exec: noExec,
outDir: ".",
artifacts: new Map(),
};
const results = [await mapped.run(ctx), await customLayer("bare", 1, undefined).run(ctx)];
expect(results.map((result) => result.verdict)).toEqual(["not-run", "not-run"]);
expect(results.map((result) => result.reason)).toEqual([
"custom layers run from M4",
"no command is mapped to this layer",
]);
expect(results.map((result) => result.findings)).toEqual([[], []]);
});
});
9 changes: 5 additions & 4 deletions src/gate/registry.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { coverageLayer } from "../layers/coverage/index.ts";
import { crapLayer } from "../layers/crap/index.ts";
import { yamlCatalog } from "../laws/layer/catalog.ts";
import { lawsLayer } from "../laws/layer/index.ts";
import { mutationLayer } from "../layers/mutation/index.ts";
import { hygieneLayer } from "../layers/hygiene/index.ts";
import { testsLayer } from "../layers/tests/index.ts";
import { compareGates } from "./order.ts";
import { BUILD_OUTPUT_STUB, customLayer, LAWS_STUB } from "./stubs.ts";
import { BUILD_OUTPUT_STUB, customLayer } from "./stubs.ts";
import type { GateConfig, Layer } from "./types.ts";

/** The layers of one run: the seven built-ins and the config's custom layers, in gate order. */
Expand All @@ -18,6 +19,6 @@ export function buildRegistry(config: GateConfig): Layer[] {
mutationLayer,
BUILD_OUTPUT_STUB,
...custom,
LAWS_STUB,
].sort((a, b) => compareGates(a.gate, b.gate));
lawsLayer(yamlCatalog),
];
}
Loading
Loading