|
| 1 | +import { afterAll, beforeAll, describe, expect, test } from 'bun:test'; |
| 2 | +import { spawnSync } from 'node:child_process'; |
| 3 | +import { existsSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs'; |
| 4 | +import { tmpdir } from 'node:os'; |
| 5 | +import { join } from 'node:path'; |
| 6 | +import { buildMacApp } from '../../../src/cli/build-macos'; |
| 7 | +import { currentPlatform } from '../../../src/common/platform'; |
| 8 | +import { SAMBAR_VERSION } from '../../../src/common/version'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Integration test for the macOS `.app` bundler. It writes a trivial entry |
| 12 | + * (not a real Sambar app — the bundler only packages it), compiles it with |
| 13 | + * `bun build --compile`, lays out the `.app`, then asserts the on-disk |
| 14 | + * structure. The produced binary is exec'd to confirm it actually runs. |
| 15 | + */ |
| 16 | +if (currentPlatform() === 'macos') { |
| 17 | + describe('buildMacApp (integration)', () => { |
| 18 | + let workDir: string; |
| 19 | + let entry: string; |
| 20 | + let outDir: string; |
| 21 | + const name = 'Hi App'; |
| 22 | + |
| 23 | + beforeAll(() => { |
| 24 | + workDir = mkdtempSync(join(tmpdir(), 'sambar-cli-build-')); |
| 25 | + entry = join(workDir, 'entry.ts'); |
| 26 | + outDir = join(workDir, 'out'); |
| 27 | + writeFileSync(entry, "console.log('hi');\nprocess.exit(0);\n"); |
| 28 | + }); |
| 29 | + |
| 30 | + afterAll(() => { |
| 31 | + rmSync(workDir, { recursive: true, force: true }); |
| 32 | + }); |
| 33 | + |
| 34 | + test('produces a structurally valid .app from a trivial entry', async () => { |
| 35 | + const appPath = await buildMacApp({ |
| 36 | + entry, |
| 37 | + name, |
| 38 | + id: 'com.example.hi', |
| 39 | + out: outDir, |
| 40 | + }); |
| 41 | + |
| 42 | + expect(appPath).toBe(join(outDir, `${name}.app`)); |
| 43 | + expect(existsSync(appPath)).toBe(true); |
| 44 | + |
| 45 | + const infoPlist = join(appPath, 'Contents', 'Info.plist'); |
| 46 | + expect(existsSync(infoPlist)).toBe(true); |
| 47 | + const plistText = readFileSync(infoPlist, 'utf8'); |
| 48 | + expect(plistText).toContain('<key>CFBundleIdentifier</key>'); |
| 49 | + expect(plistText).toContain('com.example.hi'); |
| 50 | + expect(plistText).toContain(name); |
| 51 | + expect(plistText).toContain(SAMBAR_VERSION); |
| 52 | + |
| 53 | + const exe = join(appPath, 'Contents', 'MacOS', name); |
| 54 | + expect(existsSync(exe)).toBe(true); |
| 55 | + const mode = statSync(exe).mode; |
| 56 | + // Executable bit set for owner/group/other. |
| 57 | + expect(mode & 0o111).not.toBe(0); |
| 58 | + |
| 59 | + // The compiled binary should actually run and print 'hi'. |
| 60 | + const result = spawnSync(exe, [], { encoding: 'utf8' }); |
| 61 | + expect(result.status).toBe(0); |
| 62 | + expect(result.stdout).toContain('hi'); |
| 63 | + }, 30000); |
| 64 | + |
| 65 | + test('defaults the bundle id from the name when --id is omitted', async () => { |
| 66 | + const appPath = await buildMacApp({ |
| 67 | + entry, |
| 68 | + name: 'Defaulted', |
| 69 | + out: join(workDir, 'out2'), |
| 70 | + }); |
| 71 | + const plistText = readFileSync(join(appPath, 'Contents', 'Info.plist'), 'utf8'); |
| 72 | + expect(plistText).toContain('com.sambar.defaulted'); |
| 73 | + }, 30000); |
| 74 | + }); |
| 75 | +} |
0 commit comments