|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { test } from 'vitest'; |
| 3 | +import { resolveDaemonRequestTimeoutMs } from '../request-timeout.ts'; |
| 4 | + |
| 5 | +test('wait request timeout extends past the user-supplied wait budget', () => { |
| 6 | + const base = { |
| 7 | + session: 'default', |
| 8 | + positionals: [] as string[], |
| 9 | + flags: {}, |
| 10 | + meta: {}, |
| 11 | + }; |
| 12 | + |
| 13 | + // Explicit budgets beyond the default envelope extend it (budget + margin). |
| 14 | + assert.equal( |
| 15 | + resolveDaemonRequestTimeoutMs({ |
| 16 | + ...base, |
| 17 | + command: 'wait', |
| 18 | + positionals: ['text', 'Ready', '180000'], |
| 19 | + }), |
| 20 | + 210_000, |
| 21 | + ); |
| 22 | + assert.equal( |
| 23 | + resolveDaemonRequestTimeoutMs({ |
| 24 | + ...base, |
| 25 | + command: 'wait', |
| 26 | + positionals: ['stable', '500', '120000'], |
| 27 | + }), |
| 28 | + 150_000, |
| 29 | + ); |
| 30 | + // Sleep waits block for their full duration and get the same treatment. |
| 31 | + assert.equal( |
| 32 | + resolveDaemonRequestTimeoutMs({ ...base, command: 'wait', positionals: ['120000'] }), |
| 33 | + 150_000, |
| 34 | + ); |
| 35 | + // Small budgets never shrink the envelope below the default. |
| 36 | + assert.equal( |
| 37 | + resolveDaemonRequestTimeoutMs({ |
| 38 | + ...base, |
| 39 | + command: 'wait', |
| 40 | + positionals: ['text', 'Ready', '5000'], |
| 41 | + }), |
| 42 | + 90_000, |
| 43 | + ); |
| 44 | + // No explicit budget → default envelope. |
| 45 | + assert.equal( |
| 46 | + resolveDaemonRequestTimeoutMs({ ...base, command: 'wait', positionals: ['text', 'Ready'] }), |
| 47 | + 90_000, |
| 48 | + ); |
| 49 | + assert.equal(resolveDaemonRequestTimeoutMs({ ...base, command: 'wait' }), 90_000); |
| 50 | +}); |
| 51 | + |
| 52 | +test('interaction --settle budgets add post-action settle time on top of the normal envelope', () => { |
| 53 | + const base = { |
| 54 | + session: 'default', |
| 55 | + positionals: ['@e2'], |
| 56 | + meta: {}, |
| 57 | + }; |
| 58 | + |
| 59 | + // --timeout bounds the SETTLE wait after selector resolution and the action, |
| 60 | + // so the envelope keeps the normal touch-command overhead and then adds the |
| 61 | + // settle budget plus the same safety margin used by wait. |
| 62 | + assert.equal( |
| 63 | + resolveDaemonRequestTimeoutMs({ |
| 64 | + ...base, |
| 65 | + command: 'press', |
| 66 | + flags: { settle: true, timeoutMs: 120_000 }, |
| 67 | + }), |
| 68 | + 240_000, |
| 69 | + ); |
| 70 | + // A small settle deadline still needs the normal touch-command envelope plus |
| 71 | + // room for post-action observation. |
| 72 | + assert.equal( |
| 73 | + resolveDaemonRequestTimeoutMs({ |
| 74 | + ...base, |
| 75 | + command: 'fill', |
| 76 | + flags: { settle: true, timeoutMs: 5_000 }, |
| 77 | + }), |
| 78 | + 125_000, |
| 79 | + ); |
| 80 | + // Longpress keeps the cold Android helper route and its 120-second maximum |
| 81 | + // hold inside the outer envelope. |
| 82 | + assert.equal( |
| 83 | + resolveDaemonRequestTimeoutMs({ |
| 84 | + ...base, |
| 85 | + command: 'longpress', |
| 86 | + positionals: ['300', '500', '120000'], |
| 87 | + flags: {}, |
| 88 | + }), |
| 89 | + 210_000, |
| 90 | + ); |
| 91 | + // Bare --settle adds its default budget after the longpress-specific base, |
| 92 | + // so a maximum hold still leaves room for post-action observation. |
| 93 | + assert.equal( |
| 94 | + resolveDaemonRequestTimeoutMs({ |
| 95 | + ...base, |
| 96 | + command: 'longpress', |
| 97 | + flags: { settle: true }, |
| 98 | + }), |
| 99 | + 250_000, |
| 100 | + ); |
| 101 | + // Bare timeoutMs without --settle remains wire-compatible with older touch |
| 102 | + // command clients: it is ignored instead of opting into settle semantics. |
| 103 | + assert.equal( |
| 104 | + resolveDaemonRequestTimeoutMs({ |
| 105 | + ...base, |
| 106 | + command: 'press', |
| 107 | + flags: { timeoutMs: 120_000 }, |
| 108 | + }), |
| 109 | + 90_000, |
| 110 | + ); |
| 111 | + assert.equal(resolveDaemonRequestTimeoutMs({ ...base, command: 'press', flags: {} }), 90_000); |
| 112 | +}); |
| 113 | + |
| 114 | +test('snapshot uses the standard daemon request timeout with an explicit override', () => { |
| 115 | + const base = { |
| 116 | + session: 'default', |
| 117 | + positionals: [], |
| 118 | + flags: {}, |
| 119 | + meta: {}, |
| 120 | + }; |
| 121 | + |
| 122 | + assert.equal(resolveDaemonRequestTimeoutMs({ ...base, command: 'snapshot' }), 90_000); |
| 123 | + assert.equal( |
| 124 | + resolveDaemonRequestTimeoutMs({ |
| 125 | + ...base, |
| 126 | + command: 'snapshot', |
| 127 | + flags: { timeoutMs: 120_000 }, |
| 128 | + }), |
| 129 | + 120_000, |
| 130 | + ); |
| 131 | + assert.equal(resolveDaemonRequestTimeoutMs({ ...base, command: 'screenshot' }), 90_000); |
| 132 | + assert.equal(resolveDaemonRequestTimeoutMs({ ...base, command: 'install' }), 180_000); |
| 133 | + assert.equal(resolveDaemonRequestTimeoutMs({ ...base, command: 'reinstall' }), 180_000); |
| 134 | + assert.equal(resolveDaemonRequestTimeoutMs({ ...base, command: 'install_source' }), 180_000); |
| 135 | + assert.equal( |
| 136 | + resolveDaemonRequestTimeoutMs({ |
| 137 | + ...base, |
| 138 | + command: 'prepare', |
| 139 | + positionals: ['ios-runner'], |
| 140 | + }), |
| 141 | + 240_000, |
| 142 | + ); |
| 143 | + assert.equal( |
| 144 | + resolveDaemonRequestTimeoutMs({ |
| 145 | + ...base, |
| 146 | + command: 'prepare', |
| 147 | + positionals: ['ios-runner'], |
| 148 | + flags: { timeoutMs: 240_000 }, |
| 149 | + }), |
| 150 | + 240_000, |
| 151 | + ); |
| 152 | + assert.equal(resolveDaemonRequestTimeoutMs({ ...base, command: 'test' }), undefined); |
| 153 | +}); |
0 commit comments