|
| 1 | +import { createTestDeviceInventoryGateways } from '../../__tests__/test-utils/device-inventory-gateways.ts'; |
| 2 | +/** |
| 3 | + * #3260: `fill --text-stdin` keeps the value out of argv, so it must not come back on the |
| 4 | + * response either. Sent through the real request handler to a web session, the narrowest |
| 5 | + * runtime that still runs the full fill admission, dispatch and response projection. |
| 6 | + */ |
| 7 | +import { expect, test } from 'vitest'; |
| 8 | +import path from 'node:path'; |
| 9 | +import type { WebProvider } from '@agent-device/platform-web'; |
| 10 | +import type { SessionState } from '../session-state.ts'; |
| 11 | +import { createRequestHandler } from './test-device-runtime-gateway.ts'; |
| 12 | +import { LeaseRegistry } from '../lease-registry.ts'; |
| 13 | +import { makeSessionStore } from '../../__tests__/test-utils/store-factory.ts'; |
| 14 | +import { makeAuthoringSession, makeSession } from '../../__tests__/test-utils/session-factories.ts'; |
| 15 | +import { WEB_DESKTOP_DEVICE } from '../../__tests__/test-utils/device-fixtures.ts'; |
| 16 | +import { mkdtempForTestSync } from '../../__tests__/test-utils/tmp-dir.ts'; |
| 17 | +import { |
| 18 | + createPlatformRuntimeGateway, |
| 19 | + createRequestPlatformProviders, |
| 20 | +} from '../../platform-runtime.ts'; |
| 21 | + |
| 22 | +const SECRET = 'stdin-s3cret-value'; |
| 23 | + |
| 24 | +function fillWithStdinText( |
| 25 | + session: SessionState, |
| 26 | + flags: Record<string, unknown> = {}, |
| 27 | + fillFails?: (text: string) => Error, |
| 28 | +) { |
| 29 | + const sessionStore = makeSessionStore('agent-device-router-fill-text-stdin-'); |
| 30 | + sessionStore.publish(session.name, session); |
| 31 | + const typed: string[] = []; |
| 32 | + const webProvider: WebProvider = { |
| 33 | + open: async () => {}, |
| 34 | + close: async () => {}, |
| 35 | + snapshot: async () => ({ nodes: [] }), |
| 36 | + screenshot: async () => {}, |
| 37 | + setViewport: async () => {}, |
| 38 | + click: async () => {}, |
| 39 | + fill: async (_x, _y, text) => { |
| 40 | + typed.push(text); |
| 41 | + if (fillFails) throw fillFails(text); |
| 42 | + }, |
| 43 | + typeText: async () => {}, |
| 44 | + scroll: async () => {}, |
| 45 | + }; |
| 46 | + const dir = mkdtempForTestSync('agent-device-router-fill-text-stdin-runtime-'); |
| 47 | + const handler = createRequestHandler({ |
| 48 | + logPath: path.join(dir, 'daemon.log'), |
| 49 | + token: 'test-token', |
| 50 | + sessionStore, |
| 51 | + leaseRegistry: new LeaseRegistry(), |
| 52 | + deviceInventoryGateways: createTestDeviceInventoryGateways(), |
| 53 | + requestPlatformProviders: createRequestPlatformProviders({ |
| 54 | + providers: { webProvider: () => webProvider }, |
| 55 | + }), |
| 56 | + deviceRuntimeGateway: createPlatformRuntimeGateway({ |
| 57 | + sessionsDir: dir, |
| 58 | + resolveSessionArtifacts: (sessionId) => ({ |
| 59 | + outputPath: path.join(dir, sessionId, 'app.log'), |
| 60 | + pidPath: path.join(dir, sessionId, 'app-log.pid'), |
| 61 | + }), |
| 62 | + }), |
| 63 | + trackDownloadableArtifact: () => 'artifact-id', |
| 64 | + }); |
| 65 | + const response = handler({ |
| 66 | + token: 'test-token', |
| 67 | + session: session.name, |
| 68 | + command: 'fill', |
| 69 | + positionals: ['10', '20', SECRET], |
| 70 | + flags: { textStdin: true, ...flags }, |
| 71 | + meta: { requestId: 'req-fill-text-stdin' }, |
| 72 | + }); |
| 73 | + return { response, typed, sessionStore }; |
| 74 | +} |
| 75 | + |
| 76 | +test('an unrecorded --text-stdin fill types the value but returns no part of it', async () => { |
| 77 | + const { response, typed } = fillWithStdinText(makeSession('web', { device: WEB_DESKTOP_DEVICE })); |
| 78 | + |
| 79 | + const result = await response; |
| 80 | + expect(result.ok).toBe(true); |
| 81 | + expect(typed).toEqual([SECRET]); |
| 82 | + expect(JSON.stringify(result)).not.toContain(SECRET); |
| 83 | + if (result.ok) expect(result.data?.text).toBe('[REDACTED]'); |
| 84 | +}); |
| 85 | + |
| 86 | +test('an armed --text-stdin --no-record fill returns no part of the value and records nothing', async () => { |
| 87 | + const session = makeAuthoringSession('web', { device: WEB_DESKTOP_DEVICE }); |
| 88 | + const { response, typed, sessionStore } = fillWithStdinText(session, { noRecord: true }); |
| 89 | + |
| 90 | + const result = await response; |
| 91 | + expect(result.ok).toBe(true); |
| 92 | + expect(typed).toEqual([SECRET]); |
| 93 | + expect(JSON.stringify(result)).not.toContain(SECRET); |
| 94 | + expect(sessionStore.get('web')?.actions).toEqual([]); |
| 95 | +}); |
| 96 | + |
| 97 | +test('an armed --text-stdin fill without --record-as or --no-record is refused before typing', async () => { |
| 98 | + const session = makeAuthoringSession('web', { device: WEB_DESKTOP_DEVICE }); |
| 99 | + const { response, typed } = fillWithStdinText(session); |
| 100 | + |
| 101 | + const result = await response; |
| 102 | + expect(result.ok).toBe(false); |
| 103 | + if (result.ok) return; |
| 104 | + expect(result.error.code).toBe('INVALID_ARGS'); |
| 105 | + expect(result.error.details?.reason).toBe('fill_text_stdin_unparameterized_recording'); |
| 106 | + expect(typed).toEqual([]); |
| 107 | + expect(JSON.stringify(result)).not.toContain(SECRET); |
| 108 | +}); |
| 109 | + |
| 110 | +test('a backend error that echoes the --text-stdin value does not return it', async () => { |
| 111 | + const { response, typed } = fillWithStdinText( |
| 112 | + makeSession('web', { device: WEB_DESKTOP_DEVICE }), |
| 113 | + {}, |
| 114 | + (text) => new Error(`could not type "${text}" into the field`), |
| 115 | + ); |
| 116 | + |
| 117 | + const result = await response; |
| 118 | + expect(result.ok).toBe(false); |
| 119 | + expect(typed).toEqual([SECRET]); |
| 120 | + expect(JSON.stringify(result)).not.toContain(SECRET); |
| 121 | + if (!result.ok) expect(result.error.message).toBe('could not type "[REDACTED]" into the field'); |
| 122 | +}); |
0 commit comments