// tests/lib/test-first-multi-file.test.mjs // Plan step 3 (files-backticks trial 4): below plan_version 1.9 every test file // on the `Test first:` `File:` line is read, not only the first backticked // span. 6.3.1 read the first span alone, so a step whose `Files:` were its two // `Test first:` files was not seen as tests-only when the second file is not a // test by its path. One reading: testFirstFiles (plan-schema.mjs), used by // stepTestsOnly; the executor and the step helper say the same in prose. import { test } from 'node:test'; import { strict as assert } from 'node:assert'; import { readFileSync } from 'node:fs'; import { dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; import * as m from '../../lib/parsers/plan-schema.mjs'; import { validatePlanContent } from '../../lib/validators/plan-validator.mjs'; import { buildPlan } from '../helpers/plan-v18-builder.mjs'; const HERE = dirname(fileURLToPath(import.meta.url)); const ROOT = join(HERE, '..', '..'); /** A 1.8-shaped step section: `Files:` and the `Test first:` lines given. */ const step = (files, testFirst) => [ '### Step 1: x', '', `- **Files:** ${files}`, '- **Changes:** x', '- **Test first:**', ...testFirst, '- **Verify:** `node --test tests/a.test.mjs` -> expected: exit 0', ].join('\n'); const TWO = step('`tests/a.test.mjs`, `checks/smoke.mjs`', [ ' - File: `tests/a.test.mjs`, `checks/smoke.mjs` (new)', ' - Verifies: both checks run', ]); const testFirstFiles = (body, version) => { assert.equal(typeof m.testFirstFiles, 'function', 'plan-schema.mjs exports testFirstFiles'); return m.testFirstFiles(m.parseStepFields(body), version); }; test('a 1.8 step whose Files are its two Test first files is tests-only', () => { // checks/smoke.mjs is not a test by its path: only the Test first line makes it one. assert.equal(m.stepTestsOnly(TWO).testsOnly, true, JSON.stringify(m.stepTestsOnly(TWO))); }); test('testFirstFiles returns every path on the File: line, below 1.9 and without a version', () => { assert.deepEqual(testFirstFiles(TWO, '1.8'), ['tests/a.test.mjs', 'checks/smoke.mjs']); assert.deepEqual(testFirstFiles(TWO, null), ['tests/a.test.mjs', 'checks/smoke.mjs']); assert.deepEqual(testFirstFiles(TWO), ['tests/a.test.mjs', 'checks/smoke.mjs']); }); test('testFirstFiles: a call, an option or a URL on the File: line is not read; paths are normalised and deduplicated', () => { const body = step('`tests/a.test.mjs`', [ ' - File: `tests/a.test.mjs`, `parse()`, `--watch`, `https://example.invalid/x.mjs`, `./tests/a.test.mjs`', ' - Verifies: x', ]); assert.deepEqual(testFirstFiles(body, '1.8'), ['tests/a.test.mjs']); }); test('testFirstFiles: a Verifies: line, or a span before File: on the line, is never read', () => { const body = step('`tests/a.test.mjs`', [ ' - see `lib/before.mjs` File: `tests/a.test.mjs`', ' - Verifies: `lib/other.mjs` returns `src/x.mjs`', ]); assert.deepEqual(testFirstFiles(body, '1.8'), ['tests/a.test.mjs']); const none = step('`tests/a.test.mjs`', [' - Verifies: `tests/a.test.mjs` runs']); assert.deepEqual(testFirstFiles(none, '1.8'), []); }); test('a code file beside the test on the File: line still makes the step not tests-only when Files has more', () => { const body = step('`tests/a.test.mjs`, `src/cli.mjs`', [ ' - File: `tests/a.test.mjs`, `parse()` (new)', ' - Verifies: x', ]); assert.equal(m.stepTestsOnly(body).testsOnly, false); }); test('prose: the executor and the step helper name every file on the Test first File: line', () => { const phrase = 'every file on the `Test first:` `File:` line'; for (const rel of [['commands', 'trekexecute.md'], ['agents', 'step-implementer.md']]) { const text = readFileSync(join(ROOT, ...rel), 'utf8'); assert.ok(text.includes(phrase), `${rel.join('/')} says "${phrase}"`); } }); // ---- plan_version 1.9 (plan step 5) ---------------------------------------- // From 1.9 a `Test first:` `File:` line is every line that opens with `File:` // (after an optional list marker and `**`), backticked or not, and its rest is // one entry read by entryPaths: the leading backticked paths are the test // files, the rest is description. The validator puts the same grammar on it. const PATH_CODES = ['PLAN_STEP_PATH_NOT_FIRST', 'PLAN_STEP_PATH_NOT_A_PATH']; // A plan of the given version whose first step has `Files:` `files` and the // `Test first:` `File:` line `fileLine` (both verbatim). const planWith = (version, files, fileLine) => buildPlan({ version, steps: [{ filesRaw: files, testFirstRaw: fileLine }, { endToEnd: 'bash tests/e2e.sh' }], }); test('1.9: every leading backticked path on the File: line is a test file', () => { const body = step('`tests/a.test.mjs`, `tests/b.test.mjs`', [ ' - File: `tests/a.test.mjs`, `tests/b.test.mjs` (new)', ' - Verifies: x', ]); assert.deepEqual(testFirstFiles(body, '1.9'), ['tests/a.test.mjs', 'tests/b.test.mjs']); }); test('1.9: a backticked path in the description of the File: line is not a test file', () => { const body = step('`tests/a.test.mjs`', [ ' - File: `tests/a.test.mjs` (new), covers `tests/b.test.mjs`', ' - Verifies: x', ]); assert.deepEqual(testFirstFiles(body, '1.9'), ['tests/a.test.mjs']); // Below 1.9 the line is read as 6.3.1 + Step 3 read it: every path-shaped span. assert.deepEqual(testFirstFiles(body, '1.8'), ['tests/a.test.mjs', 'tests/b.test.mjs']); }); test('1.9: a code file named only in the File: line description keeps the step from being tests-only; in 1.8 it is tests-only', () => { const files = '`tests/a.test.mjs`, `checks/smoke.mjs`'; const line = ' - File: `tests/a.test.mjs` (new), covers `checks/smoke.mjs`'; const body = step(files, [line, ' - Verifies: x']); assert.equal(m.stepTestsOnly(body, '1.9').testsOnly, false, JSON.stringify(m.stepTestsOnly(body, '1.9'))); assert.equal(m.stepTestsOnly(body, '1.8').testsOnly, true, JSON.stringify(m.stepTestsOnly(body, '1.8'))); const v19 = validatePlanContent(planWith('1.9', files, line)); assert.ok(!v19.warnings.some((w) => w.code === 'PLAN_STEP_TESTS_ONLY'), JSON.stringify(v19.warnings)); assert.deepEqual(v19.errors.filter((e) => PATH_CODES.includes(e.code)), [], JSON.stringify(v19.errors)); const v18 = validatePlanContent(planWith('1.8', files, line)); assert.ok(v18.warnings.some((w) => w.code === 'PLAN_STEP_TESTS_ONLY'), JSON.stringify(v18.warnings)); }); test('1.9: a File: line that opens with `parse()` is refused as no path (error strict, warning soft)', () => { const text = planWith('1.9', '`tests/a.test.mjs`', ' - File: `parse()`'); const strict = validatePlanContent(text); const e = strict.errors.find((i) => i.code === 'PLAN_STEP_PATH_NOT_A_PATH'); assert.ok(e, JSON.stringify(strict.errors)); assert.match(e.message, /Step 1/); assert.match(e.message, /Test first/); const soft = validatePlanContent(text, { strict: false }); assert.ok(soft.warnings.some((w) => w.code === 'PLAN_STEP_PATH_NOT_A_PATH'), JSON.stringify(soft.warnings)); assert.deepEqual(soft.errors.filter((i) => PATH_CODES.includes(i.code)), []); }); test('1.9: an unticked File: line gets PLAN_STEP_PATH_NOT_FIRST next to PLAN_STEP_NO_TEST_FIRST', () => { const r = validatePlanContent(planWith('1.9', '`tests/a.test.mjs`', ' - File: tests/a.test.mjs')); const codes = r.errors.map((e) => e.code); assert.ok(codes.includes('PLAN_STEP_PATH_NOT_FIRST'), codes.join(',')); assert.ok(codes.includes('PLAN_STEP_NO_TEST_FIRST'), codes.join(',')); const e = r.errors.find((i) => i.code === 'PLAN_STEP_PATH_NOT_FIRST'); assert.match(e.message, /Step 1/); assert.match(e.message, /tests\/a\.test\.mjs/); });