Files
msd-core/tests/frontmatter.property.test.cjs
Tom Boucher 4b908e6bcd test(tests): antagonistic tier — fast-check property tests + Stryker mutation testing (PR-gated) (#461)
* feat(#454): add antagonistic tier — fast-check property tests + Stryker mutation config

Adds property-based testing (fast-check v4) and mutation testing scaffolding
(Stryker v8) as the antagonistic validation tier for lib/*.cjs pure logic.

## Files added

### Shared setup
- tests/helpers/fast-check-setup.cjs — configureGlobal({ numRuns:200, seed:42 })
  for deterministic CI; override locally with GSD_FC_SEED

### Property test suites (node:test + fast-check)
- tests/context-utilization.property.test.cjs — boundary at 60%/70% thresholds
  (exact Math.ceil boundary, not Math.floor), TypeError on all invalid inputs,
  overflow clamping to 100%/critical, shape invariants (7 tests, all pass)
- tests/prompt-budget.property.test.cjs — estimateTokens monotonicity + ceil(len/4)
  exactness; applyBudget shape invariant, instructions/roadmap verbatim, budget
  envelope, omit tracking (11 tests, all pass)
- tests/frontmatter.property.test.cjs — extractFrontmatter/reconstructFrontmatter/
  spliceFrontmatter never-throw + type shape + splice→extract round-trip (9 tests)
- tests/adr-parser.property.test.cjs — shouldRejectAdrStatus boundary (3 statuses
  only), parseAdrMarkdown shape + title trim invariant (discovered: parser trims
  trailing whitespace) (9 tests, all pass)
- tests/config-schema.property.test.cjs — isValidConfigKey never throws, returns
  boolean, accepts all VALID/RUNTIME_STATE_KEYS, rejects empty/null/unknown (8 tests)

### Stryker mutation config
- stryker.config.mjs — testRunner:'command', mutate bin/lib/**/*.cjs minus 13
  generated files, coverageAnalysis:'off', thresholds {high:80,low:60,break:50},
  incremental:true, reporters html+clear-text+progress

### CI workflow
- .github/workflows/mutation.yml — PR-gating job (pull_request + workflow_dispatch),
  runs stryker --incremental --since origin/next (changed files only), uploads
  HTML artifact; SINCE_REF via env not interpolation (injection-safe)

### Package config
- package.json: +test:mutation, +test:mutation:since scripts
- .gitignore: +.stryker-tmp/, +.stryker-incremental.json, +reports/mutation/
- package-lock.json: fast-check@4.8.0, @stryker-mutator/core@9.6.1

## Verified
node --test on all 5 property test files: 44 tests, 0 failures.
Stryker NOT run (slow; reserved for CI).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(#454): pin upload-artifact to v7.0.1 SHA used across repo (bad SHA ea165f8d)

The SHA ea165f8d65b6e75b540449d3ec4f5dde0c5a4e1 (labeled v4.6.2) does not
resolve on GitHub Actions. All other workflows in this repo pin
043fb46d1a93c77aae656e7c1c64a875d1fc6a0a (v7.0.1) — align mutation.yml.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(#454): mutation workflow — replace invalid --since flag with changed-core-files --mutate scoping

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: CI Rebase Check <ci@gsd-redux>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-05-29 11:07:07 -04:00

195 lines
6.7 KiB
JavaScript

'use strict';
/**
* Property-based tests for frontmatter.cjs
*
* Module: get-shit-done/bin/lib/frontmatter.cjs
* Exported (pure): extractFrontmatter, reconstructFrontmatter, spliceFrontmatter
*
* Properties tested:
* (a) extractFrontmatter never throws on ANY string input (including binary/unicode)
* (b) extractFrontmatter always returns a plain object (not null, not array)
* (c) round-trip: reconstructFrontmatter(extractFrontmatter(spliceFrontmatter(content, obj)))
* preserves key-value pairs for simple flat string values
* (d) spliceFrontmatter never throws on any string/object combination
* (e) extractFrontmatter returns {} for content without a leading ---...--- block
*/
const { describe, test } = require('node:test');
const assert = require('node:assert/strict');
const fc = require('./helpers/fast-check-setup.cjs');
const {
extractFrontmatter,
reconstructFrontmatter,
spliceFrontmatter,
} = require('../get-shit-done/bin/lib/frontmatter.cjs');
// ─── Arbitraries ─────────────────────────────────────────────────────────────
// Simple YAML key: alphanumeric + underscore, at least 1 char
const yamlKey = fc.stringMatching(/^[a-z][a-z0-9_]{0,19}$/);
// Simple YAML scalar value: printable ASCII without : ' " # newlines
const yamlScalarValue = fc.stringMatching(/^[a-zA-Z0-9 ._/-]{1,40}$/);
// ─── Tests ────────────────────────────────────────────────────────────────────
describe('frontmatter: extractFrontmatter properties', () => {
// (a) Never throws on any string input
test('property: extractFrontmatter never throws on arbitrary binary/unicode input', () => {
fc.assert(
fc.property(
fc.oneof(
fc.string({ unit: 'binary', maxLength: 300 }),
fc.string({ unit: 'grapheme-composite', maxLength: 300 }),
fc.constant(''),
fc.constant('---\n---'),
fc.constant('---\nkey: value\n---\n# body'),
fc.string({ maxLength: 300 })
),
(input) => {
assert.doesNotThrow(
() => extractFrontmatter(input),
`extractFrontmatter threw on input: ${JSON.stringify(input.slice(0, 50))}`
);
}
)
);
});
// (b) Always returns a plain object
test('property: extractFrontmatter always returns a plain object', () => {
fc.assert(
fc.property(
fc.oneof(
fc.string({ unit: 'binary', maxLength: 200 }),
fc.string({ unit: 'grapheme-composite', maxLength: 200 }),
fc.string({ maxLength: 200 })
),
(input) => {
const result = extractFrontmatter(input);
assert.ok(
typeof result === 'object' && result !== null && !Array.isArray(result),
`extractFrontmatter must return plain object, got ${JSON.stringify(result)}`
);
}
)
);
});
// (e) Returns {} for content without leading --- block
test('property: content without leading --- block returns empty object', () => {
fc.assert(
fc.property(
fc.oneof(
fc.string({ minLength: 0, maxLength: 200 }).filter((s) => !s.startsWith('---')),
fc.constant('# Just a heading'),
fc.constant('plain text content'),
fc.constant('')
),
(input) => {
const result = extractFrontmatter(input);
assert.deepEqual(
result,
{},
`Expected {} for non-frontmatter input, got ${JSON.stringify(result)}`
);
}
)
);
});
});
describe('frontmatter: reconstructFrontmatter properties', () => {
test('property: reconstructFrontmatter never throws on plain objects with string values', () => {
fc.assert(
fc.property(
fc.dictionary(yamlKey, yamlScalarValue, { maxKeys: 10 }),
(obj) => {
assert.doesNotThrow(
() => reconstructFrontmatter(obj),
`reconstructFrontmatter threw on ${JSON.stringify(obj)}`
);
}
)
);
});
test('property: reconstructFrontmatter output is a string', () => {
fc.assert(
fc.property(
fc.dictionary(yamlKey, yamlScalarValue, { maxKeys: 8 }),
(obj) => {
const result = reconstructFrontmatter(obj);
assert.ok(typeof result === 'string', `Expected string got ${typeof result}`);
}
)
);
});
test('property: reconstructFrontmatter on {} returns empty string', () => {
assert.equal(reconstructFrontmatter({}), '');
});
});
describe('frontmatter: spliceFrontmatter properties', () => {
// (d) Never throws on any combination
test('property: spliceFrontmatter never throws on arbitrary content + object', () => {
fc.assert(
fc.property(
fc.string({ maxLength: 300 }),
fc.dictionary(yamlKey, yamlScalarValue, { maxKeys: 8 }),
(content, obj) => {
assert.doesNotThrow(
() => spliceFrontmatter(content, obj),
`spliceFrontmatter threw on content=${JSON.stringify(content.slice(0, 30))}`
);
}
)
);
});
test('property: spliceFrontmatter always returns a string', () => {
fc.assert(
fc.property(
fc.string({ maxLength: 200 }),
fc.dictionary(yamlKey, yamlScalarValue, { maxKeys: 5 }),
(content, obj) => {
const result = spliceFrontmatter(content, obj);
assert.ok(typeof result === 'string', `Expected string got ${typeof result}`);
}
)
);
});
// (c) Round-trip: splice then extract preserves flat string keys
test('property: splice then extract round-trip preserves flat string values', () => {
fc.assert(
fc.property(
fc.string({ maxLength: 100 }), // existing document body
// Only keys + simple values without colons/hashes that would confuse the minimal parser
fc.dictionary(
fc.stringMatching(/^[a-z][a-z0-9]{0,14}$/),
fc.stringMatching(/^[a-zA-Z0-9]{1,30}$/),
{ minKeys: 1, maxKeys: 5 }
),
(body, obj) => {
const spliced = spliceFrontmatter(body, obj);
const extracted = extractFrontmatter(spliced);
for (const [key, value] of Object.entries(obj)) {
if (typeof value === 'string' && value.length > 0) {
assert.equal(
extracted[key],
value,
`Round-trip failed for key=${key}: expected ${value} got ${extracted[key]}`
);
}
}
}
)
);
});
});