Skip to content

Commit 43d216a

Browse files
committed
chore: fix tests
1 parent 0c15333 commit 43d216a

1 file changed

Lines changed: 69 additions & 6 deletions

File tree

apps/cli/src/__tests__/conformance/scenarios.ts

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,65 @@ async function nthListAddress(
151151
return address;
152152
}
153153

154+
function readNestedBoolean(data: unknown, key: 'canContinue' | 'canJoin'): boolean | undefined {
155+
if (!data || typeof data !== 'object') return undefined;
156+
157+
const direct = data as Record<string, unknown>;
158+
if (typeof direct[key] === 'boolean') return direct[key] as boolean;
159+
160+
const result = direct.result;
161+
if (result && typeof result === 'object') {
162+
const nested = result as Record<string, unknown>;
163+
if (typeof nested[key] === 'boolean') return nested[key] as boolean;
164+
}
165+
166+
return undefined;
167+
}
168+
169+
async function findFirstContinuableListAddress(
170+
harness: ConformanceHarness,
171+
stateDir: string,
172+
docPath: string,
173+
): Promise<Record<string, unknown> | null> {
174+
const items = await listDiscoveryItems(harness, stateDir, docPath, 50);
175+
for (const item of items) {
176+
const address = item.address;
177+
if (!address || typeof address !== 'object') continue;
178+
179+
const probe = await harness.runCli(
180+
['lists', 'can-continue-previous', docPath, '--target-json', JSON.stringify(address)],
181+
stateDir,
182+
);
183+
if (probe.result.code !== 0 || probe.envelope.ok !== true) continue;
184+
if (readNestedBoolean(probe.envelope.data, 'canContinue') === true) {
185+
return address;
186+
}
187+
}
188+
return null;
189+
}
190+
191+
async function findFirstJoinableWithPreviousAddress(
192+
harness: ConformanceHarness,
193+
stateDir: string,
194+
docPath: string,
195+
): Promise<Record<string, unknown> | null> {
196+
const items = await listDiscoveryItems(harness, stateDir, docPath, 50);
197+
for (const item of items) {
198+
const address = item.address;
199+
if (!address || typeof address !== 'object') continue;
200+
201+
const probe = await harness.runCli(
202+
['lists', 'can-join', docPath, '--input-json', JSON.stringify({ target: address, direction: 'withPrevious' })],
203+
stateDir,
204+
);
205+
if (probe.result.code !== 0 || probe.envelope.ok !== true) continue;
206+
if (readNestedBoolean(probe.envelope.data, 'canJoin') === true) {
207+
return address;
208+
}
209+
}
210+
return null;
211+
}
212+
154213
function sectionMutationScenario(
155214
operationId: CliOperationId,
156215
label: string,
@@ -1247,9 +1306,11 @@ export const SUCCESS_SCENARIOS = {
12471306
if (separate.result.code !== 0) {
12481307
throw new Error('Failed to prepare continue-previous conformance fixture via lists separate.');
12491308
}
1250-
// Resolve target from the prepared document to avoid stale nodeId usage
1251-
// when list-item ids are regenerated across write/reload boundaries.
1252-
const preparedSecondItem = await nthListAddress(harness, stateDir, preparedDoc, 1);
1309+
// Resolve a list item that can actually continue the previous sequence.
1310+
// This avoids positional assumptions across environments/serialization.
1311+
const preparedSecondItem =
1312+
(await findFirstContinuableListAddress(harness, stateDir, preparedDoc)) ??
1313+
(await nthListAddress(harness, stateDir, preparedDoc, 1));
12531314
return {
12541315
stateDir,
12551316
args: [
@@ -1337,9 +1398,11 @@ export const SUCCESS_SCENARIOS = {
13371398
if (separate.result.code !== 0) {
13381399
throw new Error('Failed to prepare join conformance fixture via lists separate.');
13391400
}
1340-
// Resolve target from the prepared document to avoid stale nodeId usage
1341-
// when list-item ids are regenerated across write/reload boundaries.
1342-
const preparedSecondItem = await nthListAddress(harness, stateDir, preparedDoc, 1);
1401+
// Resolve a list item that can actually join with the previous sequence.
1402+
// This avoids positional assumptions across environments/serialization.
1403+
const preparedSecondItem =
1404+
(await findFirstJoinableWithPreviousAddress(harness, stateDir, preparedDoc)) ??
1405+
(await nthListAddress(harness, stateDir, preparedDoc, 1));
13431406
return {
13441407
stateDir,
13451408
args: [

0 commit comments

Comments
 (0)