|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { afterEach, test, vi } from 'vitest'; |
| 3 | +import type { ProviderPluginHost } from 'agent-device/plugins'; |
| 4 | +import { AppError } from '@agent-device/kernel/errors'; |
| 5 | +import testmu from './index.ts'; |
| 6 | +import { verifyTestMuConnection } from './testmu-connection-verification.ts'; |
| 7 | +import manifest from '../package.json' with { type: 'json' }; |
| 8 | + |
| 9 | +vi.mock('./testmu-connection-verification.ts', () => ({ verifyTestMuConnection: vi.fn() })); |
| 10 | + |
| 11 | +const realFetch = globalThis.fetch; |
| 12 | + |
| 13 | +afterEach(() => { |
| 14 | + globalThis.fetch = realFetch; |
| 15 | + vi.clearAllMocks(); |
| 16 | +}); |
| 17 | + |
| 18 | +function host(env: Record<string, string | undefined>): ProviderPluginHost { |
| 19 | + return { |
| 20 | + env, |
| 21 | + options: {}, |
| 22 | + clientVersion: '0.0.0-test', |
| 23 | + createError: (code, message, details) => new AppError(code, message, details), |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +function capturedAuthorization(): { read(): string | undefined } { |
| 28 | + let authorization: string | undefined; |
| 29 | + globalThis.fetch = async (_input, init) => { |
| 30 | + authorization = new Headers(init?.headers).get('authorization') ?? undefined; |
| 31 | + return new Response(JSON.stringify({ data: {} })); |
| 32 | + }; |
| 33 | + return { read: () => authorization }; |
| 34 | +} |
| 35 | + |
| 36 | +async function listArtifacts(plugin: ReturnType<typeof testmu>, env: Record<string, string>) { |
| 37 | + const setup = await plugin.setup(host(env)); |
| 38 | + if (!('webDriver' in setup)) throw new Error('TestMu AI sets up a WebDriver descriptor'); |
| 39 | + await setup.webDriver.listArtifacts!({ provider: 'testmu', providerSessionId: 'SESSION1' }); |
| 40 | +} |
| 41 | + |
| 42 | +const basic = (username: string, accessKey: string) => |
| 43 | + `Basic ${Buffer.from(`${username}:${accessKey}`).toString('base64')}`; |
| 44 | + |
| 45 | +test('the plugin declares the LT variables only when it reads credentials from the environment', () => { |
| 46 | + assert.deepEqual(testmu().credentials, { variables: ['LT_USERNAME', 'LT_ACCESS_KEY'] }); |
| 47 | + assert.deepEqual(testmu({ credentials: { username: 'u', accessKey: 'k' } }).credentials, { |
| 48 | + variables: [], |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +test('credentials passed as options replace the environment, which stays the fallback', async () => { |
| 53 | + const captured = capturedAuthorization(); |
| 54 | + const env = { LT_USERNAME: 'env-user', LT_ACCESS_KEY: 'env-key' }; |
| 55 | + |
| 56 | + await listArtifacts(testmu({ credentials: { username: 'opt-user', accessKey: 'opt-key' } }), env); |
| 57 | + assert.equal(captured.read(), basic('opt-user', 'opt-key')); |
| 58 | + await listArtifacts(testmu({ credentials: { username: 'opt-user', accessKey: 'opt-key' } }), {}); |
| 59 | + assert.equal(captured.read(), basic('opt-user', 'opt-key')); |
| 60 | + await listArtifacts(testmu(), env); |
| 61 | + assert.equal(captured.read(), basic('env-user', 'env-key')); |
| 62 | + await assert.rejects(listArtifacts(testmu(), {}), { code: 'INVALID_ARGS' }); |
| 63 | +}); |
| 64 | + |
| 65 | +test('connect uses the option credentials without an environment and verifies with them', async () => { |
| 66 | + vi.mocked(verifyTestMuConnection).mockResolvedValue({ |
| 67 | + provider: 'testmu', |
| 68 | + service: 'TestMu AI', |
| 69 | + verificationMessage: 'verified', |
| 70 | + device: { status: 'verified', name: 'iPhone 16', platform: 'ios', osVersion: '18.0' }, |
| 71 | + app: { status: 'verified', reference: 'lt://APP1' }, |
| 72 | + }); |
| 73 | + const { connection } = testmu({ credentials: { username: 'opt-user', accessKey: 'opt-key' } }); |
| 74 | + const flags = { |
| 75 | + json: false, |
| 76 | + help: false, |
| 77 | + version: false, |
| 78 | + platform: 'ios', |
| 79 | + device: 'iPhone 16', |
| 80 | + providerOsVersion: '18.0', |
| 81 | + providerApp: 'lt://APP1', |
| 82 | + } as const; |
| 83 | + |
| 84 | + const resolved = await connection!.resolve({ |
| 85 | + flags, |
| 86 | + stateDir: '/state', |
| 87 | + cwd: '/work', |
| 88 | + env: {}, |
| 89 | + host: host({}), |
| 90 | + }); |
| 91 | + assert.equal(resolved.profile.leaseProvider, 'testmu'); |
| 92 | + await connection!.verify({ flags, env: {}, host: host({}) }); |
| 93 | + assert.deepEqual( |
| 94 | + [ |
| 95 | + vi.mocked(verifyTestMuConnection).mock.calls[0]?.[0].username, |
| 96 | + vi.mocked(verifyTestMuConnection).mock.calls[0]?.[0].accessKey, |
| 97 | + ], |
| 98 | + ['opt-user', 'opt-key'], |
| 99 | + ); |
| 100 | + assert.throws( |
| 101 | + () => |
| 102 | + testmu().connection!.resolve({ |
| 103 | + flags, |
| 104 | + stateDir: '/state', |
| 105 | + cwd: '/work', |
| 106 | + env: {}, |
| 107 | + host: host({}), |
| 108 | + }), |
| 109 | + { code: 'INVALID_ARGS' }, |
| 110 | + ); |
| 111 | +}); |
| 112 | + |
| 113 | +test('the v2 declaration matches the v1 manifest that hosts without the v2 contract read', () => { |
| 114 | + const v1 = manifest.agentDevicePlugin; |
| 115 | + assert.equal(testmu().provider, v1.provider); |
| 116 | + assert.deepEqual(testmu().connection?.capabilities, v1.connection); |
| 117 | + assert.deepEqual(testmu().credentials.variables, v1.credentialVariables); |
| 118 | + assert.equal(v1.apiVersion, 1); |
| 119 | +}); |
| 120 | + |
| 121 | +test('a credentials object missing one key falls back to the environment for that key', async () => { |
| 122 | + const captured = capturedAuthorization(); |
| 123 | + await listArtifacts(testmu({ credentials: { username: 'opt-user' } as never }), { |
| 124 | + LT_USERNAME: 'env-user', |
| 125 | + LT_ACCESS_KEY: 'env-key', |
| 126 | + }); |
| 127 | + assert.equal(captured.read(), basic('opt-user', 'env-key')); |
| 128 | +}); |
0 commit comments