|
| 1 | +import test from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import fs from 'node:fs'; |
| 4 | +import os from 'node:os'; |
| 5 | +import path from 'node:path'; |
| 6 | +import { |
| 7 | + APP_LOG_PID_FILENAME, |
| 8 | + appendAppLogMarker, |
| 9 | + assertAndroidPackageArgSafe, |
| 10 | + buildIosDeviceLogStreamArgs, |
| 11 | + buildIosLogPredicate, |
| 12 | + cleanupStaleAppLogProcesses, |
| 13 | + getAppLogPathMetadata, |
| 14 | + runAppLogDoctor, |
| 15 | + rotateAppLogIfNeeded, |
| 16 | + stopAppLog, |
| 17 | +} from '../app-log.ts'; |
| 18 | + |
| 19 | +test('buildIosLogPredicate includes bundle-aware filters', () => { |
| 20 | + const predicate = buildIosLogPredicate('com.example.app'); |
| 21 | + assert.match(predicate, /subsystem == "com\.example\.app"/); |
| 22 | + assert.match(predicate, /processImagePath ENDSWITH\[c\] "\/com\.example\.app"/); |
| 23 | + assert.match(predicate, /senderImagePath ENDSWITH\[c\] "\/com\.example\.app"/); |
| 24 | + assert.match(predicate, /eventMessage CONTAINS\[c\] "com\.example\.app"/); |
| 25 | +}); |
| 26 | + |
| 27 | +test('assertAndroidPackageArgSafe rejects unsafe values', () => { |
| 28 | + assert.doesNotThrow(() => assertAndroidPackageArgSafe('com.example.app')); |
| 29 | + assert.throws(() => assertAndroidPackageArgSafe('com.example.app;rm -rf /'), /Invalid Android package/); |
| 30 | +}); |
| 31 | + |
| 32 | +test('rotateAppLogIfNeeded rotates and truncates oldest by configured max files', () => { |
| 33 | + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-app-log-rotate-')); |
| 34 | + const outPath = path.join(root, 'app.log'); |
| 35 | + fs.writeFileSync(outPath, 'a'.repeat(20)); |
| 36 | + fs.writeFileSync(`${outPath}.1`, 'old1'); |
| 37 | + fs.writeFileSync(`${outPath}.2`, 'old2'); |
| 38 | + |
| 39 | + rotateAppLogIfNeeded(outPath, { maxBytes: 10, maxRotatedFiles: 2 }); |
| 40 | + |
| 41 | + assert.equal(fs.existsSync(outPath), false); |
| 42 | + assert.equal(fs.readFileSync(`${outPath}.1`, 'utf8').length, 20); |
| 43 | + assert.equal(fs.readFileSync(`${outPath}.2`, 'utf8'), 'old1'); |
| 44 | +}); |
| 45 | + |
| 46 | +test('stopAppLog delegates stop and waits for completion', async () => { |
| 47 | + let stopped = false; |
| 48 | + let resolved = false; |
| 49 | + const wait = new Promise<{ stdout: string; stderr: string; exitCode: number }>((resolve) => { |
| 50 | + setTimeout(() => { |
| 51 | + resolved = true; |
| 52 | + resolve({ stdout: '', stderr: '', exitCode: 0 }); |
| 53 | + }, 5); |
| 54 | + }); |
| 55 | + await stopAppLog({ |
| 56 | + backend: 'android', |
| 57 | + getState: () => 'active', |
| 58 | + startedAt: Date.now(), |
| 59 | + stop: async () => { |
| 60 | + stopped = true; |
| 61 | + }, |
| 62 | + wait, |
| 63 | + }); |
| 64 | + assert.equal(stopped, true); |
| 65 | + assert.equal(resolved, true); |
| 66 | +}); |
| 67 | + |
| 68 | +test('cleanupStaleAppLogProcesses removes pid files even when pid is stale', () => { |
| 69 | + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-app-log-clean-')); |
| 70 | + const sessionDir = path.join(root, 'default'); |
| 71 | + fs.mkdirSync(sessionDir, { recursive: true }); |
| 72 | + const pidPath = path.join(sessionDir, APP_LOG_PID_FILENAME); |
| 73 | + fs.writeFileSync(pidPath, '999999\n'); |
| 74 | + |
| 75 | + cleanupStaleAppLogProcesses(root); |
| 76 | + |
| 77 | + assert.equal(fs.existsSync(pidPath), false); |
| 78 | +}); |
| 79 | + |
| 80 | +test('buildIosDeviceLogStreamArgs builds expected devicectl command args', () => { |
| 81 | + assert.deepEqual(buildIosDeviceLogStreamArgs('00008150-0000AAAA'), [ |
| 82 | + 'devicectl', |
| 83 | + 'device', |
| 84 | + 'log', |
| 85 | + 'stream', |
| 86 | + '--device', |
| 87 | + '00008150-0000AAAA', |
| 88 | + ]); |
| 89 | +}); |
| 90 | + |
| 91 | +test('cleanupStaleAppLogProcesses removes legacy plain pid files safely', () => { |
| 92 | + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-app-log-clean-legacy-')); |
| 93 | + const sessionDir = path.join(root, 'default'); |
| 94 | + fs.mkdirSync(sessionDir, { recursive: true }); |
| 95 | + const pidPath = path.join(sessionDir, APP_LOG_PID_FILENAME); |
| 96 | + fs.writeFileSync(pidPath, '1\n'); |
| 97 | + |
| 98 | + cleanupStaleAppLogProcesses(root); |
| 99 | + |
| 100 | + assert.equal(fs.existsSync(pidPath), false); |
| 101 | +}); |
| 102 | + |
| 103 | +test('appendAppLogMarker writes marker lines and metadata reflects file', () => { |
| 104 | + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-app-log-mark-')); |
| 105 | + const outPath = path.join(root, 'app.log'); |
| 106 | + appendAppLogMarker(outPath, 'checkpoint'); |
| 107 | + const content = fs.readFileSync(outPath, 'utf8'); |
| 108 | + assert.match(content, /checkpoint/); |
| 109 | + const metadata = getAppLogPathMetadata(outPath); |
| 110 | + assert.equal(metadata.exists, true); |
| 111 | + assert.ok(metadata.sizeBytes > 0); |
| 112 | +}); |
| 113 | + |
| 114 | +test('runAppLogDoctor returns note when app bundle is missing', async () => { |
| 115 | + const result = await runAppLogDoctor({ |
| 116 | + platform: 'android', |
| 117 | + id: 'emulator-5554', |
| 118 | + name: 'Pixel', |
| 119 | + kind: 'emulator', |
| 120 | + }); |
| 121 | + assert.equal(Array.isArray(result.notes), true); |
| 122 | + assert.ok(result.notes.some((note) => note.includes('Run open <app> first'))); |
| 123 | +}); |
0 commit comments