Repository navigation
test(daemon): cover lock-target matrix, auth-hook rejections, archive root inference, and longpress duration #3447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { test } from 'vitest'; | ||
| import assert from 'node:assert/strict'; | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { extractTarInstallableArtifact } from '../artifact-archive.ts'; | ||
| import { runCmdSync } from '@agent-device/host-kit/command'; | ||
| import { mkdtempForTestSync } from '../../__tests__/test-utils/tmp-dir.ts'; | ||
|
|
||
| const TAR_ENV = { ...process.env, COPYFILE_DISABLE: '1' }; | ||
|
|
||
| /** | ||
| * Builds a tar from the named top-level roots created under a fresh temp source | ||
| * dir, then extracts it with no `expectedRootName`, so the archive's own layout | ||
| * must decide the installable root (`inferArchiveRootName`). | ||
| */ | ||
| async function extractWithoutExpectedRoot( | ||
| rootNames: string[], | ||
| entries: Record<string, string>, | ||
| platform: 'ios' | 'android', | ||
| ): Promise<string> { | ||
| const tempRoot = mkdtempForTestSync('agent-device-artifact-archive-'); | ||
| const sourceRoot = path.join(tempRoot, 'src'); | ||
| fs.mkdirSync(sourceRoot, { recursive: true }); | ||
| for (const [name, contents] of Object.entries(entries)) { | ||
| const filePath = path.join(sourceRoot, name); | ||
| fs.mkdirSync(path.dirname(filePath), { recursive: true }); | ||
| fs.writeFileSync(filePath, contents); | ||
| } | ||
| const archivePath = path.join(tempRoot, 'artifact.tar'); | ||
| runCmdSync('tar', ['cf', archivePath, '-C', sourceRoot, ...rootNames], { env: TAR_ENV }); | ||
| const extractDir = path.join(tempRoot, 'out'); | ||
| fs.mkdirSync(extractDir); | ||
| return await extractTarInstallableArtifact({ archivePath, tempDir: extractDir, platform }); | ||
| } | ||
|
|
||
| test('an iOS tar infers the single top-level .app directory as the installable root', async () => { | ||
| const artifactPath = await extractWithoutExpectedRoot( | ||
| ['Sample.app'], | ||
| { 'Sample.app/payload.txt': 'payload' }, | ||
| 'ios', | ||
| ); | ||
| assert.equal(path.basename(artifactPath), 'Sample.app'); | ||
| assert.equal(fs.readFileSync(path.join(artifactPath, 'payload.txt'), 'utf8'), 'payload'); | ||
| }); | ||
|
|
||
| test('an iOS tar with no top-level .app directory is refused, not installed under a wrong root', async () => { | ||
| await assert.rejects( | ||
| async () => | ||
| await extractWithoutExpectedRoot(['payload'], { 'payload/data.txt': 'data' }, 'ios'), | ||
| /must contain a single top-level \.app directory/i, | ||
| ); | ||
| }); | ||
|
|
||
| test('an iOS tar with two top-level .app directories refuses and names both candidates', async () => { | ||
| await assert.rejects( | ||
| async () => | ||
| await extractWithoutExpectedRoot( | ||
| ['A.app', 'B.app'], | ||
| { 'A.app/x': '1', 'B.app/y': '2' }, | ||
| 'ios', | ||
| ), | ||
| /exactly one top-level \.app directory, found: A\.app, B\.app/i, | ||
| ); | ||
| }); | ||
|
|
||
| test('an Android tar installs its single top-level bundle whatever it is named', async () => { | ||
| const artifactPath = await extractWithoutExpectedRoot( | ||
| ['bundle'], | ||
| { 'bundle/base.apk': 'apk' }, | ||
| 'android', | ||
| ); | ||
| assert.equal(path.basename(artifactPath), 'bundle'); | ||
| }); | ||
|
|
||
| test('an Android tar with two top-level bundles is refused and names them', async () => { | ||
| await assert.rejects( | ||
| async () => | ||
| await extractWithoutExpectedRoot( | ||
| ['bundle-a', 'bundle-b'], | ||
| { 'bundle-a/x': '1', 'bundle-b/y': '2' }, | ||
| 'android', | ||
| ), | ||
| /must contain a single top-level bundle, found: bundle-a, bundle-b/i, | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { test } from 'vitest'; | ||
| import assert from 'node:assert/strict'; | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { createDaemonHttpServer } from './http-server.ts'; | ||
| import type { DaemonResponse } from '../daemon-request.ts'; | ||
| import { | ||
| closeLoopbackServer, | ||
| listenOnLoopback, | ||
| skipWhenLoopbackUnavailable, | ||
| } from '../../__tests__/test-utils/loopback.ts'; | ||
| import { mkdtempForTestSync } from '../../__tests__/test-utils/tmp-dir.ts'; | ||
| import { restoreEnv } from '../../__tests__/test-utils/env.ts'; | ||
|
|
||
| const DAEMON_TOKEN = 'daemon-secret'; | ||
|
|
||
| function writeAuthHook(root: string, source: string): string { | ||
| const hookPath = path.join(root, 'auth-hook.mjs'); | ||
| fs.writeFileSync(hookPath, source); | ||
| return hookPath; | ||
| } | ||
|
|
||
| /** | ||
| * Drives an auxiliary route (request diagnostics) through the shared | ||
| * token/auth-hook gate with a hook whose source is `hookSource`. The record | ||
| * never has to exist: a rejection answers before the record is resolved. | ||
| */ | ||
| async function withAuxRouteAndHook( | ||
| hookSource: string, | ||
| run: (ctx: { baseUrl: string }) => Promise<void>, | ||
| ): Promise<void> { | ||
| const root = mkdtempForTestSync('agent-device-http-auth-rejection-'); | ||
| const previousHook = process.env.AGENT_DEVICE_HTTP_AUTH_HOOK; | ||
| process.env.AGENT_DEVICE_HTTP_AUTH_HOOK = writeAuthHook(root, hookSource); | ||
| const server = await createDaemonHttpServer({ | ||
| token: DAEMON_TOKEN, | ||
| handleRequest: async (): Promise<DaemonResponse> => ({ ok: true, data: {} }), | ||
| resolveRequestDiagnosticsPath: () => path.join(root, 'unused-request-log.json'), | ||
| }); | ||
| try { | ||
| const port = await listenOnLoopback(server); | ||
| await run({ baseUrl: `http://127.0.0.1:${port}` }); | ||
| } finally { | ||
| await closeLoopbackServer(server); | ||
| restoreEnv('AGENT_DEVICE_HTTP_AUTH_HOOK', previousHook); | ||
| fs.rmSync(root, { recursive: true, force: true }); | ||
| } | ||
| } | ||
|
|
||
| async function fetchDiagnostics(baseUrl: string): Promise<Response> { | ||
| return await fetch(`${baseUrl}/sessions/default/requests/abc123/diagnostics`, { | ||
| headers: { authorization: `Bearer ${DAEMON_TOKEN}` }, | ||
| }); | ||
| } | ||
|
|
||
| // An auth hook's own rejection on an auxiliary route must reach the caller as the flat REST error | ||
| // shape those routes answer with (`{ ok: false, error }` at the hook's status code), not as a | ||
| // JSON-RPC envelope or a generic 401 that drops the hook's message. | ||
|
|
||
| test('aux route: a hook returning bare false answers 401 with the default rejection message', async (t) => { | ||
| if (await skipWhenLoopbackUnavailable(t)) return; | ||
| await withAuxRouteAndHook( | ||
| 'export default function authHook() { return false; }\n', | ||
| async ({ baseUrl }) => { | ||
| const response = await fetchDiagnostics(baseUrl); | ||
| assert.equal(response.status, 401); | ||
| assert.equal(response.headers.get('content-type'), 'application/json'); | ||
| const body = (await response.json()) as { ok?: boolean; error?: string }; | ||
| assert.equal(body.ok, false); | ||
| assert.equal(body.error, 'Request rejected by auth hook'); | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| test('aux route: a hook returning a structured rejection keeps its message and 401 status', async (t) => { | ||
| if (await skipWhenLoopbackUnavailable(t)) return; | ||
| await withAuxRouteAndHook( | ||
| "export default function authHook() { return { ok: false, code: 'FORBIDDEN', message: 'denied by policy' }; }\n", | ||
| async ({ baseUrl }) => { | ||
| const response = await fetchDiagnostics(baseUrl); | ||
| assert.equal(response.status, 401); | ||
| const body = (await response.json()) as { ok?: boolean; error?: string }; | ||
| assert.equal( | ||
| body.error, | ||
| 'denied by policy', | ||
| "the hook's own message must survive the REST rendering", | ||
| ); | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| test('aux route: a hook returning an invalid tenantId answers 500, not a 401 caller-retry trap', async (t) => { | ||
| if (await skipWhenLoopbackUnavailable(t)) return; | ||
| await withAuxRouteAndHook( | ||
| "export default function authHook() { return { tenantId: 'not a valid tenant!!' }; }\n", | ||
| async ({ baseUrl }) => { | ||
| const response = await fetchDiagnostics(baseUrl); | ||
| // The hook itself is broken (its attestation failed validation); that is a server fault, so | ||
| // the status must not masquerade as a client auth failure a retry could fix. | ||
| assert.equal(response.status, 500); | ||
| const body = (await response.json()) as { ok?: boolean; error?: string }; | ||
| assert.match(body.error ?? '', /invalid tenantId/i); | ||
| }, | ||
| ); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.