|
1 | 1 | import { beforeEach, describe, expect, it, vi } from 'vitest'; |
2 | 2 | import type { SnapshotNode } from '@agent-device/kernel/snapshot'; |
3 | | -import type { ReadTextAtPointInput } from '@agent-device/contracts/platform'; |
| 3 | +import { |
| 4 | + elementTextRead, |
| 5 | + type ElementTextReadOutcome, |
| 6 | + type ReadTextAtPointInput, |
| 7 | +} from '@agent-device/contracts/platform'; |
4 | 8 | import { readTextForNode } from '../interaction-read.ts'; |
5 | 9 |
|
6 | 10 | /** |
7 | 11 | * Bound at the seam the handler consumes (the runtime's `readTextAtPoint` operation), never at |
8 | 12 | * `core/dispatch.ts`: `get` is migrated, so the live read reaches this fake through the request |
9 | 13 | * binding rather than through the legacy dispatcher. |
10 | 14 | */ |
11 | | -const readTextAtPoint = vi.fn(async (_input: ReadTextAtPointInput) => 'backend-text'); |
| 15 | +const readTextAtPoint = vi.fn( |
| 16 | + async (_input: ReadTextAtPointInput): Promise<ElementTextReadOutcome> => |
| 17 | + elementTextRead('backend-text'), |
| 18 | +); |
12 | 19 |
|
13 | 20 | function node(overrides: Partial<SnapshotNode>): SnapshotNode { |
14 | 21 | return { |
@@ -91,21 +98,34 @@ describe('readTextForNode', () => { |
91 | 98 | expect(readTextAtPoint).not.toHaveBeenCalled(); |
92 | 99 | }); |
93 | 100 |
|
94 | | - it('falls back to the captured tree through a typed reason when the live read fails', async () => { |
95 | | - readTextAtPoint.mockRejectedValueOnce(new Error('runner transport closed')); |
| 101 | + // ADR 0019 §2: the ONLY fallbacks are the contract's classified reasons. |
| 102 | + it.each(['no-text-at-point', 'surface-not-readable'] as const)( |
| 103 | + 'falls back to the captured tree for the classified reason %s', |
| 104 | + async (reason) => { |
| 105 | + readTextAtPoint.mockResolvedValueOnce({ status: 'unreadable', reason }); |
| 106 | + const text = await readTextForNode({ |
| 107 | + ...baseParams, |
| 108 | + node: node({ type: 'textfield', value: 'snap' }), |
| 109 | + }); |
| 110 | + expect(text).toBe('snap'); |
| 111 | + }, |
| 112 | + ); |
| 113 | + |
| 114 | + it('classifies a blank live read as no-text-at-point rather than reading blank text', async () => { |
| 115 | + readTextAtPoint.mockResolvedValueOnce(elementTextRead(' ')); |
96 | 116 | const text = await readTextForNode({ |
97 | 117 | ...baseParams, |
98 | 118 | node: node({ type: 'textfield', value: 'snap' }), |
99 | 119 | }); |
100 | 120 | expect(text).toBe('snap'); |
101 | 121 | }); |
102 | 122 |
|
103 | | - it('falls back to the captured tree when the live read returns blank text', async () => { |
104 | | - readTextAtPoint.mockResolvedValueOnce(' '); |
105 | | - const text = await readTextForNode({ |
106 | | - ...baseParams, |
107 | | - node: node({ type: 'textfield', value: 'snap' }), |
108 | | - }); |
109 | | - expect(text).toBe('snap'); |
| 123 | + // The retired generic catch: an unclassified failure must NOT become "this element has no |
| 124 | + // text". It propagates, so a runner/helper failure can never be answered from a stale tree. |
| 125 | + it('propagates an unexpected live-read failure instead of falling back', async () => { |
| 126 | + readTextAtPoint.mockRejectedValueOnce(new Error('runner transport closed')); |
| 127 | + await expect( |
| 128 | + readTextForNode({ ...baseParams, node: node({ type: 'textfield', value: 'snap' }) }), |
| 129 | + ).rejects.toThrow(/runner transport closed/); |
110 | 130 | }); |
111 | 131 | }); |
0 commit comments