|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { afterEach, test, vi } from 'vitest'; |
| 5 | +import { mkdtempForTest } from '../../__tests__/tmp-dir.ts'; |
| 6 | +import type { AppleToolCommandExecutor } from '../../core/tool-provider-types.ts'; |
| 7 | +import type { ExecResult } from '@agent-device/host-kit/command'; |
| 8 | + |
| 9 | +// App discovery stats each candidate root through the host-file seam and asks that seam for the |
| 10 | +// home directory whose `Applications` folder is the third root. Those two are the only effects |
| 11 | +// mocked here: which roots exist is a host fact a Linux coverage runner cannot answer the way |
| 12 | +// macOS does, so the test declares them. Every `find` and every Info.plist read is driven |
| 13 | +// through the executor and plist-reader arguments the provider factory itself takes. |
| 14 | +const hostFileState = vi.hoisted(() => ({ |
| 15 | + home: '', |
| 16 | + directories: new Set<string>(), |
| 17 | + files: new Set<string>(), |
| 18 | +})); |
| 19 | + |
| 20 | +vi.mock('@agent-device/host-kit/host-file', async (importOriginal) => { |
| 21 | + const actual = await importOriginal<typeof import('@agent-device/host-kit/host-file')>(); |
| 22 | + return { |
| 23 | + ...actual, |
| 24 | + hostHomeDirectory: () => hostFileState.home, |
| 25 | + hostFileStat: async (filePath: string) => { |
| 26 | + if (hostFileState.directories.has(filePath)) { |
| 27 | + return { isDirectory: () => true } as fs.Stats; |
| 28 | + } |
| 29 | + if (hostFileState.files.has(filePath)) { |
| 30 | + return { isDirectory: () => false } as fs.Stats; |
| 31 | + } |
| 32 | + const error = new Error( |
| 33 | + `ENOENT: no such file or directory, stat '${filePath}'`, |
| 34 | + ) as NodeJS.ErrnoException; |
| 35 | + error.code = 'ENOENT'; |
| 36 | + throw error; |
| 37 | + }, |
| 38 | + }; |
| 39 | +}); |
| 40 | + |
| 41 | +import { createLocalAppleMacOsHostProvider } from './host-provider.ts'; |
| 42 | + |
| 43 | +type PlistReader = (plistPath: string) => Promise<Record<string, unknown> | null>; |
| 44 | + |
| 45 | +let findCalls: string[] = []; |
| 46 | + |
| 47 | +/** The bundle a candidate Info.plist path belongs to, for fixtures keyed by bundle path. */ |
| 48 | +function bundlePathFromPlist(plistPath: string): string { |
| 49 | + return plistPath.endsWith('Contents/Info.plist') |
| 50 | + ? path.dirname(path.dirname(plistPath)) |
| 51 | + : path.dirname(plistPath); |
| 52 | +} |
| 53 | + |
| 54 | +function bundleName(plistPath: string): string { |
| 55 | + return path.basename(bundlePathFromPlist(plistPath), '.app'); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * A scratch home for one run of `listApps`, so the user root is a path the test controls. |
| 60 | + * Nothing is written under it; the `hostFileStat` mock above answers its existence. |
| 61 | + */ |
| 62 | +async function scratchHome(): Promise<string> { |
| 63 | + const home = await mkdtempForTest('macos-host-home-'); |
| 64 | + hostFileState.home = home; |
| 65 | + return home; |
| 66 | +} |
| 67 | + |
| 68 | +function recordingRunCommand(params: { |
| 69 | + findLinesByRoot?: Record<string, string[]>; |
| 70 | + failingRoots?: readonly string[]; |
| 71 | +}): AppleToolCommandExecutor { |
| 72 | + return async (cmd, args): Promise<ExecResult> => { |
| 73 | + if (cmd === 'find' && args[1] === '-maxdepth') { |
| 74 | + const root = args[0]!; |
| 75 | + findCalls.push(root); |
| 76 | + if (params.failingRoots?.includes(root)) { |
| 77 | + return { stdout: '', stderr: `find: ${root}: Operation not permitted`, exitCode: 1 }; |
| 78 | + } |
| 79 | + return { |
| 80 | + stdout: (params.findLinesByRoot?.[root] ?? []).join('\n'), |
| 81 | + stderr: '', |
| 82 | + exitCode: 0, |
| 83 | + }; |
| 84 | + } |
| 85 | + throw new Error(`The macOS host provider must not run ${cmd} ${args.join(' ')} in a test`); |
| 86 | + }; |
| 87 | +} |
| 88 | + |
| 89 | +async function listApps(params: { |
| 90 | + filter: 'user-installed' | 'all'; |
| 91 | + findLinesByRoot?: Record<string, string[]>; |
| 92 | + failingRoots?: readonly string[]; |
| 93 | + readPlistJson: PlistReader; |
| 94 | +}): Promise<{ bundleId: string; name: string }[]> { |
| 95 | + const provider = createLocalAppleMacOsHostProvider( |
| 96 | + recordingRunCommand({ |
| 97 | + findLinesByRoot: params.findLinesByRoot, |
| 98 | + failingRoots: params.failingRoots, |
| 99 | + }), |
| 100 | + params.readPlistJson, |
| 101 | + ); |
| 102 | + return await provider.listApps(params.filter); |
| 103 | +} |
| 104 | + |
| 105 | +afterEach(() => { |
| 106 | + findCalls = []; |
| 107 | + hostFileState.home = ''; |
| 108 | + hostFileState.directories.clear(); |
| 109 | + hostFileState.files.clear(); |
| 110 | +}); |
| 111 | + |
| 112 | +test('listApps walks every present root, normalizes find output, and sorts resolved apps by name', async () => { |
| 113 | + const home = await scratchHome(); |
| 114 | + const userApplications = path.join(home, 'Applications'); |
| 115 | + hostFileState.directories.add('/Applications'); |
| 116 | + hostFileState.directories.add('/System/Applications'); |
| 117 | + hostFileState.directories.add(userApplications); |
| 118 | + |
| 119 | + const found = await listApps({ |
| 120 | + filter: 'all', |
| 121 | + findLinesByRoot: { |
| 122 | + '/Applications': [ |
| 123 | + '/Applications/Zeta.app', |
| 124 | + '/Applications/Alpha.app ', |
| 125 | + '', |
| 126 | + ' ', |
| 127 | + '/Applications/Beta.app', |
| 128 | + ], |
| 129 | + '/System/Applications': ['/System/Applications/Calc.app'], |
| 130 | + [userApplications]: [path.join(userApplications, 'Home.app')], |
| 131 | + }, |
| 132 | + readPlistJson: async (plistPath) => { |
| 133 | + // Beta's first candidate misses, so its identity comes from the bundle-root plist. |
| 134 | + if (plistPath === '/Applications/Beta.app/Contents/Info.plist') { |
| 135 | + return null; |
| 136 | + } |
| 137 | + if (plistPath === '/Applications/Beta.app/Info.plist') { |
| 138 | + return { CFBundleIdentifier: ' com.beta.app ', CFBundleName: 'Beta Name' }; |
| 139 | + } |
| 140 | + return { CFBundleIdentifier: `com.example.${bundleName(plistPath).toLowerCase()}` }; |
| 141 | + }, |
| 142 | + }); |
| 143 | + |
| 144 | + // The blank and padded find lines resolve to real bundles only after trimming; Beta keeps the |
| 145 | + // padded identifier trimmed and the CFBundleName its bundle-root plist reports; the user |
| 146 | + // root's bundle is present; and the list sorts by resolved name, not by root order. |
| 147 | + assert.deepEqual(found, [ |
| 148 | + { bundleId: 'com.example.alpha', name: 'Alpha' }, |
| 149 | + { bundleId: 'com.beta.app', name: 'Beta Name' }, |
| 150 | + { bundleId: 'com.example.calc', name: 'Calc' }, |
| 151 | + { bundleId: 'com.example.home', name: 'Home' }, |
| 152 | + { bundleId: 'com.example.zeta', name: 'Zeta' }, |
| 153 | + ]); |
| 154 | + assert.deepEqual(findCalls, ['/Applications', '/System/Applications', userApplications]); |
| 155 | +}); |
| 156 | + |
| 157 | +test('listApps probes each root independently: a non-directory root is never searched and a failing find contributes nothing', async () => { |
| 158 | + const home = await scratchHome(); |
| 159 | + const userApplications = path.join(home, 'Applications'); |
| 160 | + hostFileState.files.add('/Applications'); |
| 161 | + hostFileState.directories.add('/System/Applications'); |
| 162 | + hostFileState.directories.add(userApplications); |
| 163 | + |
| 164 | + const found = await listApps({ |
| 165 | + filter: 'all', |
| 166 | + failingRoots: [userApplications], |
| 167 | + findLinesByRoot: { |
| 168 | + '/System/Applications': ['/System/Applications/Calc.app', '/System/Applications/Notes.app'], |
| 169 | + }, |
| 170 | + readPlistJson: async (plistPath) => ({ |
| 171 | + CFBundleIdentifier: `com.apple.${bundleName(plistPath).toLowerCase()}`, |
| 172 | + CFBundleName: bundleName(plistPath), |
| 173 | + }), |
| 174 | + }); |
| 175 | + |
| 176 | + // `/Applications` is a plain file, so `find` never runs for it; the user root's find exits |
| 177 | + // non-zero, so its (hypothetical) hits are dropped; the one healthy root still resolves. |
| 178 | + assert.deepEqual(found, [ |
| 179 | + { bundleId: 'com.apple.calc', name: 'Calc' }, |
| 180 | + { bundleId: 'com.apple.notes', name: 'Notes' }, |
| 181 | + ]); |
| 182 | + assert.deepEqual(findCalls, ['/System/Applications', userApplications]); |
| 183 | +}); |
| 184 | + |
| 185 | +test('listApps finds nothing when no candidate root exists and reads no plist for it', async () => { |
| 186 | + await scratchHome(); |
| 187 | + |
| 188 | + const found = await listApps({ |
| 189 | + filter: 'all', |
| 190 | + findLinesByRoot: { '/Applications': ['/Applications/Any.app'] }, |
| 191 | + readPlistJson: async () => { |
| 192 | + throw new Error('no bundle may exist without a root'); |
| 193 | + }, |
| 194 | + }); |
| 195 | + |
| 196 | + assert.deepEqual(found, []); |
| 197 | + assert.deepEqual(findCalls, []); |
| 198 | +}); |
| 199 | + |
| 200 | +test('listApps keeps only bundles whose plist carries an identifier and fails open on a plist read that throws', async () => { |
| 201 | + await scratchHome(); |
| 202 | + hostFileState.directories.add('/Applications'); |
| 203 | + const read: string[] = []; |
| 204 | + |
| 205 | + const found = await listApps({ |
| 206 | + filter: 'all', |
| 207 | + findLinesByRoot: { |
| 208 | + '/Applications': [ |
| 209 | + '/Applications/Anonymous.app', |
| 210 | + '/Applications/BlankId.app', |
| 211 | + '/Applications/Broken.app', |
| 212 | + '/Applications/NameOnly.app', |
| 213 | + '/Applications/Named.app', |
| 214 | + '/Applications/ObjectId.app', |
| 215 | + ], |
| 216 | + }, |
| 217 | + readPlistJson: async (plistPath) => { |
| 218 | + read.push(plistPath); |
| 219 | + if (plistPath.includes('/Broken.app/')) { |
| 220 | + throw new Error('plutil died'); |
| 221 | + } |
| 222 | + if (plistPath.includes('/Anonymous.app/')) { |
| 223 | + return null; |
| 224 | + } |
| 225 | + if (plistPath.includes('/NameOnly.app/')) { |
| 226 | + return { CFBundleDisplayName: 'A name without any identifier' }; |
| 227 | + } |
| 228 | + if (plistPath.includes('/BlankId.app/')) { |
| 229 | + return { CFBundleIdentifier: ' ', CFBundleName: 'BlankId' }; |
| 230 | + } |
| 231 | + if (plistPath.includes('/ObjectId.app/')) { |
| 232 | + return { CFBundleIdentifier: { not: 'a string' } }; |
| 233 | + } |
| 234 | + return { CFBundleIdentifier: 'com.named.app' }; |
| 235 | + }, |
| 236 | + }); |
| 237 | + |
| 238 | + // Broken.app's throw fails open to no identity; a display name, a blank identifier, and an |
| 239 | + // object-valued identifier are all identifiers absent; only a real string identifier survives, |
| 240 | + // named from the bundle basename because its plist carries no name. |
| 241 | + assert.deepEqual(found, [{ bundleId: 'com.named.app', name: 'Named' }]); |
| 242 | + // Both candidate locations are consulted before a bundle is given up on. |
| 243 | + assert.ok(read.includes('/Applications/Anonymous.app/Contents/Info.plist')); |
| 244 | + assert.ok(read.includes('/Applications/Anonymous.app/Info.plist')); |
| 245 | +}); |
| 246 | + |
| 247 | +test('listApps hides Apple bundles for the user-installed filter and shows them for all', async () => { |
| 248 | + await scratchHome(); |
| 249 | + hostFileState.directories.add('/Applications'); |
| 250 | + const findLinesByRoot = { |
| 251 | + '/Applications': ['/Applications/MyTool.app', '/Applications/System Preferences.app'], |
| 252 | + }; |
| 253 | + const readPlistJson: PlistReader = async (plistPath) => |
| 254 | + plistPath.includes('/System Preferences.app/') |
| 255 | + ? { |
| 256 | + CFBundleIdentifier: 'com.apple.systempreferences', |
| 257 | + CFBundleDisplayName: 'System Settings', |
| 258 | + } |
| 259 | + : { CFBundleIdentifier: 'com.mytool.app', CFBundleName: 'MyTool' }; |
| 260 | + |
| 261 | + const userInstalled = await listApps({ |
| 262 | + filter: 'user-installed', |
| 263 | + findLinesByRoot, |
| 264 | + readPlistJson, |
| 265 | + }); |
| 266 | + assert.deepEqual(userInstalled, [{ bundleId: 'com.mytool.app', name: 'MyTool' }]); |
| 267 | + |
| 268 | + const all = await listApps({ filter: 'all', findLinesByRoot, readPlistJson }); |
| 269 | + assert.deepEqual(all, [ |
| 270 | + { bundleId: 'com.mytool.app', name: 'MyTool' }, |
| 271 | + { bundleId: 'com.apple.systempreferences', name: 'System Settings' }, |
| 272 | + ]); |
| 273 | +}); |
0 commit comments