From 69bcad7883d1691297ee2fef6c4197ef8a287888 Mon Sep 17 00:00:00 2001 From: Martin Leduc <31558169+DecimalTurn@users.noreply.github.com> Date: Sat, 14 Nov 2026 09:33:46 -0500 Subject: [PATCH 01/18] docs: update instructions for dependent data gathering --- scripts/report-dependents.mjs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/scripts/report-dependents.mjs b/scripts/report-dependents.mjs index b3c36e1a..2bbcb534 100644 --- a/scripts/report-dependents.mjs +++ b/scripts/report-dependents.mjs @@ -34,7 +34,13 @@ * The npm requests require network access. A names file is useful when the * npm dependents page is unavailable or when the report should cover a fixed * set of packages. Use `--refresh-names` to require a live npm refresh instead - * of falling back to cached names when npm is unavailable. + * of falling back to cached names when npm is unavailable. If the saved HTML + * page is stale, open the Dependents page in a browser, wait for it to finish + * loading, and save it again as to: + * + * docs/dependents/@decimalturn_toml-patch - npm.html + * + * Page URL: https://www.npmjs.com/package/@decimalturn/toml-patch?activeTab=dependents */ import { readFile, writeFile } from 'node:fs/promises'; import { execFile } from 'node:child_process'; From 2f99e841bc2946be2ed0669819a304dba1a09d85 Mon Sep 17 00:00:00 2001 From: Martin Leduc <31558169+DecimalTurn@users.noreply.github.com> Date: Sat, 14 Nov 2026 13:17:23 -0500 Subject: [PATCH 02/18] test: add issue 335 regression cases --- src/__tests__/patch.test.ts | 86 +++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/src/__tests__/patch.test.ts b/src/__tests__/patch.test.ts index f6046b63..d1c72f2d 100644 --- a/src/__tests__/patch.test.ts +++ b/src/__tests__/patch.test.ts @@ -4792,6 +4792,92 @@ describe('sub-table replacement known issues', () => { expect(patched).toBe(expected); }); + // See https://github.com/DecimalTurn/toml-patch/issues/335 + // Case 1: replace a sub-table that has its own [header] with a scalar + test('replaces a sub-table split between a dotted key and a deeper header', () => { + const existing = dedent` + [fruit] + apple.color = "red" + + [fruit.apple.texture] + smooth = true + ` + '\n'; + + const updated = parse(existing); + updated.fruit.apple.texture = 'smooth'; + + const patched = patch(existing, updated); + const expected = dedent` + [fruit] + apple.color = "red" + apple.texture = "smooth" + ` + '\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + + }); + + // See https://github.com/DecimalTurn/toml-patch/issues/335 + // Case 2: delete a table whose array-of-tables child has its own [[header]] + test('deletes dotted keys and deeper array-of-table headers with their parent table', () => { + const existing = dedent` + [fruit] + apple.color = "red" + + [[fruit.apple.seeds]] + size = 2 + ` + '\n'; + + const updated = parse(existing); + delete updated.fruit.apple; + + const patched = patch(existing, updated); + const expected = dedent` + [fruit] + ` + '\n'; + + expect(parse(patched)).toEqual(updated); + expect(patched).toBe(expected); + + }); + + // See https://github.com/DecimalTurn/toml-patch/issues/335 + // Case 3A: replace a two-level dotted table inside an inline table + test('removes sibling dotted keys when replacing a nested inline-table path', () => { + const existing = 'x = { a.b.c = 1, a.b.d = 2 }\n'; + const updated = parse(existing); + updated.x.a.b = [1, 2]; + + const patched = patch(existing, updated); + const expected = 'x = { a.b = [ 1, 2 ] }\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + + }); + + // See https://github.com/DecimalTurn/toml-patch/issues/335 + // Case 3B: replace a two-level dotted table in a block-table TOML document + test('removes sibling dotted keys when replacing a nested path in a block table', () => { + const existing = dedent` + [x] + a.b.c = 1 + a.b.d = 2 + ` + '\n'; + const updated = parse(existing); + updated.x.a.b = [1, 2]; + + const patched = patch(existing, updated); + const expected = dedent` + [x] + a.b = [ 1, 2 ] + ` + '\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + }); + // See https://github.com/DecimalTurn/toml-patch/issues/330 test.skip('separates replacement table array entries the same way whether the old sub-table key was explicit', () => { // Documents a feature not implemented yet: keeping the document's blank-line From f7171f098bb21e5c7f2122fbaf248d41bfd43dd4 Mon Sep 17 00:00:00 2001 From: Martin Leduc <31558169+DecimalTurn@users.noreply.github.com> Date: Sat, 14 Nov 2026 15:44:36 -0500 Subject: [PATCH 03/18] fix: replace mixed dotted sub-table under explicit parent (#335) Emit a scalar replacement as a dotted row in the existing explicit parent when dotted siblings already define the intermediate table. This avoids creating a conflicting child header. --- src/patch.ts | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/patch.ts b/src/patch.ts index 55f913f3..117357c6 100644 --- a/src/patch.ts +++ b/src/patch.ts @@ -2751,6 +2751,27 @@ function applyChanges( const tableParent = findStructuralParent(original, existing) ?? findParent(original, change.path); const aotOwner = findAotEntryForPath(original, change.path); + const explicitParent = findDeepestExplicitTable( + original, + existingTableKey, + aotOwner?.key.item.value.length ?? 0, + aotOwner + ); + if (explicitParent) { + const relativeKey = existingTableKey.slice(explicitParent.key.item.value.length); + const hasImplicitParent = relativeKey.length > 1 && + (explicitParent.items as TreeNode[]).some(item => + isKeyValue(item) && + item.key.value.length > 1 && + item.key.value[0] === relativeKey[0] + ); + if (hasImplicitParent && replaceBelowExplicitTable( + original, explicitParent, existingTableKey, jsValue, format, commentEligibleNodes, aotOwner + )) { + return; + } + } + // The table's key can be a strict prefix of OTHER document sections // (e.g. `[""]` collapsing to a scalar while `["".hv8lx]` is also a // document-level section extending that prefix). Those sections @@ -5451,7 +5472,8 @@ function replaceBelowExplicitTable( fullKey: string[], jsValue: any, format: TomlFormat, - commentEligibleNodes: WeakSet + commentEligibleNodes: WeakSet, + owner?: TableArray ): boolean { const relativeKey = fullKey.slice(parent.key.item.value.length); @@ -5468,7 +5490,7 @@ function replaceBelowExplicitTable( const sections = Array.from(parseTOML(toTOML(sectionDoc.items, format))); if (!sections.every(item => isTable(item) || isTableArray(item))) return false; - const toRemove = findDocumentItemsByKeyPrefix(document, fullKey); + const toRemove = findAotScopedDocumentItems(document, fullKey, owner); const removedIndex = toRemove.length > 0 ? document.items.indexOf(toRemove[0]) : -1; for (const node of toRemove) removeMember(document, document, node); for (const row of parentRows) removeMember(document, parent, row); @@ -5485,7 +5507,7 @@ function replaceBelowExplicitTable( const row = generateExplicitParentRow(relativeKey, jsValue, format); if (!row) return false; - for (const node of findDocumentItemsByKeyPrefix(document, fullKey)) { + for (const node of findAotScopedDocumentItems(document, fullKey, owner)) { removeMember(document, document, node); } upsertExplicitParentRow(document, parent, relativeKey, row); From d1bccb4266e39f2f3b5afa49d37806b4d86a0dff Mon Sep 17 00:00:00 2001 From: Martin Leduc <31558169+DecimalTurn@users.noreply.github.com> Date: Sat, 14 Nov 2026 15:44:57 -0500 Subject: [PATCH 04/18] fix: remove section descendants with dotted parent (#335) When a removal path prefix-matches a dotted key inside a table, also remove document-level headers under that prefix. This prevents array-of-table children from recreating deleted data. --- src/patch.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/patch.ts b/src/patch.ts index 117357c6..c0b7750a 100644 --- a/src/patch.ts +++ b/src/patch.ts @@ -3480,6 +3480,7 @@ function applyChanges( for (const sibling of toRemove) { removeMember(original, parent, sibling); } + removeDocumentItemsExtendingPath(original, change.path, node); } } @@ -5608,7 +5609,7 @@ function removeSiblingsExtendingPrefix( } } -function removeDocumentItemsExtendingPath(document: Document, path: Path, keep: Block): void { +function removeDocumentItemsExtendingPath(document: Document, path: Path, keep: TreeNode): void { const cstPath = stripAotEntryIndices(document, path); const owner = findAotEntryForPath(document, path); for (const item of findAotScopedDocumentItems(document, cstPath, owner)) { From a1cd2371e7c51e24342c334a35cabed3c4663148 Mon Sep 17 00:00:00 2001 From: Martin Leduc <31558169+DecimalTurn@users.noreply.github.com> Date: Sat, 14 Nov 2026 15:45:10 -0500 Subject: [PATCH 05/18] fix: resolve dotted prefixes inside inline tables (#335) Resolve a prefix-matched InlineItem to its structural container, allowing replacement logic to remove sibling dotted rows before writing the collapsed key. --- src/patch.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/patch.ts b/src/patch.ts index c0b7750a..c83bf6e6 100644 --- a/src/patch.ts +++ b/src/patch.ts @@ -2356,7 +2356,8 @@ function applyChanges( // sweeps below must look in the REAL container (the AOT entry) or stale // children survive the collapse and re-parse fails with "Value already // defined" (fuzz seed 1674968). Resolve it structurally. - if (containerParent && isKeyValue(containerParent) && + if (containerParent && + (isKeyValue(containerParent) || isInlineItem(containerParent)) && isPrefixMatchedNode(containerParent, change.path.slice(0, -1))) { containerParent = findStructuralParent(original, containerParent); } From 59cd7af0aa6dccf8e94bb51937be8f3130c780be Mon Sep 17 00:00:00 2001 From: Martin Leduc <31558169+DecimalTurn@users.noreply.github.com> Date: Sat, 14 Nov 2026 15:38:23 -0500 Subject: [PATCH 06/18] test: report fuzz5 sweep progress Record the clean seed sweep and document fuzz5 coverage boundaries. --- docs/Fuzz-Testing.md | 12 +++++++++--- scripts/fuzz-run5.ts | 10 ++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/docs/Fuzz-Testing.md b/docs/Fuzz-Testing.md index 3efc738c..dcceddb3 100644 --- a/docs/Fuzz-Testing.md +++ b/docs/Fuzz-Testing.md @@ -107,7 +107,13 @@ general randomizer rarely creates the AOT ownership layouts it targets. Its default is one mutation per seed. Every four consecutive seeds cover all four mutation classes, while keeping failures easy to reproduce. Passing `--mutations 3` applies additional deterministic mutations after the - seed-selected first operation. + seed-selected first operation. It does not generate dotted-key assignments + or inline tables, delete arbitrary table properties, or replace a table + property with a scalar. Those are the key shapes and operations in + [issue #335](https://github.com/DecimalTurn/toml-patch/issues/335). +- **Latest clean sweep:** On 2026-11-14, seeds 0 through 100,000 inclusive + (100,001 seeds, one mutation per seed, `--fast-fail`) completed with zero + failures. ### Patch-lite fuzzing @@ -224,7 +230,8 @@ pnpm run fuzz5 The default is one mutation per seed, so each failure points to the seed's selected operation. Passing `--mutations 3` to `scripts/fuzz-run5.ts` applies -two more deterministic AOT mutations after that first operation. +two more deterministic AOT mutations after that first operation. Pass +`--progress N` to report the scanned seed range every N seeds. This runner behavior differs from the composable profiles above: `aot` and `aot-format` always remove an AOT entry first, while `aot-general` uses the @@ -515,4 +522,3 @@ The sweep-notes doc **must be committed** too — write it to `docs: add sweep notes for -` message. A *clean* window (0 failures) gets no notes file, but a window that needed fixes gets one, listing each seed, its distilled minimal repro, and the root cause + fix commit. - diff --git a/scripts/fuzz-run5.ts b/scripts/fuzz-run5.ts index 1e15d78b..18f0bf9f 100644 --- a/scripts/fuzz-run5.ts +++ b/scripts/fuzz-run5.ts @@ -2,6 +2,8 @@ // // Run a range: // npx -y tsx scripts/fuzz-run5.ts --seed 0 --to 9999 --mutations 1 +// Report progress every 100 seeds: +// npx -y tsx scripts/fuzz-run5.ts --seed 0 --to 9999 --mutations 1 --progress 100 // Stop at the first failure: // npx -y tsx scripts/fuzz-run5.ts --seed 0 --to 9999 --mutations 1 --fast-fail import { fuzzOne5 } from '../src/__tests__/fuzz-patch5'; @@ -16,13 +18,21 @@ const hasFlag = (name: string) => args.includes(`--${name}`); const seed = param('seed', 0); const to = param('to', seed); const mutations = param('mutations', 1); +const progress = param('progress', 0); const fastFail = hasFlag('fast-fail'); const failures: string[] = []; let scanned = 0; +if (!Number.isInteger(progress) || progress < 0) { + throw new Error('--progress must be a non-negative integer.'); +} + for (let current = seed; current <= to; current++) { scanned++; const result = fuzzOne5(current, mutations); + if (progress > 0 && scanned % progress === 0) { + console.log(`PROGRESS: scanned ${scanned} seeds (${seed}..${current})`); + } if (result.status !== 'ok') { const line = `SEED ${current} : ${result.status}${result.error ? ` | ${result.error}` : ''}`; failures.push(line); From a6598c75f6a163c9395bd7e628d7e606fb1699f5 Mon Sep 17 00:00:00 2001 From: Martin Leduc <31558169+DecimalTurn@users.noreply.github.com> Date: Sat, 14 Nov 2026 16:16:03 -0500 Subject: [PATCH 07/18] test: add structural fuzzing for replacements Add targeted fuzz6 coverage for dotted keys, AOT deletions, and inline-table replacements. --- docs/Fuzz-Testing.md | 20 ++++ package.json | 1 + scripts/fuzz-run6.ts | 44 +++++++++ src/__tests__/fuzz-patch6.ts | 174 +++++++++++++++++++++++++++++++++++ src/__tests__/fuzz6.test.ts | 23 +++++ 5 files changed, 262 insertions(+) create mode 100644 scripts/fuzz-run6.ts create mode 100644 src/__tests__/fuzz-patch6.ts create mode 100644 src/__tests__/fuzz6.test.ts diff --git a/docs/Fuzz-Testing.md b/docs/Fuzz-Testing.md index dcceddb3..9bdc3d1c 100644 --- a/docs/Fuzz-Testing.md +++ b/docs/Fuzz-Testing.md @@ -115,6 +115,26 @@ general randomizer rarely creates the AOT ownership layouts it targets. (100,001 seeds, one mutation per seed, `--fast-fail`) completed with zero failures. +### Variant 6: deletion and structural replacement fuzzing + +- **Implementation:** `src/__tests__/fuzz-patch6.ts` +- **Runner:** `scripts/fuzz-run6.ts`, `pnpm run fuzz6` +- **Purpose:** Generates block tables with dotted-key assignments, tables with + nested AOT descendants, and inline tables with overlapping dotted paths. The + seed rotates among replacing a header-defined table with a scalar, deleting + a table that owns an AOT descendant, and replacing a nested inline-table + path. +- **Why it exists:** These source shapes and edits target the failures reported + in [issue #335](https://github.com/DecimalTurn/toml-patch/issues/335), which + the AOT-only generator in variant 5 cannot produce. +- **Reproducing the three targeted cases:** Seeds 0, 1 and 2 select each case + once. Use `--to 2` to run all three without stopping at the first failure. +- **Coverage boundary:** Variant 6 focuses on structural deletion and + replacement cases. A clean run is evidence only for the generated source + shapes and mutations, not a proof that every structural edit is correct. +- **Latest clean sweep:** On 2026-11-14, seeds 0 through 100,000 inclusive + (100,001 seeds, one mutation per seed) passed after rebasing onto `fix-335`. + ### Patch-lite fuzzing - **Implementation:** `src/__tests__/fuzz-patch-lite.ts` diff --git a/package.json b/package.json index 8cdd6220..d42cbe3c 100644 --- a/package.json +++ b/package.json @@ -65,6 +65,7 @@ "fuzz3": "npx -y tsx scripts/fuzz-run3.ts --seed 0 --to 9999 --mutations 3", "fuzz4": "npx -y tsx scripts/fuzz-run4.ts --seed 0 --to 9999 --mutations 3", "fuzz5": "npx -y tsx scripts/fuzz-run5.ts --seed 0 --to 9999 --mutations 1 --progress 100", + "fuzz6": "npx -y tsx scripts/fuzz-run6.ts --seed 0 --to 9999 --mutations 1 --progress 100", "bench:bundle-size": "node benchmark/bundle-size.mjs", "bench:parse": "node benchmark/parse-benchmark.mjs", "bench:stringify": "node benchmark/stringify-benchmark.mjs", diff --git a/scripts/fuzz-run6.ts b/scripts/fuzz-run6.ts new file mode 100644 index 00000000..c21de9d2 --- /dev/null +++ b/scripts/fuzz-run6.ts @@ -0,0 +1,44 @@ +// Fuzz-runner variant for dotted-key/header overlaps and structural replacements. +// +// Run an inclusive range: +// npx -y tsx scripts/fuzz-run6.ts --seed 0 --to 9999 --mutations 1 +// Stop at the first failure: +// npx -y tsx scripts/fuzz-run6.ts --seed 0 --to 9999 --mutations 1 --fast-fail +import { fuzzOne6 } from '../src/__tests__/fuzz-patch6'; + +const args = process.argv.slice(2); +const param = (name: string, fallback: number) => { + const index = args.indexOf(`--${name}`); + return index >= 0 ? Number(args[index + 1]) : fallback; +}; +const hasFlag = (name: string) => args.includes(`--${name}`); + +const seed = param('seed', 0); +const to = param('to', seed); +const mutations = param('mutations', 1); +const progress = param('progress', 0); +const fastFail = hasFlag('fast-fail'); +const failures: string[] = []; +let scanned = 0; + +if (!Number.isInteger(progress) || progress < 0) { + throw new Error('--progress must be a non-negative integer.'); +} + +for (let current = seed; current <= to; current++) { + scanned++; + const result = fuzzOne6(current, mutations); + if (progress > 0 && scanned % progress === 0) { + console.log(`PROGRESS: scanned ${scanned} seeds (${seed}..${current})`); + } + if (result.status !== 'ok') { + const line = `SEED ${current} : ${result.status}${result.error ? ` | ${result.error.split('\n')[0]}` : ''}`; + failures.push(line); + console.log(line); + if (fastFail) break; + } +} + +const end = seed + scanned - 1; +console.log(`FAILURES: ${failures.length} (scanned ${scanned} seeds: ${seed}..${end})`); +if (failures.length > 0) process.exit(1); diff --git a/src/__tests__/fuzz-patch6.ts b/src/__tests__/fuzz-patch6.ts new file mode 100644 index 00000000..35c1234d --- /dev/null +++ b/src/__tests__/fuzz-patch6.ts @@ -0,0 +1,174 @@ +/** + * Structure-first patch fuzzer for deletions and table replacements. + * + * Each seed targets dotted-key/header overlap, deleting a table that owns an + * AOT descendant, or replacing a nested inline-table path. + */ +import { SeededRandom } from './randomizer'; +import { + deepEqual, + runFuzz, + type FuzzContext, + type GeneratedSource, + type MutationPlan, + type MutationRun, + type PatchFuzzResult +} from './fuzz-patch'; + +type StructuralScenario = + | { + kind: 'replace-header-table'; + path: string[]; + replacement: string; + source: string; + } + | { + kind: 'delete-table-with-aot'; + path: string[]; + source: string; + } + | { + kind: 'replace-inline-table'; + path: string[]; + replacement: number[]; + source: string; + }; + +function createScenario(seed: number, mutationIndex: number): StructuralScenario { + const rng = new SeededRandom(seed + mutationIndex * 100003 + 6000000); + const suffix = `${Math.abs(seed).toString(36)}_${mutationIndex.toString(36)}`; + const scenarioKind = ((seed + mutationIndex) % 3 + 3) % 3; + + if (scenarioKind === 0) { + const root = `table_${suffix}`; + const child = `child_${suffix}`; + const sibling = `sibling_${suffix}`; + const target = `target_${suffix}`; + const replacement = `value_${suffix}_${rng.nextInt(1000)}`; + const source = [ + `[${root}]`, + `${child}.${sibling} = ${JSON.stringify(`keep_${suffix}`)}`, + '', + `[${root}.${child}.${target}]`, + `value = ${rng.nextRange(-999, 999)}` + ].join('\n') + '\n'; + + return { + kind: 'replace-header-table', + path: [root, child, target], + replacement, + source + }; + } + + if (scenarioKind === 1) { + const root = `table_${suffix}`; + const child = `child_${suffix}`; + const sibling = `sibling_${suffix}`; + const aot = `items_${suffix}`; + const source = [ + `[${root}]`, + `${child}.${sibling} = ${JSON.stringify(`keep_${suffix}`)}`, + '', + `[[${root}.${child}.${aot}]]`, + `value = ${rng.nextRange(-999, 999)}` + ].join('\n') + '\n'; + + return { + kind: 'delete-table-with-aot', + path: [root, child], + source + }; + } + + const root = `inline_${suffix}`; + const child = `child_${suffix}`; + const target = `target_${suffix}`; + const left = `left_${suffix}`; + const right = `right_${suffix}`; + const replacement = [rng.nextRange(-99, 99), rng.nextRange(-99, 99)]; + const source = `${root} = { ${child}.${target}.${left} = ${rng.nextRange(-999, 999)}, ${child}.${target}.${right} = ${rng.nextRange(-999, 999)} }\n`; + + return { + kind: 'replace-inline-table', + path: [root, child, target], + replacement, + source + }; +} + +function recordAtPath(value: unknown, path: string[]): Record { + let current = value; + for (let index = 0; index < path.length; index++) { + if (!current || typeof current !== 'object' || Array.isArray(current) || current instanceof Date) { + throw new Error(`Expected a table at ${path.slice(0, index).join('.') || ''}.`); + } + current = (current as Record)[path[index]]; + } + + if (!current || typeof current !== 'object' || Array.isArray(current) || current instanceof Date) { + throw new Error(`Expected a table at ${path.join('.') || ''}.`); + } + return current as Record; +} + +function generateStructuralSource( + { seed, mutationCount }: Pick +): GeneratedSource { + const scenarios = Array.from({ length: mutationCount }, (_, index) => createScenario(seed, index)); + return { + source: scenarios.map(scenario => scenario.source).join('\n'), + description: 'dotted-key, AOT-deletion, and inline-table structural cases' + }; +} + +function mutateStructuralCases({ + updated, + context +}: { + original: unknown; + updated: unknown; + context: FuzzContext; +}): MutationRun { + const descriptions: string[] = []; + const plans: MutationPlan[] = []; + + for (let index = 0; index < context.mutationCount; index++) { + const scenario = createScenario(context.seed, index); + const path = scenario.path; + const parent = recordAtPath(updated, path.slice(0, -1)); + const key = path[path.length - 1]; + + switch (scenario.kind) { + case 'replace-header-table': + parent[key] = scenario.replacement; + descriptions.push(`replace header-defined table with scalar at ${path.join('.')}`); + plans.push({ kind: scenario.kind, path, newValue: scenario.replacement }); + break; + case 'delete-table-with-aot': + delete parent[key]; + descriptions.push(`delete table with nested AOT descendant at ${path.join('.')}`); + plans.push({ kind: scenario.kind, path }); + break; + case 'replace-inline-table': + parent[key] = scenario.replacement; + descriptions.push(`replace inline table subtree with array at ${path.join('.')}`); + plans.push({ kind: scenario.kind, path, newValue: scenario.replacement }); + break; + } + } + + return { updated, descriptions, plans }; +} + +export function fuzzOne6(seed: number, mutationCount: number): PatchFuzzResult { + return runFuzz(seed, { + mutationCount, + generateSource: generateStructuralSource, + generateFormat: () => undefined, + mutate: mutateStructuralCases, + compare: deepEqual, + variant: '6', + profile: 'structural-replacements' + }); +} diff --git a/src/__tests__/fuzz6.test.ts b/src/__tests__/fuzz6.test.ts new file mode 100644 index 00000000..e1789cde --- /dev/null +++ b/src/__tests__/fuzz6.test.ts @@ -0,0 +1,23 @@ +import { fuzzOne6 } from './fuzz-patch6'; + +test('fuzz6 rotates through the three targeted structural mutations', () => { + const results = [0, 1, 2].map(seed => fuzzOne6(seed, 1)); + + expect(results.map(result => result.mutationDescs?.[0]?.split(' at ')[0])).toEqual([ + 'replace header-defined table with scalar', + 'delete table with nested AOT descendant', + 'replace inline table subtree with array' + ]); +}); + +test('fuzz6 seed 0 round-trips replacing a header-defined table with a scalar', () => { + expect(fuzzOne6(0, 1).status).toBe('ok'); +}); + +test('fuzz6 seed 1 round-trips deleting a table that owns an AOT descendant', () => { + expect(fuzzOne6(1, 1).status).toBe('ok'); +}); + +test('fuzz6 seed 2 round-trips replacing a nested inline-table path', () => { + expect(fuzzOne6(2, 1).status).toBe('ok'); +}); From 665c8552719a426e693dbadcb902b3f57e91569d Mon Sep 17 00:00:00 2001 From: Martin Leduc <31558169+DecimalTurn@users.noreply.github.com> Date: Sat, 14 Nov 2026 16:28:24 -0500 Subject: [PATCH 08/18] test: add related tests --- src/__tests__/patch.test.ts | 95 +++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/src/__tests__/patch.test.ts b/src/__tests__/patch.test.ts index d1c72f2d..510b335b 100644 --- a/src/__tests__/patch.test.ts +++ b/src/__tests__/patch.test.ts @@ -4878,6 +4878,101 @@ describe('sub-table replacement known issues', () => { expect(parse(patched)).toEqual(updated); }); + // Follow-up ownership and nesting cases for issue #335. + describe('issue 335 follow-up ownership and nesting cases', () => { + test('replaces a dotted/header-defined sub-table in only the selected AOT entry', () => { + const existing = dedent` + [[fruit]] + id = "red" + apple.color = "red" + + [fruit.apple.texture] + smooth = true + + [[fruit]] + id = "green" + apple.color = "green" + + [fruit.apple.texture] + smooth = false + ` + '\n'; + + const updated = parse(existing); + updated.fruit[0].apple.texture = 'matte'; + + const patched = patch(existing, updated); + const expected = dedent` + [[fruit]] + id = "red" + apple.color = "red" + apple.texture = "matte" + + [[fruit]] + id = "green" + apple.color = "green" + + [fruit.apple.texture] + smooth = false + ` + '\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + }); + + test('deletes a dotted parent and its nested AOT only from the selected entry', () => { + const existing = dedent` + [[fruit]] + id = "red" + apple.color = "red" + + [[fruit.apple.seeds]] + size = 1 + + [[fruit]] + id = "green" + apple.color = "green" + + [[fruit.apple.seeds]] + size = 2 + ` + '\n'; + + const updated = parse(existing); + delete updated.fruit[0].apple; + + const patched = patch(existing, updated); + const expected = dedent` + [[fruit]] + id = "red" + + [[fruit]] + id = "green" + apple.color = "green" + + [[fruit.apple.seeds]] + size = 2 + ` + '\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + }); + + test('replaces dotted inline-table children in one array element without changing siblings', () => { + const existing = + 'items = [{ id = "a", opts.deep.left = 1, opts.deep.right = 2 }, ' + + '{ id = "b", opts.deep.left = 3, opts.deep.right = 4 }]\n'; + const updated = parse(existing); + updated.items[0].opts.deep = [1, 2]; + + const patched = patch(existing, updated); + const expected = + 'items = [{ id = "a", opts.deep = [ 1, 2 ] }, ' + + '{ id = "b", opts.deep.left = 3, opts.deep.right = 4 }]\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + }); + }); + // See https://github.com/DecimalTurn/toml-patch/issues/330 test.skip('separates replacement table array entries the same way whether the old sub-table key was explicit', () => { // Documents a feature not implemented yet: keeping the document's blank-line From 9cccdf358a9c1e0d7a594d48093104a8258ab758 Mon Sep 17 00:00:00 2001 From: Martin Leduc <31558169+DecimalTurn@users.noreply.github.com> Date: Sat, 14 Nov 2026 16:32:51 -0500 Subject: [PATCH 09/18] test: group issue 335 follow-up cases --- src/__tests__/patch.test.ts | 134 ++++++++++++++++++------------------ 1 file changed, 67 insertions(+), 67 deletions(-) diff --git a/src/__tests__/patch.test.ts b/src/__tests__/patch.test.ts index 510b335b..d927c089 100644 --- a/src/__tests__/patch.test.ts +++ b/src/__tests__/patch.test.ts @@ -4881,95 +4881,95 @@ describe('sub-table replacement known issues', () => { // Follow-up ownership and nesting cases for issue #335. describe('issue 335 follow-up ownership and nesting cases', () => { test('replaces a dotted/header-defined sub-table in only the selected AOT entry', () => { - const existing = dedent` - [[fruit]] - id = "red" - apple.color = "red" + const existing = dedent` + [[fruit]] + id = "red" + apple.color = "red" - [fruit.apple.texture] - smooth = true + [fruit.apple.texture] + smooth = true - [[fruit]] - id = "green" - apple.color = "green" + [[fruit]] + id = "green" + apple.color = "green" - [fruit.apple.texture] - smooth = false - ` + '\n'; + [fruit.apple.texture] + smooth = false + ` + '\n'; - const updated = parse(existing); - updated.fruit[0].apple.texture = 'matte'; + const updated = parse(existing); + updated.fruit[0].apple.texture = 'matte'; - const patched = patch(existing, updated); - const expected = dedent` - [[fruit]] - id = "red" - apple.color = "red" - apple.texture = "matte" + const patched = patch(existing, updated); + const expected = dedent` + [[fruit]] + id = "red" + apple.color = "red" + apple.texture = "matte" - [[fruit]] - id = "green" - apple.color = "green" + [[fruit]] + id = "green" + apple.color = "green" - [fruit.apple.texture] - smooth = false - ` + '\n'; + [fruit.apple.texture] + smooth = false + ` + '\n'; - expect(patched).toBe(expected); - expect(parse(patched)).toEqual(updated); + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); }); test('deletes a dotted parent and its nested AOT only from the selected entry', () => { - const existing = dedent` - [[fruit]] - id = "red" - apple.color = "red" + const existing = dedent` + [[fruit]] + id = "red" + apple.color = "red" - [[fruit.apple.seeds]] - size = 1 + [[fruit.apple.seeds]] + size = 1 - [[fruit]] - id = "green" - apple.color = "green" + [[fruit]] + id = "green" + apple.color = "green" - [[fruit.apple.seeds]] - size = 2 - ` + '\n'; + [[fruit.apple.seeds]] + size = 2 + ` + '\n'; - const updated = parse(existing); - delete updated.fruit[0].apple; + const updated = parse(existing); + delete updated.fruit[0].apple; - const patched = patch(existing, updated); - const expected = dedent` - [[fruit]] - id = "red" + const patched = patch(existing, updated); + const expected = dedent` + [[fruit]] + id = "red" - [[fruit]] - id = "green" - apple.color = "green" + [[fruit]] + id = "green" + apple.color = "green" - [[fruit.apple.seeds]] - size = 2 - ` + '\n'; + [[fruit.apple.seeds]] + size = 2 + ` + '\n'; - expect(patched).toBe(expected); - expect(parse(patched)).toEqual(updated); + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); }); test('replaces dotted inline-table children in one array element without changing siblings', () => { - const existing = - 'items = [{ id = "a", opts.deep.left = 1, opts.deep.right = 2 }, ' + - '{ id = "b", opts.deep.left = 3, opts.deep.right = 4 }]\n'; - const updated = parse(existing); - updated.items[0].opts.deep = [1, 2]; - - const patched = patch(existing, updated); - const expected = - 'items = [{ id = "a", opts.deep = [ 1, 2 ] }, ' + - '{ id = "b", opts.deep.left = 3, opts.deep.right = 4 }]\n'; - - expect(patched).toBe(expected); - expect(parse(patched)).toEqual(updated); + const existing = + 'items = [{ id = "a", opts.deep.left = 1, opts.deep.right = 2 }, ' + + '{ id = "b", opts.deep.left = 3, opts.deep.right = 4 }]\n'; + const updated = parse(existing); + updated.items[0].opts.deep = [1, 2]; + + const patched = patch(existing, updated); + const expected = + 'items = [{ id = "a", opts.deep = [ 1, 2 ] }, ' + + '{ id = "b", opts.deep.left = 3, opts.deep.right = 4 }]\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); }); }); From 27a02a746533d82026fe6f1fb6771f8e1ba5ac27 Mon Sep 17 00:00:00 2001 From: Martin Leduc <31558169+DecimalTurn@users.noreply.github.com> Date: Sat, 14 Nov 2026 16:33:03 -0500 Subject: [PATCH 10/18] fix: keep AOT sub-table replacements in entry --- src/patch.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/patch.ts b/src/patch.ts index c83bf6e6..5cfda296 100644 --- a/src/patch.ts +++ b/src/patch.ts @@ -2807,6 +2807,15 @@ function applyChanges( commentEligibleNodes.add(freshKV); return; } + + const relativeKey = existingTableKey.slice(aotOwner.key.item.value.length); + const scopedRow = generateExplicitParentRow(relativeKey, jsValue, format); + if (scopedRow) { + removeMember(original, tableParent, existing); + insert(original, aotOwner, scopedRow, undefined); + commentEligibleNodes.add(scopedRow); + return; + } } if (parentKey.length > 0) { From b6c395562a32573df2cfd47838df9c92306a4c26 Mon Sep 17 00:00:00 2001 From: Martin Leduc <31558169+DecimalTurn@users.noreply.github.com> Date: Sat, 14 Nov 2026 16:37:09 -0500 Subject: [PATCH 11/18] fix: limit AOT replacements to implicit dotted parents --- src/patch.ts | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/patch.ts b/src/patch.ts index 5cfda296..e32c1361 100644 --- a/src/patch.ts +++ b/src/patch.ts @@ -2808,13 +2808,22 @@ function applyChanges( return; } - const relativeKey = existingTableKey.slice(aotOwner.key.item.value.length); - const scopedRow = generateExplicitParentRow(relativeKey, jsValue, format); - if (scopedRow) { - removeMember(original, tableParent, existing); - insert(original, aotOwner, scopedRow, undefined); - commentEligibleNodes.add(scopedRow); - return; + const relativeParentKey = parentKey.slice(aotOwner.key.item.value.length); + const hasImplicitAotParent = relativeParentKey.length > 0 && + (aotOwner.items as TreeNode[]).some(item => + isKeyValue(item) && + item.key.value.length > relativeParentKey.length && + keyStartsWith(item.key.value, relativeParentKey) + ); + if (hasImplicitAotParent) { + const relativeKey = existingTableKey.slice(aotOwner.key.item.value.length); + const scopedRow = generateExplicitParentRow(relativeKey, jsValue, format); + if (scopedRow) { + removeMember(original, tableParent, existing); + insert(original, aotOwner, scopedRow, undefined); + commentEligibleNodes.add(scopedRow); + return; + } } } From b0d52f04c3aedcdd0e66471dd8fc7dd11f4c6125 Mon Sep 17 00:00:00 2001 From: Martin Leduc <31558169+DecimalTurn@users.noreply.github.com> Date: Sat, 14 Nov 2026 16:37:34 -0500 Subject: [PATCH 12/18] test: expect compact nested array layout --- src/__tests__/patch.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/__tests__/patch.test.ts b/src/__tests__/patch.test.ts index d927c089..d17ceb9e 100644 --- a/src/__tests__/patch.test.ts +++ b/src/__tests__/patch.test.ts @@ -4965,7 +4965,7 @@ describe('sub-table replacement known issues', () => { const patched = patch(existing, updated); const expected = - 'items = [{ id = "a", opts.deep = [ 1, 2 ] }, ' + + 'items = [{ id = "a", opts.deep = [1, 2] }, ' + '{ id = "b", opts.deep.left = 3, opts.deep.right = 4 }]\n'; expect(patched).toBe(expected); From 23509be8f1b242b20a34179e11c14a42e940ca9c Mon Sep 17 00:00:00 2001 From: Martin Leduc <31558169+DecimalTurn@users.noreply.github.com> Date: Sat, 14 Nov 2026 17:48:29 -0500 Subject: [PATCH 13/18] docs: update changelog --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 541937b4..ddbbd50a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Patching: correctly replace and delete tables split across dotted keys and headers, including nested array-of-tables and inline-table cases. ([#335]) + ## [3.3.2] - 2026-10-31 ### Fixed @@ -530,5 +534,6 @@ This first forked version from [timhall/toml-patch](https://github.com/timhall/t [#329]: https://github.com/DecimalTurn/toml-patch/pull/329 [#332]: https://github.com/DecimalTurn/toml-patch/pull/332 [#333]: https://github.com/DecimalTurn/toml-patch/pull/333 +[#335]: https://github.com/DecimalTurn/toml-patch/issues/335 [0e66e68]: https://github.com/DecimalTurn/toml-patch/commit/0e66e68cbf42a07bc44445e46c3ea7bea97f95c1 [f97d571]: https://github.com/DecimalTurn/toml-patch/commit/f97d57194952b6603f0fcd686c32d3ae52a2c903 From df0b464f0532154f062ea309fa733f1504661c79 Mon Sep 17 00:00:00 2001 From: Martin Leduc <31558169+DecimalTurn@users.noreply.github.com> Date: Sat, 14 Nov 2026 18:57:44 -0500 Subject: [PATCH 14/18] test: add few more --- src/__tests__/patch.test.ts | 181 ++++++++++++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) diff --git a/src/__tests__/patch.test.ts b/src/__tests__/patch.test.ts index d17ceb9e..2b71e8bd 100644 --- a/src/__tests__/patch.test.ts +++ b/src/__tests__/patch.test.ts @@ -4971,6 +4971,187 @@ describe('sub-table replacement known issues', () => { expect(patched).toBe(expected); expect(parse(patched)).toEqual(updated); }); + + test('replaces a dotted/header-defined sub-table in the second AOT entry only', () => { + const existing = dedent` + [[fruit]] + id = "red" + + [fruit.apple.texture] + smooth = true + + [[fruit]] + id = "green" + apple.color = "green" + + [fruit.apple.texture] + smooth = false + ` + '\n'; + + const updated = parse(existing); + updated.fruit[1].apple.texture = 'matte'; + + const patched = patch(existing, updated); + const expected = dedent` + [[fruit]] + id = "red" + + [fruit.apple.texture] + smooth = true + + [[fruit]] + id = "green" + apple.color = "green" + apple.texture = "matte" + ` + '\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + }); + + test('deletes dotted and header-defined descendants only from the selected AOT entry', () => { + const existing = dedent` + [[fruit]] + id = "red" + apple.color = "red" + + [fruit.apple.texture] + smooth = true + + [[fruit.apple.seeds]] + size = 1 + + [[fruit]] + id = "green" + apple.color = "green" + + [fruit.apple.texture] + smooth = false + + [[fruit.apple.seeds]] + size = 2 + ` + '\n'; + + const updated = parse(existing); + delete updated.fruit[1].apple; + + const patched = patch(existing, updated); + const expected = dedent` + [[fruit]] + id = "red" + apple.color = "red" + + [fruit.apple.texture] + smooth = true + + [[fruit.apple.seeds]] + size = 1 + + [[fruit]] + id = "green" + ` + '\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + }); + + test('replaces dotted inline-table children in the middle array element only', () => { + const existing = + 'items = [{ id = "a", opts.deep.left = 1, opts.deep.right = 2 }, ' + + '{ id = "b", opts.deep.left = 3, opts.deep.right = 4 }, ' + + '{ id = "c", opts.deep.left = 5, opts.deep.right = 6 }]\n'; + const updated = parse(existing); + updated.items[1].opts.deep = [7, 8]; + + const patched = patch(existing, updated); + const expected = + 'items = [{ id = "a", opts.deep.left = 1, opts.deep.right = 2 }, ' + + '{ id = "b", opts.deep = [7, 8] }, ' + + '{ id = "c", opts.deep.left = 5, opts.deep.right = 6 }]\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + }); + + test('replaces a table before an out-of-order sibling header', () => { + const existing = dedent` + [fruit.apple] + name = "Fuji" + [animal] + name = "fox" + [fruit.orange] + name = "Navel" + ` + '\n'; + + const updated = parse(existing); + updated.fruit.apple = 'Fuji'; + + const patched = patch(existing, updated); + const expected = dedent` + [fruit] + apple = "Fuji" + [animal] + name = "fox" + [fruit.orange] + name = "Navel" + ` + '\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + }); + + test('replaces an out-of-order nested table without moving its sibling headers', () => { + const existing = dedent` + [fruit.apple] + name = "Fuji" + [animal] + name = "fox" + [fruit.apple.texture] + smooth = true + [fruit.orange] + name = "Navel" + ` + '\n'; + + const updated = parse(existing); + updated.fruit.apple.texture = 'matte'; + + const patched = patch(existing, updated); + const expected = dedent` + [fruit.apple] + name = "Fuji" + texture = "matte" + [animal] + name = "fox" + [fruit.orange] + name = "Navel" + ` + '\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + }); + + test('deletes out-of-order descendants without removing unrelated headers', () => { + const existing = dedent` + [fruit.apple] + name = "Fuji" + [animal] + name = "fox" + [fruit.orange] + name = "Navel" + ` + '\n'; + + const updated = parse(existing); + delete updated.fruit; + + const patched = patch(existing, updated); + const expected = dedent` + [animal] + name = "fox" + ` + '\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + }); }); // See https://github.com/DecimalTurn/toml-patch/issues/330 From 9f9dbc8a8026a18c00eaae3a8ae5de313124550a Mon Sep 17 00:00:00 2001 From: Martin Leduc <31558169+DecimalTurn@users.noreply.github.com> Date: Sat, 14 Nov 2026 18:57:53 -0500 Subject: [PATCH 15/18] docs: update changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ddbbd50a..9156f675 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed -- Patching: correctly replace and delete tables split across dotted keys and headers, including nested array-of-tables and inline-table cases. ([#335]) +- Patching: correctly replace and delete tables split across dotted keys and headers, including nested inline-table paths. ([#335]) +- Patching: keep nested sub-table replacements scoped to the selected array-of-tables entry. ## [3.3.2] - 2026-10-31 From 392962f312e8a827939d27a3eb42294ce27ea552 Mon Sep 17 00:00:00 2001 From: Martin Leduc <31558169+DecimalTurn@users.noreply.github.com> Date: Sat, 14 Nov 2026 23:11:31 -0500 Subject: [PATCH 16/18] docs: improve documentation for 27a02a7 --- CHANGELOG.md | 3 ++- src/__tests__/patch.test.ts | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9156f675..4e659644 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Patching: correctly replace and delete tables split across dotted keys and headers, including nested inline-table paths. ([#335]) -- Patching: keep nested sub-table replacements scoped to the selected array-of-tables entry. +- Patching: keep nested sub-table replacements scoped to the selected array-of-tables entry and prevent table redefinition ([27a02a7]). ## [3.3.2] - 2026-10-31 @@ -538,3 +538,4 @@ This first forked version from [timhall/toml-patch](https://github.com/timhall/t [#335]: https://github.com/DecimalTurn/toml-patch/issues/335 [0e66e68]: https://github.com/DecimalTurn/toml-patch/commit/0e66e68cbf42a07bc44445e46c3ea7bea97f95c1 [f97d571]: https://github.com/DecimalTurn/toml-patch/commit/f97d57194952b6603f0fcd686c32d3ae52a2c903 +[27a02a7]: https://github.com/DecimalTurn/toml-patch/commit/27a02a746533d82026fe6f1fb6771f8e1ba5ac27 \ No newline at end of file diff --git a/src/__tests__/patch.test.ts b/src/__tests__/patch.test.ts index 2b71e8bd..46cedd58 100644 --- a/src/__tests__/patch.test.ts +++ b/src/__tests__/patch.test.ts @@ -4880,6 +4880,9 @@ describe('sub-table replacement known issues', () => { // Follow-up ownership and nesting cases for issue #335. describe('issue 335 follow-up ownership and nesting cases', () => { + + // This test was initially failing due to table redefinition issues. + // Addressed in 27a02a7 test('replaces a dotted/header-defined sub-table in only the selected AOT entry', () => { const existing = dedent` [[fruit]] From e7faae587dc426f17c14c959709910fc312e6e54 Mon Sep 17 00:00:00 2001 From: Martin Leduc <31558169+DecimalTurn@users.noreply.github.com> Date: Sun, 15 Nov 2026 00:10:52 -0500 Subject: [PATCH 17/18] test: add additional regression tests for header-defined table transformation in presence of dotted-keys --- src/__tests__/patch.test.ts | 188 ++++++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) diff --git a/src/__tests__/patch.test.ts b/src/__tests__/patch.test.ts index 46cedd58..46f7255e 100644 --- a/src/__tests__/patch.test.ts +++ b/src/__tests__/patch.test.ts @@ -4959,6 +4959,194 @@ describe('sub-table replacement known issues', () => { expect(parse(patched)).toEqual(updated); }); + test('replaces a header-defined table with a dotted key when its parent was defined by dotted keys', () => { + const existing = dedent` + [route] + args.id = "a" + [route.args.deep.leaf] + id = "a" + [route.args.other] + z = 1 + ` + '\n'; + + const updated = parse(existing) as { + route: { args: { id: string; deep: { leaf: { id: string } } | string; other: { z: number } } }; + }; + updated.route.args.deep = 'none'; + + const patched = patch(existing, updated); + const expected = dedent` + [route] + args.id = "a" + args.deep = "none" + [route.args.other] + z = 1 + ` + '\n'; + + expect(parse(patched)).toEqual(updated); + expect(patched).toBe(expected); + }); + + test('quotes a dotted parent when replacing its header-defined child with a dotted key', () => { + const existing = dedent` + ["my.route"] + "my.args".id = "a" + ["my.route"."my.args".deep.leaf] + id = "a" + ["my.route"."my.args".other] + z = 1 + ` + '\n'; + + const updated = parse(existing) as { + 'my.route': { + 'my.args': { id: string; deep: { leaf: { id: string } } | string; other: { z: number } }; + }; + }; + updated['my.route']['my.args'].deep = 'none'; + + const patched = patch(existing, updated); + const expected = dedent` + ["my.route"] + "my.args".id = "a" + "my.args".deep = "none" + ["my.route"."my.args".other] + z = 1 + ` + '\n'; + + expect(parse(patched)).toEqual(updated); + expect(patched).toBe(expected); + }); + + test('replaces multiple header-defined tables with dotted keys under a dotted parent', () => { + const existing = dedent` + [route] + args.id = "a" + [route.args.deep.leaf] + id = "deep" + [route.args.wide.leaf] + id = "wide" + [route.args.other] + z = 1 + ` + '\n'; + + const updated = parse(existing) as { + route: { + args: { + id: string; + deep: { leaf: { id: string } } | string; + wide: { leaf: { id: string } } | number; + other: { z: number }; + }; + }; + }; + updated.route.args.deep = 'none'; + updated.route.args.wide = 2; + + const patched = patch(existing, updated); + const expected = dedent` + [route] + args.id = "a" + args.deep = "none" + args.wide = 2 + [route.args.other] + z = 1 + ` + '\n'; + + expect(parse(patched)).toEqual(updated); + expect(patched).toBe(expected); + }); + + test('deletes a dotted parent and its descendant headers without recreating the table', () => { + const existing = dedent` + [route] + args.deep.token = "secret" + + [route.args.deep.cache] + enabled = true + + [route.meta] + name = "api" + ` + '\n'; + + const updated = parse(existing); + delete updated.route.args; + + const patched = patch(existing, updated); + const expected = dedent` + [route] + + [route.meta] + name = "api" + ` + '\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + }); + + test('deleting a scalar removes its dotted and header descendants', () => { + const existing = dedent` + value = 2019-06-13T09:28:26 + value.child.enabled = true + + [value.settings] + mode = "old" + ` + '\n'; + + const updated = parse(existing) as { value?: unknown }; + delete updated.value; + + const patched = patch(existing, updated); + + expect(patched).toBe('\n'); + expect(parse(patched)).toEqual(updated); + }); + + test('deletes dotted and header descendants from the first AOT entry only', () => { + const existing = dedent` + [[fruit]] + id = "red" + apple.color = "red" + + [fruit.apple.texture] + smooth = true + + [[fruit.apple.seeds]] + size = 1 + + [[fruit]] + id = "green" + apple.color = "green" + + [fruit.apple.texture] + smooth = false + + [[fruit.apple.seeds]] + size = 2 + ` + '\n'; + + const updated = parse(existing); + delete updated.fruit[0].apple; + + const patched = patch(existing, updated); + const expected = dedent` + [[fruit]] + id = "red" + + [[fruit]] + id = "green" + apple.color = "green" + + [fruit.apple.texture] + smooth = false + + [[fruit.apple.seeds]] + size = 2 + ` + '\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + }); + test('replaces dotted inline-table children in one array element without changing siblings', () => { const existing = 'items = [{ id = "a", opts.deep.left = 1, opts.deep.right = 2 }, ' + From 7712b0ba0d73372090ebfdd0b7a6075b9caaade7 Mon Sep 17 00:00:00 2001 From: Martin Leduc <31558169+DecimalTurn@users.noreply.github.com> Date: Sun, 15 Nov 2026 00:16:17 -0500 Subject: [PATCH 18/18] test: preserve root scope in fuzz6 scenarios Emit root inline-table cases before section headers so multi-mutation seeds retain their document scope. --- src/__tests__/fuzz-patch6.ts | 4 +++- src/__tests__/fuzz6.test.ts | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/__tests__/fuzz-patch6.ts b/src/__tests__/fuzz-patch6.ts index 35c1234d..c28d0bbf 100644 --- a/src/__tests__/fuzz-patch6.ts +++ b/src/__tests__/fuzz-patch6.ts @@ -116,8 +116,10 @@ function generateStructuralSource( { seed, mutationCount }: Pick ): GeneratedSource { const scenarios = Array.from({ length: mutationCount }, (_, index) => createScenario(seed, index)); + const inlineScenarios = scenarios.filter(scenario => scenario.kind === 'replace-inline-table'); + const sectionScenarios = scenarios.filter(scenario => scenario.kind !== 'replace-inline-table'); return { - source: scenarios.map(scenario => scenario.source).join('\n'), + source: [...inlineScenarios, ...sectionScenarios].map(scenario => scenario.source).join('\n'), description: 'dotted-key, AOT-deletion, and inline-table structural cases' }; } diff --git a/src/__tests__/fuzz6.test.ts b/src/__tests__/fuzz6.test.ts index e1789cde..c8d96c11 100644 --- a/src/__tests__/fuzz6.test.ts +++ b/src/__tests__/fuzz6.test.ts @@ -21,3 +21,10 @@ test('fuzz6 seed 1 round-trips deleting a table that owns an AOT descendant', () test('fuzz6 seed 2 round-trips replacing a nested inline-table path', () => { expect(fuzzOne6(2, 1).status).toBe('ok'); }); + +test('fuzz6 seed 0 keeps root inline tables scoped correctly across three mutations', () => { + const result = fuzzOne6(0, 3); + + expect(result.status, result.error).toBe('ok'); + expect(result.mutationDescs).toHaveLength(3); +});