Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/service/launchd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ export function buildPlist(): string {
<dict>
<key>PATH</key>
<string>${escapeXml(pathEnv)}</string>
</dict>
${process.env.CODEX_BIN ? ` <key>CODEX_BIN</key>
<string>${escapeXml(process.env.CODEX_BIN)}</string>
` : ''} </dict>
</dict>
</plist>
`;
Expand Down
2 changes: 2 additions & 0 deletions src/service/systemd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export function buildUnit(): string {
const nodePath = process.execPath;
const cliBinPath = resolveCliBinPath();
const pathEnv = process.env.PATH ?? '';
const codexBinEnv = process.env.CODEX_BIN ? `Environment="CODEX_BIN=${esc(process.env.CODEX_BIN)}"\n` : '';
return `[Unit]
Description=feishu-codex-bridge bot
After=network-online.target
Expand All @@ -55,6 +56,7 @@ RestartSec=5
StandardOutput=append:${serviceStdoutPath()}
StandardError=append:${serviceStderrPath()}
Environment="PATH=${esc(pathEnv)}"
${codexBinEnv}

[Install]
WantedBy=default.target
Expand Down
1 change: 1 addition & 0 deletions src/service/win-startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export function buildLauncherCmd(): string {
return [
'@echo off',
`set "PATH=${process.env.PATH ?? ''}"`,
...(process.env.CODEX_BIN ? [`set "CODEX_BIN=${process.env.CODEX_BIN}"`] : []),
`set "${SERVICE_ENV_FLAG}=1"`,
`"${process.execPath}" "${resolveCliBinPath()}" run >> "${serviceStdoutPath()}" 2>> "${serviceStderrPath()}"`,
'',
Expand Down
34 changes: 34 additions & 0 deletions test/service-definitions.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, it } from 'vitest';
import { buildPlist } from '../src/service/launchd';
import { buildUnit, SYSTEMD_UNIT_NAME } from '../src/service/systemd';
import { buildLauncherCmd, buildLauncherVbs } from '../src/service/win-startup';

Expand Down Expand Up @@ -28,6 +29,22 @@ describe('systemd unit (buildUnit)', () => {
it('unit name is a .service', () => {
expect(SYSTEMD_UNIT_NAME).toMatch(/\.service$/);
});

it('preserves an explicit CODEX_BIN override for the background service', () => {
withCodexBin('/opt/codex/bin/codex', () => {
expect(buildUnit()).toContain('Environment="CODEX_BIN=/opt/codex/bin/codex"');
});
});
});

describe('launchd plist (buildPlist)', () => {
it('preserves an explicit CODEX_BIN override for the background service', () => {
withCodexBin('/Applications/Codex & Friends.app/Contents/Resources/codex', () => {
const plist = buildPlist();
expect(plist).toContain('<key>CODEX_BIN</key>');
expect(plist).toContain('/Applications/Codex &amp; Friends.app/Contents/Resources/codex');
});
});
});

describe('Windows hidden launcher (.cmd)', () => {
Expand All @@ -45,6 +62,12 @@ describe('Windows hidden launcher (.cmd)', () => {
expect(cmd).toContain('>> "');
expect(cmd).toContain('2>> "');
});

it('preserves an explicit CODEX_BIN override for the background service', () => {
withCodexBin('C:\\Tools\\codex.exe', () => {
expect(buildLauncherCmd()).toContain('set "CODEX_BIN=C:\\Tools\\codex.exe"');
});
});
});

describe('Windows hidden launcher (.vbs)', () => {
Expand All @@ -55,3 +78,14 @@ describe('Windows hidden launcher (.vbs)', () => {
expect(vbs).toMatch(/sh\.Run "cmd \/c "".+\.cmd""", 0, False/);
});
});

function withCodexBin(value: string, fn: () => void): void {
const prev = process.env.CODEX_BIN;
process.env.CODEX_BIN = value;
try {
fn();
} finally {
if (prev === undefined) delete process.env.CODEX_BIN;
else process.env.CODEX_BIN = prev;
}
}