@activate-core/test-forge 2.7.0 → 2.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 2.7.1
4
+
5
+ - New code never makes generate write a test again. With the doc unchanged and every rule tested, `generate --refresh-code` reads the code facts again and fills the names the tests wait on from them (two small calls); the test files are not rewritten. `--rewrite-waiting` writes the waiting tests again when asked by name.
6
+ - kit.insert's id is used as the id it is, with no AI: `op[ID_FIELD]` (ID_FIELD = need('inserted row id field')) becomes `op`, and the blank name goes.
7
+ - With `--rewrite-waiting`: a file whose blank names sit in its hooks or helpers has only that setup written again (its tests stay); a file whose blank names sit in a parametrised table or loop is written again whole, once per doc and code, never one holding a passed test.
8
+ - A refused answer keeps the tests in it that pass the checks on their own.
9
+
3
10
  ## 2.7.0
4
11
 
5
12
  - The names new tests still wait on are looked up once, at generate time, against the journey's code facts; a value is kept only if the code has it.
package/dist/cli/index.js CHANGED
@@ -93,7 +93,7 @@ function printUsage() {
93
93
  console.log(' testforge generate --basic Use the basic generator only');
94
94
  console.log(' testforge generate --force Regenerate everything');
95
95
  console.log(' testforge generate --no-wait Stop when the AI hits its usage limit (default: wait for the reset and resume)');
96
- console.log(' testforge generate --plan-only Plan and check each journey (testforge/journeys/<area>/<id>/plan-check.md), write no tests', ' testforge generate --journey <name> --refresh-code Read the code again and rewrite the tests still waiting on names (after the feature code lands)');
96
+ console.log(' testforge generate --plan-only Plan and check each journey (testforge/journeys/<area>/<id>/plan-check.md), write no tests', ' testforge generate --journey <name> --refresh-code Read the code again and fill the names the tests wait on (tests are not written again)', ' testforge generate --journey <name> --rewrite-waiting Also write again the tests still waiting on names (costs AI calls)');
97
97
  console.log(' testforge generate --journey <name> Generate just ONE journey');
98
98
  console.log(' testforge generate --area <area> Generate a whole area (e.g. api)');
99
99
  console.log(' testforge discover Draft journey docs (asks: map or reference)');
@@ -250,6 +250,7 @@ async function main() {
250
250
  : undefined, // undefined -> config.waitForLimit (default on)
251
251
  planOnly: process.argv.includes('--plan-only'),
252
252
  refreshCode: process.argv.includes('--refresh-code'),
253
+ rewriteWaiting: process.argv.includes('--rewrite-waiting'),
253
254
  });
