Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import path from 'node:path';

import { type CustomBuildContext } from '../../customBuildContext';
import { Sentry } from '../../sentry';
import { pollAgentDeviceArtifactsForUploadAsync } from '../utils/agentDeviceArtifacts';
import {
type DetachedProcessHandle,
getDeviceRunSessionIdOrThrow,
Expand All @@ -28,10 +29,14 @@ import {
} from '../utils/remoteDeviceRunSession';

const AGENT_DEVICE_PACKAGE_NAME = 'agent-device';
const AGENT_DEVICE_REPO_URL = 'https://github.com/callstackincubator/agent-device.git';
const AGENT_DEVICE_REPO_URL = 'https://github.com/callstack/agent-device.git';
const SRC_DIR = '/tmp/agent-device-src';
const DAEMON_JSON_PATH = path.join(os.homedir(), '.agent-device', 'daemon.json');
const STARTUP_TIMEOUT_MS = 60_000;
const AGENT_DEVICE_DAEMON_ENV = {
AGENT_DEVICE_DAEMON_SERVER_MODE: 'http',
AGENT_DEVICE_RETAIN_ARTIFACTS: '1',
};

export function createStartAgentDeviceRemoteSessionBuildFunction(
ctx: CustomBuildContext
Expand Down Expand Up @@ -109,6 +114,12 @@ export function createStartAgentDeviceRemoteSessionBuildFunction(
},
logger,
});
void pollAgentDeviceArtifactsForUploadAsync(ctx, {
deviceRunSessionId,
daemonUrl: `http://127.0.0.1:${daemonPort}`,
daemonToken,
logger,
});

