import assert from 'node:assert/strict' import { execFileSync } from 'node:child_process' import { existsSync, mkdirSync, mkdtempSync, readFileSync, readlinkSync, rmSync, symlinkSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import path from 'node:path' import test from 'node:test' import { prepareBrandedElectron } from './patch-dev-electron-branding.mjs' const BUDDY = '/usr/libexec/PlistBuddy' function plistValue(plist, key) { return execFileSync(BUDDY, ['-c', `Print :${key}`, plist], { encoding: 'utf8' }).trim() } test('dev Electron launches from a versioned DeepWork.app without changing the stock bundle', { skip: process.platform !== 'darwin' }, () => { const root = mkdtempSync(path.join(tmpdir(), 'hermes-dock-brand-')) try { const electronDist = path.join(root, 'dist') const cacheRoot = path.join(root, 'cache') const source = path.join(electronDist, 'Electron.app/Contents') const sourcePlist = path.join(source, 'Info.plist') mkdirSync(path.join(source, 'MacOS'), { recursive: true }) mkdirSync(path.join(source, 'Frameworks/Test.framework/Versions/A/Resources'), { recursive: true }) writeFileSync(path.join(source, 'Frameworks/Test.framework/Versions/A/Resources/icudtl.dat'), 'icu') symlinkSync('A', path.join(source, 'Frameworks/Test.framework/Versions/Current')) writeFileSync(path.join(source, 'MacOS/Electron'), 'binary') writeFileSync(sourcePlist, ` CFBundleNameElectron CFBundleDisplayNameElectron CFBundleExecutableElectron CFBundleIdentifiercom.github.Electron CFBundleShortVersionString1.0 `) const options = { electronDist, cacheRoot } const binary = prepareBrandedElectron(options) const brandedPlist = path.join(cacheRoot, '1.0/DeepWork.app/Contents/Info.plist') assert.equal(binary, path.join(cacheRoot, '1.0/DeepWork.app/Contents/MacOS/Electron')) assert.equal(plistValue(brandedPlist, 'CFBundleDisplayName'), 'DeepWork') assert.equal(plistValue(brandedPlist, 'CFBundleExecutable'), 'Electron') assert.equal(plistValue(brandedPlist, 'CFBundleIdentifier'), 'com.nousresearch.hermes.dev') assert.equal(plistValue(sourcePlist, 'CFBundleName'), 'Electron') const copiedFramework = path.join(cacheRoot, '1.0/DeepWork.app/Contents/Frameworks/Test.framework/Versions') assert.equal(readlinkSync(path.join(copiedFramework, 'Current')), 'A') assert.equal(readFileSync(path.join(copiedFramework, 'Current/Resources/icudtl.dat'), 'utf8'), 'icu') const sentinel = path.join(cacheRoot, '1.0/DeepWork.app/sentinel') writeFileSync(sentinel, 'keep') assert.equal(prepareBrandedElectron(options), binary) assert.equal(readFileSync(sentinel, 'utf8'), 'keep') writeFileSync(path.join(cacheRoot, '1.0/DeepWork.app/.hermes-electron-version'), 'stale') assert.equal(prepareBrandedElectron(options), binary) assert.equal(existsSync(sentinel), false) assert.equal(readFileSync(path.join(copiedFramework, 'Current/Resources/icudtl.dat'), 'utf8'), 'icu') assert.equal(readFileSync(path.join(cacheRoot, '1.0/DeepWork.app/.hermes-electron-version'), 'utf8').startsWith('1.0:'), true) writeFileSync(sentinel, 'keep') execFileSync(BUDDY, ['-c', 'Set :CFBundleShortVersionString 2.0', sourcePlist]) const updatedBinary = prepareBrandedElectron(options) assert.equal(updatedBinary, path.join(cacheRoot, '2.0/DeepWork.app/Contents/MacOS/Electron')) assert.equal(readFileSync(sentinel, 'utf8'), 'keep') } finally { rmSync(root, { recursive: true, force: true }) } })