Skip to content
Merged
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
7 changes: 7 additions & 0 deletions packages/daemon/src/resources/artifact-tracking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,16 @@ test.each(['deadline-aborted', 'late-aborted'] as const)(
const succeeded = new Promise<void>((resolve) => {
tarSucceeded = resolve;
});
let tarStarted!: () => void;
const started = new Promise<void>((resolve) => {
tarStarted = resolve;
});
let firstArchiveDir = '';
const runCmd = command.runCmd;
const archive = vi.spyOn(command, 'runCmd').mockImplementationOnce(async (...args) => {
firstArchiveDir = path.dirname(String(args[1]?.[1]));
args[2]?.signal?.addEventListener('abort', aborted, { once: true });
tarStarted();
if (failure === 'late-aborted') {
const result = await runCmd(...args);
tarSucceeded();
Expand All @@ -242,6 +247,8 @@ test.each(['deadline-aborted', 'late-aborted'] as const)(
void first.catch(() => {});
const deadline = timers.mock.calls.find(([, delay]) => delay === 15 * 60 * 1000)?.[0];
timers.mockRestore();
// The packer loads on demand, so tar starts a tick after the open; the deadline fires once it runs.
await started;
if (failure === 'late-aborted') await succeeded;
expect(deadline).toBeTypeOf('function');
runtime.run(() => deadline!());
Expand Down
49 changes: 16 additions & 33 deletions packages/daemon/src/resources/artifact-tracking.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { runtimeSlot } from '@agent-device/host-kit/runtime-slots';
import crypto from 'node:crypto';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { finished } from 'node:stream/promises';
import { AppError, createRequestCanceledError } from '@agent-device/kernel/errors';
import type { DaemonArtifactType } from '@agent-device/kernel/contracts';
import type { DeviceRotation } from '@agent-device/contracts/device';
import type { DaemonArtifactInventoryEntry } from '@agent-device/contracts/observability';
import { runCmd } from '@agent-device/host-kit/command';
import type { PackedGzipTar } from '@agent-device/host-kit/gzip-tar';
import {
expiredTenantOwnedEntryError,
requireTenantOwnedEntry,
Expand All @@ -29,7 +28,7 @@ const DIRECTORY_ARCHIVE_MIME_TYPE = 'application/gzip';

type DirectoryArchive = {
archivePath: string;
tempDir: string;
cleanup(): void;
fileName: string;
sizeBytes: number;
};
Expand Down Expand Up @@ -389,7 +388,7 @@ async function createDirectoryArchive(
hostEnv: NodeJS.ProcessEnv,
controller: AbortController,
): Promise<DirectoryArchive> {
let tempDir: string | undefined;
let packed: PackedGzipTar | undefined;
try {
cleanupDirectoryArchive(entry);

Expand All @@ -398,35 +397,19 @@ async function createDirectoryArchive(
path.basename(entry.artifactPath) || artifactId,
);
const fileName = archiveFilename(sourceName);
tempDir = fs.mkdtempSync(
path.join(os.tmpdir(), `agent-device-download-${crypto.randomUUID()}-`),
);
const archivePath = path.join(tempDir, fileName);
await runCmd(
'tar',
[
'czf',
archivePath,
'-C',
path.dirname(entry.artifactPath),
'--',
path.basename(entry.artifactPath),
],
{
signal: controller.signal,
timeoutMs: ARTIFACT_DOWNLOAD_MAX_LIFETIME_MS,
env: {
...hostEnv,
COPYFILE_DISABLE: '1',
},
},
);
controller.signal.throwIfAborted();
// Loaded on demand: only a directory artifact packs, and the daemon's eager closure stays small.
const { packGzipTar } = await import('@agent-device/host-kit/gzip-tar');
packed = await packGzipTar(entry.artifactPath, {
env: hostEnv,
signal: controller.signal,
timeoutMs: ARTIFACT_DOWNLOAD_MAX_LIFETIME_MS,
fileName,
});
const archive = {
archivePath,
tempDir,
archivePath: packed.archivePath,
cleanup: packed.cleanup,
fileName,
sizeBytes: fs.statSync(archivePath).size,
sizeBytes: fs.statSync(packed.archivePath).size,
};
entry.directoryArchive = archive;
return archive;
Expand All @@ -441,7 +424,7 @@ async function createDirectoryArchive(
{ artifactPath: entry.artifactPath },
error instanceof Error ? error : undefined,
);
if (tempDir) fs.rmSync(tempDir, { recursive: true, force: true });
packed?.cleanup();
throw failure;
}
}
Expand Down Expand Up @@ -510,7 +493,7 @@ function removeBestEffort(targetPath: string): void {

function cleanupDirectoryArchive(entry: ArtifactEntry): void {
if (!entry.directoryArchive) return;
removeBestEffort(entry.directoryArchive.tempDir);
entry.directoryArchive.cleanup();
entry.directoryArchive = undefined;
}

Expand Down
4 changes: 4 additions & 0 deletions packages/host-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
"types": "./src/file.ts",
"default": "./src/file.ts"
},
"./gzip-tar": {
"types": "./src/gzip-tar.ts",
"default": "./src/gzip-tar.ts"
},
"./host-file": {
"types": "./src/host-file.ts",
"default": "./src/host-file.ts"
Expand Down
80 changes: 80 additions & 0 deletions packages/host-kit/src/gzip-tar.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { afterEach, test } from 'vitest';
import { runCmd, withCommandExecutorOverride } from './command.ts';
import { packGzipTar } from './gzip-tar.ts';
import { mkdtempForTestSync } from './internal/tmp-dir.fixtures.ts';

const roots: string[] = [];
afterEach(() => {
for (const root of roots.splice(0)) fs.rmSync(root, { recursive: true, force: true });
});

function bundle(name: string): string {
const root = mkdtempForTestSync('gzip-tar-fixture-');
roots.push(root);
const directory = path.join(root, name);
fs.mkdirSync(directory);
fs.writeFileSync(path.join(directory, 'Info.plist'), 'plist');
return directory;
}

async function entries(archivePath: string): Promise<string[]> {
const { stdout } = await runCmd('tar', ['tzf', archivePath], { env: process.env });
return stdout.split('\n').filter(Boolean).sort();
}

test('packs the directory as the archive’s one top-level entry, and cleanup removes it', async () => {
const packed = await packGzipTar(bundle('Example.app'), { env: process.env });
assert.equal(path.basename(packed.archivePath), 'Example.app.tar.gz');
assert.deepEqual(await entries(packed.archivePath), ['Example.app/', 'Example.app/Info.plist']);
packed.cleanup();
assert.equal(fs.existsSync(path.dirname(packed.archivePath)), false);
packed.cleanup();
});

test('a directory name that starts with a dash is packed as a name, not read as an option', async () => {
const packed = await packGzipTar(bundle('-v.app'), { env: process.env, fileName: 'build.tgz' });
try {
assert.equal(path.basename(packed.archivePath), 'build.tgz');
assert.deepEqual(await entries(packed.archivePath), ['-v.app/', '-v.app/Info.plist']);
} finally {
packed.cleanup();
}
});

test('a pack that fails leaves no temporary directory behind', async () => {
let archivePath = '';
await assert.rejects(
withCommandExecutorOverride(
(cmd, args) => {
if (cmd !== 'tar') return undefined;
archivePath = String(args[1]);
throw new Error('tar unavailable');
},
async () => await packGzipTar(bundle('Example.app'), { env: process.env }),
),
/tar unavailable/,
);
assert.notEqual(archivePath, '');
assert.equal(fs.existsSync(path.dirname(archivePath)), false);
});

test('an already-aborted pack starts no tar and leaves nothing behind', async () => {
const controller = new AbortController();
controller.abort(new Error('canceled'));
let ran = false;
await assert.rejects(
withCommandExecutorOverride(
(cmd) => {
if (cmd === 'tar') ran = true;
return undefined;
},
async () =>
await packGzipTar(bundle('Example.app'), { env: process.env, signal: controller.signal }),
),
/canceled/,
);
assert.equal(ran, false);
});
57 changes: 57 additions & 0 deletions packages/host-kit/src/gzip-tar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { randomUUID } from 'node:crypto';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
// Through the public module, so a test that stubs `runCmd` there also stubs the pack.
import { runCmd } from './command.ts';

export type GzipTarPackOptions = Readonly<{
/** The host environment `tar` runs with. */
env: Readonly<Record<string, string | undefined>>;
signal?: AbortSignal;
timeoutMs?: number;
/** The archive's file name; `<directory name>.tar.gz` when omitted. */
fileName?: string;
}>;

export type PackedGzipTar = Readonly<{
archivePath: string;
/** Removes the archive and its temporary directory. Safe to call more than once. */
cleanup(): void;
}>;

/**
* Packs a directory, such as a simulator `.app`, into a gzip tar in a fresh temporary directory, for
* upload or download. The archive holds the directory itself as its one top-level entry. On failure
* nothing is left behind.
*/
export async function packGzipTar(
directory: string,
options: GzipTarPackOptions,
): Promise<PackedGzipTar> {
options.signal?.throwIfAborted();
const name = path.basename(directory);
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), `agent-device-pack-${randomUUID()}-`));
const cleanup = () => {
try {
fs.rmSync(tempDir, { recursive: true, force: true });
} catch {
// best-effort cleanup only
}
};
const archivePath = path.join(tempDir, options.fileName ?? `${name}.tar.gz`);
try {
// `--` keeps a directory name that starts with `-` from being read as an option.
await runCmd('tar', ['czf', archivePath, '-C', path.dirname(directory), '--', name], {
// Keeps macOS tar from adding AppleDouble `._` entries.
env: { ...options.env, COPYFILE_DISABLE: '1' },
...(options.signal === undefined ? {} : { signal: options.signal }),
...(options.timeoutMs === undefined ? {} : { timeoutMs: options.timeoutMs }),
});
options.signal?.throwIfAborted();
} catch (error) {
cleanup();
throw error;
}
return { archivePath, cleanup };
}
3 changes: 2 additions & 1 deletion packages/provider-revyl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ npm install -g agent-device
agent-device plugins add @agent-device/revyl
export REVYL_API_KEY=your-api-key
export AGENT_DEVICE_SESSION=checkout-ios
agent-device connect revyl --platform ios --provider-app https://apps.example.com/MyApp-simulator.zip
agent-device connect revyl --platform ios
agent-device install com.example.app ./build/MyApp.app
agent-device open com.example.app
agent-device snapshot -i
agent-device close
Expand Down
3 changes: 2 additions & 1 deletion packages/provider-revyl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
"devDependencies": {
"@agent-device/capture-kit": "workspace:*",
"@agent-device/contracts": "workspace:*",
"@agent-device/host-kit": "workspace:*",
"@agent-device/kernel": "workspace:*",
"@agent-device/provision-kit": "workspace:*",
"@agent-device/provider-webdriver": "workspace:*",
"@agent-device/provision-kit": "workspace:*",
"@agent-device/test-kit": "workspace:*",
"agent-device": "workspace:*"
},
Expand Down
29 changes: 26 additions & 3 deletions packages/provider-revyl/src/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ test('verify authenticates with the cheap call and reports the profile without r
// The literals are asserted so a reworded report fails the test instead of being compared with
// itself.
assert.deepEqual(result.device, {
status: 'configured',
status: 'deferred',
name: 'iPhone 16',
platform: 'ios',
osVersion: '18.0',
Expand All @@ -70,13 +70,27 @@ test('verify reports a profile that names no device or OS version', async () =>
{ appUrl: 'https://apps.test/app.apk' },
);
assert.deepEqual(result.device, {
status: 'configured',
status: 'deferred',
platform: 'android',
message:
'Revyl assigns a device of this platform when the session starts; a model or OS version it has no device for is reported then.',
});
});

test('verify reports a profile without an app as one the device starts without', async () => {
const fixture = await backend();
const result = await verifyRevylProfile(
{ apiKey: 'test-key', apiUrl: fixture.backendUrl },
'0.0.0-test',
'ios',
{},
);
assert.deepEqual(result.app, {
status: 'missing',
message: 'No app URL; the device starts without one. Install a build, then open it.',
});
});

test('verify reports a rejected key as an unauthorized error before checking the app', async () => {
const fixture = await backend({
respond: { 'GET /api/v1/entity/users/get_user_uuid': { status: 401, body: { error: 'nope' } } },
Expand Down Expand Up @@ -201,10 +215,19 @@ test('resolve keeps the device, OS version and app URL and refuses a non-mobile
);
});

test('resolve keeps a profile without an app, so the device starts with none', () => {
const connection = createRevylConnection(HOST);
const resolved = connection.resolve({
flags: { platform: 'android' } as CliFlags,
cwd: '/tmp',
} as never);
assert.equal('providerApp' in resolved.profile, false);
assert.equal('extraFlags' in resolved, false);
});

test('resolve refuses an app that is not an https URL', () => {
const connection = createRevylConnection(HOST);
for (const providerApp of [
undefined,
'./build/app.ipa',
'https://user@apps.test/app.zip',
'http://apps.test/app.zip',
Expand Down
Loading
Loading