|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import os from 'node:os'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { test } from 'vitest'; |
| 6 | +import { createAgentDeviceClient } from '../../../src/agent-device-client.ts'; |
| 7 | +import { |
| 8 | + cleanupDownloadableArtifact, |
| 9 | + trackDownloadableArtifact, |
| 10 | +} from '../../../src/daemon/artifact-tracking.ts'; |
| 11 | +import { finalizeDaemonResponse } from '../../../src/daemon/request-finalization.ts'; |
| 12 | +import { createDaemonHttpServer } from '../../../src/daemon/server/http-server.ts'; |
| 13 | +import { normalizeAgentDeviceError } from '../../../src/kernel/errors.ts'; |
| 14 | +import { downloadRemoteArtifact } from '../../../src/remote/daemon-artifacts.ts'; |
| 15 | +import { createDaemonProxyServer } from '../../../src/remote/daemon-proxy.ts'; |
| 16 | +import { |
| 17 | + closeLoopbackServer, |
| 18 | + listenOnLoopback, |
| 19 | + skipWhenLoopbackUnavailable, |
| 20 | +} from '../../../src/__tests__/test-utils/loopback.ts'; |
| 21 | + |
| 22 | +const TENANT = 'local-proxy-tenant'; |
| 23 | +const OTHER_TENANT = 'other-tenant'; |
| 24 | + |
| 25 | +test('Provider-backed integration local proxy materializes tenant-scoped screenshots', async (t) => { |
| 26 | + if (await skipWhenLoopbackUnavailable(t, 'local proxy artifact tenant coverage')) return; |
| 27 | + |
| 28 | + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-proxy-artifact-tenant-')); |
| 29 | + const remoteScreenshotPath = path.join(tempDir, 'remote-shot.png'); |
| 30 | + const localScreenshotPath = path.join(tempDir, 'local-shot.png'); |
| 31 | + const rejectedScreenshotPath = path.join(tempDir, 'rejected-shot.png'); |
| 32 | + fs.writeFileSync(remoteScreenshotPath, 'tenant-scoped-png'); |
| 33 | + const artifactIds: string[] = []; |
| 34 | + const upstream = await createDaemonHttpServer({ |
| 35 | + token: 'upstream-token', |
| 36 | + handleRequest: async (req) => { |
| 37 | + assert.equal(req.command, 'screenshot'); |
| 38 | + assert.equal(req.meta?.tenantId, TENANT); |
| 39 | + assert.equal(req.meta?.runId, 'local-proxy-run'); |
| 40 | + assert.equal(req.meta?.sessionIsolation, 'tenant'); |
| 41 | + return finalizeDaemonResponse( |
| 42 | + req, |
| 43 | + { ok: true, data: { path: remoteScreenshotPath } }, |
| 44 | + (artifact) => { |
| 45 | + const artifactId = trackDownloadableArtifact(artifact); |
| 46 | + artifactIds.push(artifactId); |
| 47 | + return artifactId; |
| 48 | + }, |
| 49 | + ); |
| 50 | + }, |
| 51 | + }); |
| 52 | + const protectedArtifactId = trackDownloadableArtifact({ |
| 53 | + artifactPath: remoteScreenshotPath, |
| 54 | + tenantId: TENANT, |
| 55 | + artifactType: 'screenshot', |
| 56 | + fileName: 'remote-shot.png', |
| 57 | + }); |
| 58 | + artifactIds.push(protectedArtifactId); |
| 59 | + const proxy = createDaemonProxyServer({ |
| 60 | + upstreamBaseUrl: `http://127.0.0.1:${await listenOnLoopback(upstream)}`, |
| 61 | + upstreamToken: 'upstream-token', |
| 62 | + clientToken: 'proxy-token', |
| 63 | + }); |
| 64 | + |
| 65 | + try { |
| 66 | + const proxyPort = await listenOnLoopback(proxy); |
| 67 | + const daemonBaseUrl = `http://127.0.0.1:${proxyPort}/agent-device`; |
| 68 | + |
| 69 | + await assert.rejects( |
| 70 | + async () => |
| 71 | + await downloadRemoteArtifact({ |
| 72 | + baseUrl: daemonBaseUrl, |
| 73 | + token: 'proxy-token', |
| 74 | + tenantId: OTHER_TENANT, |
| 75 | + artifactId: protectedArtifactId, |
| 76 | + destinationPath: rejectedScreenshotPath, |
| 77 | + }), |
| 78 | + (error: unknown) => { |
| 79 | + const normalized = normalizeAgentDeviceError(error); |
| 80 | + assert.equal(normalized.details?.statusCode, 401); |
| 81 | + assert.match(String(normalized.details?.body), /different tenant/i); |
| 82 | + return true; |
| 83 | + }, |
| 84 | + ); |
| 85 | + assert.equal(fs.existsSync(rejectedScreenshotPath), false); |
| 86 | + |
| 87 | + const client = createAgentDeviceClient({ |
| 88 | + daemonBaseUrl, |
| 89 | + daemonAuthToken: 'proxy-token', |
| 90 | + tenant: TENANT, |
| 91 | + runId: 'local-proxy-run', |
| 92 | + sessionIsolation: 'tenant', |
| 93 | + stateDir: tempDir, |
| 94 | + }); |
| 95 | + const screenshot = await client.capture.screenshot({ path: localScreenshotPath }); |
| 96 | + |
| 97 | + assert.equal(screenshot.path, localScreenshotPath); |
| 98 | + assert.equal(fs.readFileSync(localScreenshotPath, 'utf8'), 'tenant-scoped-png'); |
| 99 | + } finally { |
| 100 | + for (const artifactId of artifactIds) cleanupDownloadableArtifact(artifactId); |
| 101 | + await closeLoopbackServer(proxy); |
| 102 | + await closeLoopbackServer(upstream); |
| 103 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 104 | + } |
| 105 | +}); |
0 commit comments