254
255
  if (results.length === 0) {
255
256
  console.log('No journey files found in testforge/docs/. Add a .md file and try again.');
@@ -260,6 +261,8 @@ async function main() {
260
261
  // A row with no test dir is the run-level notice (AI unavailable), not an
261
262
  // unchanged journey — do not label it as one.
262
263
  console.log(r.testDir ? ` [skip] ${r.journeyName} (unchanged)` : ` [skip] ${r.journeyName}`);
264
+ for (const w of r.warnings ?? [])
265
+ console.log(` ! ${w}`);
263
266
  }
264
267
  else {
265
268
  const tag = r.status === 'ai' ? '[AI]' : '[basic]';
@@ -966,3 +966,44 @@ export function cleanOutput(text) {
966
966
  .join('\n')
967
967
  .trim();
968
968
  }
969
+ /**
970
+ * Write a file's setup again (its hooks and helpers), not its tests: the
971
+ * setup waited on names the code facts answer, or built data through routes
972
+ * the facts do not show. One small call; the tests stay as they are.
973
+ * Returns the new blocks in order, or null when the answer does not have one
974
+ * per block.
975
+ */
976
+ export async function rewriteSetupWithAI(input) {
977
+ const mark = (n) => `// ==== SETUP ${n} ====`;
978
+ const prompt = [
979
+ `You are a senior SDET rewriting the setup of one Jest test file: its hooks and helpers, not its tests.`,
980
+ `The tests below this setup stay exactly as they are and use what it sets.`,
981
+ ``,
982
+ `Rules`,
983
+ `- Make the data the setup needs with kit.insert(table, row) and kit.update(table, match, values),`,
984
+ ` using the tables and columns <code_facts> shows. kit.insert returns the new row's id itself.`,
985
+ `- Send a request only through a route <code_facts> shows, with its fields as written there.`,
986
+ `- Never call need(), and never use a constant that is not in <file_top> or set by this setup.`,
987
+ `- Keep each block what it is (the same hook, or a helper with the same name and parameters), and`,
988
+ ` keep every variable it sets (ctx.*, outer let variables, maps) with the same name and meaning.`,
989
+ `- No comments except what was there.`,
990
+ ``,
991
+ `Output: for each block, the line ${mark(1)} (with its number) and then its code. Nothing else.`,
992
+ ``,
993
+ `<testkit>`, input.helpers, `</testkit>`,
994
+ ``,
995
+ ...(input.facts ? [`<code_facts>`, input.facts, `</code_facts>`, ``] : []),
996
+ `<file_top path="${input.filePath}">`, input.top, `</file_top>`,
997
+ ``,
998
+ ...input.blocks.flatMap((b, i) => [mark(i + 1), b.text, ``]),
999
+ ].join('\n');
1000
+ const config = loadConfig();
1001
+ const { program, args } = withNoTools(splitCommand(config.aiCommand), 'Answer only with the setup blocks asked for.');
1002
+ // Split on the markers first: the comment cleaner would take them out.
1003
+ const out = await runWithStdin(program, args, prompt, config.aiTimeoutMs);
1004
+ const parts = out.split(/^\/\/ ==== SETUP (\d+) ====[ \t]*$/m);
1005
+ const got = [];
1006
+ for (let i = 1; i < parts.length; i += 2)
1007
+ got[Number(parts[i]) - 1] = parts[i + 1].replace(/^\s*```\w*\s*$/gm, '').trim();
1008
+ return got.length === input.blocks.length && got.every((g) => g && g.length > 0) ? got : null;
1009
+ }
@@ -82,6 +82,7 @@ const STAGES = [
82
82
  [/^=== REPAIR ===$/m, 'repair'],
83
83
  [/^You are a senior QA engineer\. Generate a complete/, 'write'],
84
84
  [/^You are a senior QA engineer writing one .* test file/, 'write'],
85
+ [/^You are a senior SDET rewriting the setup/, 'setup'],
85
86
  [/^You are a senior test architect/, 'plan'],
86
87
  [/^You are a senior engineer establishing the MECHANICS/, 'grounding'],
87
88
  [/^You review ONE claimed product defect/, 'defect-review'],
@@ -102,8 +102,25 @@ export async function completeFile(content, ctx) {
102
102
  calls = 1;
103
103
  try {
104
104
  const tests = await ctx.write(ctx.journey, ctx.path, head, title, '', ctx.harnessPath, ctx.transport, factsSlice(ctx.facts, slice.controls), { journey: slice.text, helpers: ctx.helpers, rules: left }, describeProblems(firstDrop, first.cov));
105
- const candidate = (ctx.fix ?? ((c) => c))(appendTests(out, onlyNewTests(tests, out, left)));
106
- const refused = ctx.check(candidate);
105
+ const fix = ctx.fix ?? ((c) => c);
106
+ let candidate = fix(appendTests(out, onlyNewTests(tests, out, left)));
107
+ let refused = ctx.check(candidate);
108
+ if (refused) {
109
+ // One bad test refuses the whole answer: keep the tests that pass the
110
+ // checks on their own, so the good ones are not thrown away with it.
111
+ const answer = onlyNewTests(tests, out, left);
112
+ let kept = out;
113
+ for (const b of findTestBlocks(answer)) {
114
+ const next = fix(appendTests(kept, answer.slice(b.start, b.end)));
115
+ if (!ctx.check(next))
116
+ kept = next;
117
+ }
118
+ if (kept !== out) {
119
+ notes.push(`some tests written again for ${left.join(', ')} were refused (${refused.split('\n')[0].slice(0, 160)}); the others were kept`);
120
+ candidate = kept;
121
+ refused = null;
122
+ }
123
+ }
107
124
  if (refused) {
108
125
  notes.push(`tests written again for ${left.join(', ')} were refused (${refused.split('\n')[0]})`);
109
126
  failedRedo = true;
@@ -26,13 +26,46 @@ import { cleanStaleFiles, collisionWarning, hasDeferredStubs, isDeferredStub, is
26
26
  import { aiFailureNote, aiUnavailableReason, confirmAiAlive, lastAiFailureWasProcess, noteAiFailure, noteAiRejected, noteAiSuccess, resetAiSession, waitForAi, } from './ai-session.js';
27
27
  import { generateTestCases } from './generator.js';
28
28
  import { writeDeferredStub } from './playwright-writer.js';
29
- import { buildPrompt, generateWithAI, generateWithAIAsync, writeFilesWithAI, writeTestsWithAI, } from './ai-generator.js';
30
- import { completeFile } from './complete-file.js';
31
- import { tuckStrayTests } from './test-blocks.js';
29
+ import { rewriteSetupWithAI, buildPrompt, generateWithAI, generateWithAIAsync, writeFilesWithAI, writeTestsWithAI, } from './ai-generator.js';
30
+ import { completeFile, withoutBlankNames, withoutNames } from './complete-file.js';
31
+ import { findSetupBlocks, findTestBlocks, tuckStrayTests, useInsertIds } from './test-blocks.js';
32
32
  import { passedTitles } from './passed.js';
33
33
  import { scanNames } from '../run/name-scan.js';
34
34
  import { INTERFACE_PATH, isThingName, neededNames, pruneInterface, readInterface, recordInterface } from './interface-file.js';
35
35
  /** Names the file asks for that testforge/interface.json still has blank. */
36
+ /**
37
+ * Is a blank name used outside every test (a hook, a helper, the file's
38
+ * setup)? Then the whole file waits on it, and only writing the whole file
39
+ * again — with the code facts — clears it; no test of it can have passed.
40
+ */
41
+ export function blankUsedOutsideTests(content) {
42
+ const blocks = findTestBlocks(content);
43
+ const outside = blocks.reduceRight((c, b) => c.slice(0, b.start) + c.slice(b.end), content);
44
+ return blankNames(content).some((n) => {
45
+ const esc = n.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
46
+ const v = new RegExp(`\\b(?:const|let|var)\\s+([A-Za-z_$][\\w$]*)\\s*=\\s*need\\(\\s*(['"\`])${esc}\\2`).exec(content)?.[1];
47
+ if (!v)
48
+ return false;
49
+ const uses = outside.match(new RegExp(`\\b${v.replace(/\$/g, '\\$')}\\b`, 'g')) ?? [];
50
+ return uses.length > 1; // more than its own declaration
51
+ });
52
+ }
53
+ /**
54
+ * Where a file's blank names are used outside its tests: 'setup' when every
55
+ * such use sits in a hook or helper TestForge can rewrite on its own, 'file'
56
+ * when one sits somewhere it cannot split out (a parametrised table, a test
57
+ * built in a loop), undefined when none.
58
+ */
59
+ export function blankOutsideKind(content) {
60
+ if (!blankUsedOutsideTests(content))
61
+ return undefined;
62
+ const vars = [...content.matchAll(/\b(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*=\s*need\(\s*(['"`])([^'"`\n]+)\2/g)]
63
+ .filter((m) => blankNames(`need('${m[3]}')`).length > 0).map((m) => m[1]);
64
+ const covered = [...findTestBlocks(content), ...findSetupBlocks(content)];
65
+ const rest = covered.sort((a, b) => b.start - a.start).reduce((c, b) => c.slice(0, b.start) + c.slice(b.end), content);
66
+ const used = vars.some((v) => (rest.match(new RegExp(`\\b${v.replace(/\$/g, '\\$')}\\b`, 'g')) ?? []).length > 1);
67
+ return used ? 'file' : 'setup';
68
+ }
36
69
  /** Does this test use a name the product has not given yet (directly, or through the constant it was put in)? */
37
70
  function usesBlankName(content, block) {
38
71
  const text = content.slice(block.start, block.end);
@@ -504,6 +537,46 @@ export async function runGenerate(docsDir, useAI = true, force = false, only, ar
504
537
  return {};
505
538
  }
506
539
  };
540
+ const rememberRefused = (slug, ids) => {
541
+ try {
542
+ const all = readRefused();
543
+ all[slug] = [...new Set([...(all[slug] ?? []), ...ids])];
544
+ mkdirSync(dirname(refusedPath), { recursive: true });
545
+ writeFileSync(refusedPath, JSON.stringify({ key: refusedKey(), rules: all }, null, 2), 'utf-8');
546
+ }
547
+ catch { /* best effort */ }
548
+ };
549
+ /** The file with the setup blocks that use a blank name written again, or null when that did not work. */
550
+ const redoSetup = async (content, filePath, facts, kitApi) => {
551
+ const isBlank = (n) => blankNames(`need('${n}')`).length > 0;
552
+ const blankVars = [...content.matchAll(/\b(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*=\s*need\(\s*(['"`])([^'"`\n]+)\2/g)]
553
+ .filter((m) => isBlank(m[3])).map((m) => m[1]);
554
+ const uses = (t) => blankVars.some((v) => new RegExp(`\\b${v.replace(/\$/g, '\\$')}\\b`).test(t));
555
+ const blocks = findSetupBlocks(content).filter((b) => uses(b.text));
556
+ if (!blocks.length)
557
+ return null;
558
+ const top = withoutBlankNames(content.slice(0, Math.min(...blocks.map((b) => b.start))), isBlank).slice(-6000);
559
+ let got = null;
560
+ try {
561
+ got = await rewriteSetupWithAI({ filePath, top, blocks, facts, helpers: kitApi });
562
+ noteAiSuccess();
563
+ }
564
+ catch (e) {
565
+ noteAiFailure(e);
566
+ return null;
567
+ }
568
+ if (!got)
569
+ return null;
570
+ // Each block is still what it was (the same hook or helper), and asks for nothing.
571
+ if (got.some((g, i) => !g.includes(blocks[i].name) || /\bneed\(/.test(g) || uses(g)))
572
+ return null;
573
+ let out = content;
574
+ for (let i = blocks.length - 1; i >= 0; i--)
575
+ out = out.slice(0, blocks[i].start) + got[i] + out.slice(blocks[i].end);
576
+ out = withoutNames(out);
577
+ const why = isSyntaxValid(out, ext) ? genGate(out, filePath) : syntaxReason(out, ext);
578
+ return why ? null : out;
579
+ };
507
580
  const suiteComplete = () => {
508
581
  const refused = readRefused();
509
582
  const plan = readPlan(journey);
@@ -528,7 +601,9 @@ export async function runGenerate(docsDir, useAI = true, force = false, only, ar
528
601
  continue;
529
602
  const p = join(testDir, f);
530
603
  const before = readFileSync(p, 'utf-8');
531
- const after = tuckStrayTests(before);
604
+ // Both mechanical, no AI: stray tests back inside the describe, and
605
+ // kit.insert's id used as the id it is (op[ID_FIELD] → op).
606
+ const after = useInsertIds(tuckStrayTests(before));
532
607
  if (after !== before)
533
608
  writeFileSync(p, after, 'utf-8');
534
609
  }
@@ -546,6 +621,28 @@ export async function runGenerate(docsDir, useAI = true, force = false, only, ar
546
621
  catch {
547
622
  return false;
548
623
  } }));
624
+ // The doc is the same and every rule has its test: new code changes no
625
+ // test. Its facts are read again and the names the tests wait on are
626
+ // filled from them; the test files are not written again (that is
627
+ // --rewrite-waiting, asked for by name).
628
+ if (!force && !opts.rewriteWaiting && waitsOnCode && cache[file] === hash && !hasDeferredStubs(testDir, ext) && suiteComplete()) {
629
+ const usage0 = aiUsage();
630
+ const facts = /^status:\s*not built/im.test(markdown) ? null : await groundJourney(journey, hash, sourceRoot(), markdown, false, opts.refreshCode);
631
+ const blank = [...new Set(testFiles0.flatMap((p) => { try {
632
+ return blankNames(readFileSync(p, 'utf-8'));
633
+ }
634
+ catch {
635
+ return [];
636
+ } }))];
637
+ const res = facts && blank.length ? scanNames(blank, sourceRoot(), facts) : undefined;
638
+ const notes = [
639
+ `doc unchanged: no test written again (code facts read again${res ? `, ${res.filled.length} of ${blank.length} blank name(s) filled from them` : ''})`,
640
+ ...(res?.filled.length ? [`filled: ${res.filled.slice(0, 8).map((f) => `${f.name} = ${f.value}`).join(', ')}${res.filled.length > 8 ? ', …' : ''}`] : []),
641
+ ...(res?.stillBlank.length ? [`${res.stillBlank.length} name(s) still blank — fill them in testforge/interface.json, or run with --rewrite-waiting to have the tests written again: ${res.stillBlank.slice(0, 8).join(', ')}${res.stillBlank.length > 8 ? ', …' : ''}`] : []),
642
+ ];
643
+ results.push({ journeyName: journey.name, testDir, testFiles: [], seedPath, status: 'skipped', seedStatus: 'none', warnings: notes, usage: usageSince(usage0) });
644
+ continue;
645
+ }
549
646
  if (!force &&
550
647
  !waitsOnCode &&
551
648
  cache[file] === hash &&
@@ -624,6 +721,20 @@ export async function runGenerate(docsDir, useAI = true, force = false, only, ar
624
721
  if (!memory || !pf.rules?.length)
625
722
  return content;
626
723
  const { mem, facts } = await fileMemory(pf);
724
+ // A blank name in a hook or helper makes the whole file wait. Only the
725
+ // setup is written again (one small call), never the tests; once per
726
+ // doc and code if it is refused.
727
+ if (kept && stale.has(filePath) && blankOutsideKind(content) === 'setup' && !(readRefused()[pf.slug] ?? []).includes('#setup')) {
728
+ const redone = await redoSetup(content, filePath, facts, mem?.helpers ?? helpers);
729
+ if (redone) {
730
+ warnings.push(`${pf.slug}: its setup (hooks and helpers) was written again from the code facts; its tests were kept`);
731
+ content = redone;
732
+ }
733
+ else {
734
+ warnings.push(`${pf.slug}: its setup could not be written again — the file keeps waiting on its names`);
735
+ rememberRefused(pf.slug, ['#setup']);
736
+ }
737
+ }
627
738
  const done = await completeFile(content, {
628
739
  kept, locked: passedTitles(filePath),
629
740
  redo: kept && stale.has(filePath) ? (redoAll ? () => true : (b) => usesBlankName(content, b)) : undefined,
@@ -636,15 +747,8 @@ export async function runGenerate(docsDir, useAI = true, force = false, only, ar
636
747
  });
637
748
  written.push({ slug: pf.slug, tested: done.tested, cannotTest: done.cannotTest, missing: done.missing, dropped: done.dropped });
638
749
  // What was asked and still has no test is not asked again for this doc and code.
639
- if (done.refusedRules.length) {
640
- try {
641
- const all = readRefused();
642
- all[pf.slug] = [...new Set([...(all[pf.slug] ?? []), ...done.refusedRules])];
643
- mkdirSync(dirname(refusedPath), { recursive: true });
644
- writeFileSync(refusedPath, JSON.stringify({ key: refusedKey(), rules: all }, null, 2), 'utf-8');
645
- }
646
- catch { /* best effort */ }
647
- }
750
+ if (done.refusedRules.length)
751
+ rememberRefused(pf.slug, done.refusedRules);
648
752
  for (const n of neededNames(done.content))
649
753
  if (!blankNames(`need('${n}')`).length)
650
754
  askedNames.add(n);
@@ -768,7 +872,7 @@ export async function runGenerate(docsDir, useAI = true, force = false, only, ar
768
872
  const stale = new Set();
769
873
  // Every test of a journey first written with no code is looked at again once its code is there.
770
874
  let redoAll = false;
771
- if (useAI && !notBuilt && blindBefore && testFiles0.length && (await grounding())) {
875
+ if (useAI && !notBuilt && opts.rewriteWaiting && blindBefore && testFiles0.length && (await grounding())) {
772
876
  for (const p of planned.map(pathOf))
773
877
  if (existsSync(p))
774
878
  stale.add(p);
@@ -776,7 +880,7 @@ export async function runGenerate(docsDir, useAI = true, force = false, only, ar
776
880
  if (redoAll)
777
881
  warnings.push(`its code is here now: the tests first written without it are written again from the code (tests that passed are kept)`);
778
882
  }
779
- if (useAI && !notBuilt && !redoAll) {
883
+ if (useAI && !notBuilt && !redoAll && opts.rewriteWaiting) {
780
884
  const waiting = planned.map(pathOf).filter((p) => { try {
781
885
  return existsSync(p) && blankNames(readFileSync(p, 'utf-8')).length > 0;
782
886
  }
@@ -789,12 +893,35 @@ export async function runGenerate(docsDir, useAI = true, force = false, only, ar
789
893
  if (waiting.length && (opts.refreshCode || codeMoved) && (await grounding())) {
790
894
  for (const p of waiting)
791
895
  stale.add(p);
792
- if (stale.size)
793
- warnings.push(`${stale.size} file(s) waiting on names the code now has: only their waiting tests are written again`);
896
+ if (stale.size) {
897
+ const kinds = [...stale].map((p) => { try {
898
+ return blankOutsideKind(readFileSync(p, 'utf-8'));
899
+ }
900
+ catch {
901
+ return undefined;
902
+ } });
903
+ const setup = kinds.filter((k) => k === 'setup').length;
904
+ const done = readRefused();
905
+ const whole = [...stale].filter((p, i) => kinds[i] === 'file' && !(done[planned.find((pf) => pathOf(pf) === p)?.slug ?? ''] ?? []).includes('#whole')).length;
906
+ warnings.push(`${stale.size} file(s) waiting on names the code now has: only their waiting tests are written again${setup ? `; the setup (hooks, helpers) of ${setup}` : ''}${whole ? `; ${whole} written again whole, once (their names sit in parametrised tables or loops)` : ''}`);
907
+ }
794
908
  }
795
909
  }
796
910
  // A doc without rule ids has no per-test redo: its waiting files are written again whole, as before.
797
- const rewriteWhole = (p) => stale.has(p) && !memory;
911
+ // Written again whole: a doc without rule ids (no per-test redo), or a file
912
+ // whose blank names sit where no test or setup block can be split out —
913
+ // once per doc and code, and never a file holding a test that passed.
914
+ const rewriteWhole = (p) => stale.has(p) && (!memory || (() => {
915
+ try {
916
+ const c = readFileSync(p, 'utf-8');
917
+ const slug = planned.find((pf) => pathOf(pf) === p)?.slug ?? '';
918
+ return blankOutsideKind(c) === 'file' && !(readRefused()[slug] ?? []).includes('#whole')
919
+ && !findTestBlocks(c).some((b) => passedTitles(p).has(b.title));
920
+ }
921
+ catch {
922
+ return false;
923
+ }
924
+ })());
798
925
  const keeps = (p) => {
799
926
  if (rewriteWhole(p))
800
927
  return false;
@@ -880,6 +1007,8 @@ export async function runGenerate(docsDir, useAI = true, force = false, only, ar
880
1007
  }
881
1008
  let content;
882
1009
  let status;
1010
+ if (memory && stale.has(filePath) && existsSync(filePath))
1011
+ rememberRefused(pf.slug, ['#whole']);
883
1012
  if (useAI) {
884
1013
  let draft = drafts.get(filePath);
885
1014
  let draftRefusal = draft === undefined ? undefined : isSyntaxValid(draft, ext) ? genGate(draft, filePath) : syntaxReason(draft, ext);
@@ -119,3 +119,90 @@ export function renameInBlocks(content, blocks, pairs) {
119
119
  });
120
120
  return { content: spliceBlocks(content, edits), replaced };
121
121
  }
122
+ /**
123
+ * Tests written when the writer took kit.insert's answer for a row
124
+ * (`const op = await kit.insert(…); op[ID_FIELD]` with ID_FIELD =
125
+ * need('inserted row id field')). kit.insert returns the id itself, so
126
+ * `op[ID_FIELD]` is `op`: rewritten here, the blank name goes, and no AI is
127
+ * asked to write the file again. Only variables assigned from kit.insert.
128
+ */
129
+ export function useInsertIds(content) {
130
+ const decl = /^[ \t]*(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*=\s*need\(\s*(['"`])([^'"`\n]*\binsert\w*\b[^'"`\n]*\bid\b[^'"`\n]*)\2\s*\);?[ \t]*\n/gim;
131
+ let out = content;
132
+ for (const m of [...content.matchAll(decl)]) {
133
+ const [line, name] = m;
134
+ const fromInsert = new Set([...out.matchAll(/\b([A-Za-z_$][\w$]*)\s*=\s*await\s+kit\.insert\(/g)].map((x) => x[1]));
135
+ const esc = name.replace(/\$/g, '\\$');
136
+ out = out.replace(new RegExp(`\\b([A-Za-z_$][\\w$]*)\\[${esc}\\]`, 'g'), (all, v) => (fromInsert.has(v) ? v : all));
137
+ // The constant goes only when nothing uses it any more.
138
+ if (!new RegExp(`\\b${esc}\\b`).test(out.replace(line, '')))
139
+ out = out.replace(line, '');
140
+ }
141
+ return out;
142
+ }
143
+ /** Index just past the bracket closing the one at `open` ((, { or [), skipping strings, templates and comments; -1 if none. */
144
+ export function closeBracket(src, open) {
145
+ const pairs = { '(': ')', '{': '}', '[': ']' };
146
+ const stack = [];
147
+ for (let i = open; i < src.length; i++) {
148
+ const c = src[i];
149
+ if (c === '/' && src[i + 1] === '/') {
150
+ const n = src.indexOf('\n', i);
151
+ if (n < 0)
152
+ return -1;
153
+ i = n;
154
+ continue;
155
+ }
156
+ if (c === '/' && src[i + 1] === '*') {
157
+ const n = src.indexOf('*/', i + 2);
158
+ if (n < 0)
159
+ return -1;
160
+ i = n + 1;
161
+ continue;
162
+ }
163
+ if (c === '"' || c === "'" || c === '`') {
164
+ for (i += 1; i < src.length && src[i] !== c; i++)
165
+ if (src[i] === '\\')
166
+ i += 1;
167
+ continue;
168
+ }
169
+ if (pairs[c])
170
+ stack.push(pairs[c]);
171
+ else if (c === ')' || c === '}' || c === ']') {
172
+ if (stack.pop() !== c)
173
+ return -1;
174
+ if (!stack.length)
175
+ return i + 1;
176
+ }
177
+ }
178
+ return -1;
179
+ }
180
+ /**
181
+ * The file's setup outside its tests: hooks (beforeAll/beforeEach/afterAll/
182
+ * afterEach) and helper functions (function x() {…}, const x = async () => {…}).
183
+ */
184
+ export function findSetupBlocks(content) {
185
+ const tests = findTestBlocks(content);
186
+ const inTest = (i) => tests.some((t) => i >= t.start && i < t.end);
187
+ const out = [];
188
+ const START = /^[ \t]*(?:(?:test\.)?(beforeAll|beforeEach|afterAll|afterEach)\s*\(|(?:async\s+)?function\s+([A-Za-z_$][\w$]*)\s*\(|(?:const|let)\s+([A-Za-z_$][\w$]*)\s*=\s*(?:async\s*)?(?:\([^)\n]*\)|[A-Za-z_$][\w$]*)\s*=>)/gm;
189
+ for (const m of content.matchAll(START)) {
190
+ const at = m.index;
191
+ if (inTest(at) || out.some((b) => at >= b.start && at < b.end))
192
+ continue;
193
+ let end = -1;
194
+ if (m[1])
195
+ end = closeBracket(content, content.indexOf('(', at));
196
+ else {
197
+ const arrow = m[3] ? content.indexOf('=>', at) : -1;
198
+ const brace = content.indexOf('{', m[2] ? closeBracket(content, content.indexOf('(', at)) : arrow);
199
+ end = brace < 0 ? -1 : closeBracket(content, brace);
200
+ }
201
+ if (end < 0)
202
+ continue;
203
+ if (content[end] === ';')
204
+ end += 1;
205
+ out.push({ start: at, end, name: m[1] ?? m[2] ?? m[3], text: content.slice(at, end) });
206
+ }
207
+ return out;
208
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@activate-core/test-forge",
3
- "version": "2.7.0",
3
+ "version": "2.7.1",
4
4
  "description": "AI-powered QA automation. Scans a project, drafts journey docs, and generates seeded Jest/Playwright tests. Works in any project, with any AI CLI.",
5
5
  "type": "module",
6
6
  "bin": {