logger.info('Remote session is live. Keeping the job alive until the session is stopped.');
// Keep the turtle job alive so the daemon and tunnel stay reachable
Expand Down Expand Up @@ -144,7 +155,7 @@ async function startAgentDeviceDaemonAsync({
return spawnDetached({
command: 'node',
args: [daemonPath],
env: { ...env, AGENT_DEVICE_DAEMON_SERVER_MODE: 'http' },
env: { ...env, ...AGENT_DEVICE_DAEMON_ENV },
});
} catch (err) {
const error = err instanceof Error ? err : new Error(String(err));
Expand Down Expand Up @@ -201,7 +212,7 @@ async function startAgentDeviceDaemonFromGitAsync({
command: 'bun',
args: ['run', 'src/daemon.ts'],
cwd: SRC_DIR,
env: { ...env, AGENT_DEVICE_DAEMON_SERVER_MODE: 'http' },
env: { ...env, ...AGENT_DEVICE_DAEMON_ENV },
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
import { bunyan } from '@expo/logger';
import fetch from 'node-fetch';
import { Readable } from 'node:stream';

import { CustomBuildContext } from '../../../customBuildContext';
import { uploadDeviceRunSessionArtifactAsync } from '../deviceRunSessionArtifacts';
import {
listAgentDeviceArtifactsAsync,
pollAgentDeviceArtifactsForUploadAsync,
uploadAgentDeviceArtifactAsync,
} from '../agentDeviceArtifacts';

jest.mock('../deviceRunSessionArtifacts');
jest.mock('node-fetch');

const { Response } = jest.requireActual('node-fetch') as typeof import('node-fetch');

async function readStreamAsync(stream: NodeJS.ReadableStream): Promise<void> {
for await (const chunk of stream as Readable) {
void chunk;
}
}

async function flushPromisesAsync(): Promise<void> {
for (let i = 0; i < 10; i++) {
await jest.advanceTimersByTimeAsync(0);
await Promise.resolve();
}
}

function createLoggerMock(): bunyan {
return {
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
debug: jest.fn(),
} as unknown as bunyan;
}

describe(listAgentDeviceArtifactsAsync, () => {
beforeEach(() => {
jest.mocked(fetch).mockReset();
});

it('lists agent-device artifacts with bearer auth', async () => {
jest.mocked(fetch).mockResolvedValue(
new Response(
JSON.stringify({
artifacts: [
{
id: 'artifact-id',
filename: 'report.json',
mimeType: 'application/json',
sizeBytes: 123,
createdAt: '2026-07-02T12:00:00.000Z',
expiresAt: '2026-07-02T12:15:00.000Z',
},
],
})
)
);

const artifacts = await listAgentDeviceArtifactsAsync({
daemonUrl: 'http://127.0.0.1:1234',
daemonToken: 'daemon-token',
});

expect(artifacts).toEqual([
{
id: 'artifact-id',
filename: 'report.json',
mimeType: 'application/json',
sizeBytes: 123,
createdAt: '2026-07-02T12:00:00.000Z',
expiresAt: '2026-07-02T12:15:00.000Z',
},
]);
expect(jest.mocked(fetch)).toHaveBeenCalledWith('http://127.0.0.1:1234/artifacts', {
headers: { Authorization: 'Bearer daemon-token' },
});
});

it('reports artifact inventory as unsupported when the daemon does not expose the endpoint', async () => {
jest.mocked(fetch).mockResolvedValue(new Response('Not found', { status: 404 }));

await expect(
listAgentDeviceArtifactsAsync({
daemonUrl: 'http://127.0.0.1:1234',
daemonToken: 'daemon-token',
})
).rejects.toThrow('agent-device daemon does not expose artifact inventory.');
});
});

describe(uploadAgentDeviceArtifactAsync, () => {
beforeEach(() => {
jest.mocked(fetch).mockReset();
jest.mocked(uploadDeviceRunSessionArtifactAsync).mockReset();
});

it('downloads an agent-device artifact and uploads it as a device run session artifact', async () => {
const data = Buffer.from('artifact-data');
const logger = createLoggerMock();
const ctx = {} as unknown as CustomBuildContext;

jest.mocked(fetch).mockResolvedValueOnce(new Response(Readable.from([data])));
jest
.mocked(uploadDeviceRunSessionArtifactAsync)
.mockImplementationOnce(async (_ctx, { stream }) => {
await readStreamAsync(stream);
});

await uploadAgentDeviceArtifactAsync(ctx, {
deviceRunSessionId: 'drs-id',
daemonUrl: 'http://127.0.0.1:1234',
daemonToken: 'daemon-token',
logger,
artifact: {
id: 'artifact-id',
filename: 'report.json',
mimeType: 'application/json',
sizeBytes: data.length,
createdAt: '2026-07-02T12:00:00.000Z',
expiresAt: '2026-07-02T12:15:00.000Z',
},
});

expect(jest.mocked(fetch)).toHaveBeenCalledWith('http://127.0.0.1:1234/artifacts/artifact-id', {
headers: { Authorization: 'Bearer daemon-token' },
});
expect(jest.mocked(uploadDeviceRunSessionArtifactAsync)).toHaveBeenCalledWith(ctx, {
deviceRunSessionId: 'drs-id',
artifactId: 'artifact-id',
name: 'report.json (artifact-id)',
filename: 'report.json',
size: data.length,
stream: expect.anything(),
});
});
});

describe(pollAgentDeviceArtifactsForUploadAsync, () => {
beforeEach(() => {
jest.useFakeTimers();
jest.mocked(fetch).mockReset();
jest.mocked(uploadDeviceRunSessionArtifactAsync).mockReset();
});

afterEach(() => {
jest.useRealTimers();
});
Comment on lines +149 to +151

it('retries an artifact after a failed upload', async () => {
const data = Buffer.from('artifact-data');
const logger = createLoggerMock();
const ctx = {} as unknown as CustomBuildContext;
const artifact = {
id: 'artifact-id',
filename: 'report.json',
mimeType: 'application/json',
sizeBytes: data.length,
createdAt: '2026-07-02T12:00:00.000Z',
expiresAt: '2026-07-02T12:15:00.000Z',
};
const listResponse = () => new Response(JSON.stringify({ artifacts: [artifact] }));

jest
.mocked(fetch)
.mockResolvedValueOnce(listResponse())
.mockResolvedValueOnce(new Response(Readable.from([data])))
.mockResolvedValueOnce(listResponse())
.mockResolvedValueOnce(new Response(Readable.from([data])));
jest
.mocked(uploadDeviceRunSessionArtifactAsync)
.mockRejectedValueOnce(new Error('upload failed'))
.mockImplementationOnce(async (_ctx, { stream }) => {
await readStreamAsync(stream);
});

void pollAgentDeviceArtifactsForUploadAsync(ctx, {
deviceRunSessionId: 'drs-id',
daemonUrl: 'http://127.0.0.1:1234',
daemonToken: 'daemon-token',
logger,
});

await flushPromisesAsync();
expect(jest.mocked(uploadDeviceRunSessionArtifactAsync)).toHaveBeenCalledTimes(1);

await jest.advanceTimersByTimeAsync(5_000);
await flushPromisesAsync();

expect(jest.mocked(uploadDeviceRunSessionArtifactAsync)).toHaveBeenCalledTimes(2);
});
});
Loading
Loading