|
| 1 | +import { test } from 'vitest'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { DAEMON_COMMAND_GROUPS, PUBLIC_COMMANDS } from '../command-catalog.ts'; |
| 4 | +import { |
| 5 | + fillCommandCodec, |
| 6 | + findCommandCodec, |
| 7 | + interactionTargetCodec, |
| 8 | + isCommandCodec, |
| 9 | + settingsCommandCodec, |
| 10 | + waitCommandCodec, |
| 11 | +} from '../command-codecs.ts'; |
| 12 | +import type { CliFlags } from '../utils/command-schema.ts'; |
| 13 | + |
| 14 | +const BASE_FLAGS: CliFlags = { |
| 15 | + json: false, |
| 16 | + help: false, |
| 17 | + version: false, |
| 18 | +}; |
| 19 | + |
| 20 | +test('command catalog owns daemon routing groups', () => { |
| 21 | + assert.equal(DAEMON_COMMAND_GROUPS.snapshot.has(PUBLIC_COMMANDS.wait), true); |
| 22 | + assert.equal(DAEMON_COMMAND_GROUPS.observability.has(PUBLIC_COMMANDS.logs), true); |
| 23 | + assert.equal(DAEMON_COMMAND_GROUPS.replay.has(PUBLIC_COMMANDS.test), true); |
| 24 | +}); |
| 25 | + |
| 26 | +test('wait codec preserves CLI bare text and client selector forms', () => { |
| 27 | + const options = waitCommandCodec.decode(['Continue', '1500'], BASE_FLAGS); |
| 28 | + assert.equal(options.text, 'Continue'); |
| 29 | + assert.equal(options.timeoutMs, 1500); |
| 30 | + assert.deepEqual( |
| 31 | + waitCommandCodec.encode({ |
| 32 | + selector: 'id=submit', |
| 33 | + timeoutMs: 2000, |
| 34 | + }), |
| 35 | + ['id=submit', '2000'], |
| 36 | + ); |
| 37 | +}); |
| 38 | + |
| 39 | +test('interaction and fill codecs share ref, selector, and point grammar', () => { |
| 40 | + assert.deepEqual(interactionTargetCodec.decode(['@e3', 'Email']), { |
| 41 | + ref: '@e3', |
| 42 | + label: 'Email', |
| 43 | + }); |
| 44 | + assert.deepEqual(interactionTargetCodec.encode({ selector: 'id=submit' }), ['id=submit']); |
| 45 | + assert.deepEqual(fillCommandCodec.decode(['id=email', 'qa@example.com']), { |
| 46 | + kind: 'selector', |
| 47 | + target: { selector: 'id=email' }, |
| 48 | + text: 'qa@example.com', |
| 49 | + }); |
| 50 | + assert.deepEqual(fillCommandCodec.decode(['@e4', 'Email', 'qa@example.com']), { |
| 51 | + kind: 'ref', |
| 52 | + target: { ref: '@e4', label: 'Email' }, |
| 53 | + text: 'qa@example.com', |
| 54 | + }); |
| 55 | + assert.deepEqual(fillCommandCodec.decode(['10', '20', 'hello']), { |
| 56 | + kind: 'point', |
| 57 | + target: { x: 10, y: 20 }, |
| 58 | + text: 'hello', |
| 59 | + }); |
| 60 | + assert.deepEqual( |
| 61 | + fillCommandCodec.encode({ |
| 62 | + ref: '@e4', |
| 63 | + label: 'Email', |
| 64 | + text: 'qa@example.com', |
| 65 | + }), |
| 66 | + ['@e4', 'Email', 'qa@example.com'], |
| 67 | + ); |
| 68 | +}); |
| 69 | + |
| 70 | +test('find and is codecs round-trip command action positionals', () => { |
| 71 | + const findOptions = findCommandCodec.decode(['label', 'Continue', 'wait', '3000'], { |
| 72 | + ...BASE_FLAGS, |
| 73 | + platform: 'ios', |
| 74 | + findFirst: true, |
| 75 | + }); |
| 76 | + assert.equal(findOptions.platform, 'ios'); |
| 77 | + assert.equal(findOptions.locator, 'label'); |
| 78 | + assert.equal(findOptions.query, 'Continue'); |
| 79 | + assert.equal(findOptions.action, 'wait'); |
| 80 | + assert.equal(findOptions.timeoutMs, 3000); |
| 81 | + assert.equal(findOptions.first, true); |
| 82 | + assert.deepEqual(findCommandCodec.encode(findOptions), ['label', 'Continue', 'wait', '3000']); |
| 83 | + |
| 84 | + const isOptions = isCommandCodec.decode(['text', 'id=title', 'Welcome'], BASE_FLAGS); |
| 85 | + assert.equal(isOptions.predicate, 'text'); |
| 86 | + assert.equal(isOptions.selector, 'id=title'); |
| 87 | + assert.equal(isOptions.value, 'Welcome'); |
| 88 | + assert.deepEqual(isCommandCodec.encode(isOptions), ['text', 'id=title', 'Welcome']); |
| 89 | +}); |
| 90 | + |
| 91 | +test('settings codec owns positional grammar for command and client paths', () => { |
| 92 | + const location = settingsCommandCodec.decode(['location', 'set', '37.3349', '-122.009'], { |
| 93 | + ...BASE_FLAGS, |
| 94 | + platform: 'ios', |
| 95 | + }); |
| 96 | + assert.equal(location.platform, 'ios'); |
| 97 | + assert.equal(location.setting, 'location'); |
| 98 | + assert.equal(location.state, 'set'); |
| 99 | + assert.equal(location.latitude, 37.3349); |
| 100 | + assert.equal(location.longitude, -122.009); |
| 101 | + assert.deepEqual(settingsCommandCodec.encode(location), [ |
| 102 | + 'location', |
| 103 | + 'set', |
| 104 | + '37.3349', |
| 105 | + '-122.009', |
| 106 | + ]); |
| 107 | + |
| 108 | + assert.deepEqual( |
| 109 | + settingsCommandCodec.encode({ |
| 110 | + setting: 'permission', |
| 111 | + state: 'grant', |
| 112 | + permission: 'camera', |
| 113 | + mode: 'limited', |
| 114 | + }), |
| 115 | + ['permission', 'grant', 'camera', 'limited'], |
| 116 | + ); |
| 117 | +}); |
0 commit comments