|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | + |
| 3 | +import { assertWaitText } from './live-assertions.ts'; |
| 4 | +import { acceptDeepLinkConfirmationIfPresent } from './live-automation-scenario.ts'; |
| 5 | +import { type LiveContext, runStep, verifyBehavior } from './live-harness.ts'; |
| 6 | + |
| 7 | +const VISIBLE_DEPTH_DEEP_LINK = 'agent-device-test-app:///snapshot-depth'; |
| 8 | +const TITLE_ID = 'visible-depth-title'; |
| 9 | +const PARENT_ID = 'visible-depth-clipped-parent'; |
| 10 | +const CHILD_ID = 'visible-depth-projected-child'; |
| 11 | + |
| 12 | +type SnapshotNode = { |
| 13 | + depth?: unknown; |
| 14 | + identifier?: unknown; |
| 15 | + index?: unknown; |
| 16 | + label?: unknown; |
| 17 | + parentIndex?: unknown; |
| 18 | +}; |
| 19 | + |
| 20 | +export async function assertRegularVisibleDepthFrontier(context: LiveContext): Promise<void> { |
| 21 | + await runStep(context, 'open regular visible-depth fixture', [ |
| 22 | + 'open', |
| 23 | + context.appId, |
| 24 | + '--relaunch', |
| 25 | + '--launch-url', |
| 26 | + VISIBLE_DEPTH_DEEP_LINK, |
| 27 | + ]); |
| 28 | + await acceptDeepLinkConfirmationIfPresent(context); |
| 29 | + await assertWaitText(context, 'Regular visible depth frontier'); |
| 30 | + |
| 31 | + const regular = await runStep(context, 'capture regular visible-depth frontier', [ |
| 32 | + 'snapshot', |
| 33 | + '--depth', |
| 34 | + '1', |
| 35 | + ]); |
| 36 | + assertSnapshotBackend(regular, 'regular depth-1 snapshot'); |
| 37 | + const regularNodes = snapshotNodes(regular); |
| 38 | + const regularRoot = requireRoot(regularNodes, 'regular depth-1 snapshot'); |
| 39 | + const regularTitle = requireIdentifier(regularNodes, TITLE_ID, 'regular depth-1 snapshot'); |
| 40 | + const projectedChild = requireIdentifier(regularNodes, CHILD_ID, 'regular depth-1 snapshot'); |
| 41 | + assert.equal( |
| 42 | + regularTitle.label, |
| 43 | + 'Regular visible depth frontier', |
| 44 | + `regular title should remain observable: ${JSON.stringify(regular)}`, |
| 45 | + ); |
| 46 | + assert.equal( |
| 47 | + regularNodes.some((node) => node.identifier === PARENT_ID), |
| 48 | + false, |
| 49 | + `clipped structural parent must be absent from regular presentation: ${JSON.stringify(regular)}`, |
| 50 | + ); |
| 51 | + assert.equal( |
| 52 | + projectedChild.depth, |
| 53 | + 1, |
| 54 | + `projected child should occupy presented depth 1: ${JSON.stringify(regular)}`, |
| 55 | + ); |
| 56 | + assert.equal( |
| 57 | + projectedChild.parentIndex, |
| 58 | + regularRoot.index, |
| 59 | + `projected child should be reparented to the presented root: ${JSON.stringify(regular)}`, |
| 60 | + ); |
| 61 | + assert.ok( |
| 62 | + regularNodes.every((node) => numericDepth(node) <= 1), |
| 63 | + `regular --depth 1 exceeded the presented frontier: ${JSON.stringify(regular)}`, |
| 64 | + ); |
| 65 | + |
| 66 | + const rawFull = await runStep(context, 'capture full raw visible-depth tree', [ |
| 67 | + 'snapshot', |
| 68 | + '--raw', |
| 69 | + ]); |
| 70 | + assertSnapshotBackend(rawFull, 'full raw visible-depth snapshot'); |
| 71 | + const rawFullNodes = snapshotNodes(rawFull); |
| 72 | + const rawParent = requireIdentifier(rawFullNodes, PARENT_ID, 'full raw visible-depth snapshot'); |
| 73 | + const rawChild = requireIdentifier(rawFullNodes, CHILD_ID, 'full raw visible-depth snapshot'); |
| 74 | + assert.ok( |
| 75 | + numericDepth(rawChild) > 1, |
| 76 | + `raw projected child must remain below traversal depth 1: ${JSON.stringify(rawFull)}`, |
| 77 | + ); |
| 78 | + assert.equal( |
| 79 | + rawChild.parentIndex, |
| 80 | + rawParent.index, |
| 81 | + `raw tree must retain the structural parent relationship: ${JSON.stringify(rawFull)}`, |
| 82 | + ); |
| 83 | + |
| 84 | + const rawDepthOne = await runStep(context, 'capture raw depth-bounded visible-depth tree', [ |
| 85 | + 'snapshot', |
| 86 | + '--raw', |
| 87 | + '--depth', |
| 88 | + '1', |
| 89 | + ]); |
| 90 | + assertSnapshotBackend(rawDepthOne, 'raw depth-1 visible-depth snapshot'); |
| 91 | + const rawDepthOneNodes = snapshotNodes(rawDepthOne); |
| 92 | + assert.equal( |
| 93 | + rawDepthOneNodes.some((node) => node.identifier === CHILD_ID), |
| 94 | + false, |
| 95 | + `raw --depth 1 must omit the raw depth-2 child: ${JSON.stringify(rawDepthOne)}`, |
| 96 | + ); |
| 97 | + assert.ok( |
| 98 | + rawDepthOneNodes.every((node) => numericDepth(node) <= 1), |
| 99 | + `raw --depth 1 exceeded the acquisition frontier: ${JSON.stringify(rawDepthOne)}`, |
| 100 | + ); |
| 101 | + |
| 102 | + await runStep(context, 'restore fixture home after visible-depth capture', [ |
| 103 | + 'open', |
| 104 | + context.appId, |
| 105 | + '--relaunch', |
| 106 | + ]); |
| 107 | + await assertWaitText(context, 'Agent Device Tester'); |
| 108 | + verifyBehavior( |
| 109 | + context, |
| 110 | + 'regular-visible-depth-frontier', |
| 111 | + 'public regular depth 1 keeps an independently projected child after removing its clipped structural parent while raw depth remains traversal-bounded', |
| 112 | + ); |
| 113 | +} |
| 114 | + |
| 115 | +function snapshotNodes(result: { json?: any }): SnapshotNode[] { |
| 116 | + const nodes = result.json?.data?.nodes; |
| 117 | + assert.ok( |
| 118 | + Array.isArray(nodes), |
| 119 | + `snapshot response did not contain nodes: ${JSON.stringify(result)}`, |
| 120 | + ); |
| 121 | + return nodes as SnapshotNode[]; |
| 122 | +} |
| 123 | + |
| 124 | +function requireIdentifier(nodes: SnapshotNode[], identifier: string, description: string) { |
| 125 | + const node = nodes.find((candidate) => candidate.identifier === identifier); |
| 126 | + assert.ok(node, `${description} missing ${identifier}: ${JSON.stringify(nodes)}`); |
| 127 | + return node; |
| 128 | +} |
| 129 | + |
| 130 | +function requireRoot(nodes: SnapshotNode[], description: string) { |
| 131 | + const root = nodes.find( |
| 132 | + (node) => |
| 133 | + numericDepth(node) === 0 && (node.parentIndex === undefined || node.parentIndex === null), |
| 134 | + ); |
| 135 | + assert.ok(root, `${description} missing a presented root: ${JSON.stringify(nodes)}`); |
| 136 | + return root; |
| 137 | +} |
| 138 | + |
| 139 | +function numericDepth(node: SnapshotNode): number { |
| 140 | + assert.equal( |
| 141 | + typeof node.depth, |
| 142 | + 'number', |
| 143 | + `snapshot node has no numeric depth: ${JSON.stringify(node)}`, |
| 144 | + ); |
| 145 | + return node.depth as number; |
| 146 | +} |
| 147 | + |
| 148 | +function assertSnapshotBackend(result: { json?: any }, description: string): void { |
| 149 | + assert.equal( |
| 150 | + result.json?.data?.snapshotQuality?.backend, |
| 151 | + 'tree', |
| 152 | + `${description} must exercise the recursive tree backend: ${JSON.stringify(result)}`, |
| 153 | + ); |
| 154 | +} |
0 commit comments