import { describe, it, expect, beforeEach, afterEach } from 'vitest' import { spawnSync } from 'child_process' import { mkdirSync, writeFileSync, rmSync, chmodSync, existsSync, readFileSync, statSync } from 'fs' import { join } from 'path' import { tmpdir } from 'os' import { repoRoot } from '../helpers/repo-root' import { extractShellFunction as extractEntrypointFunction } from '../helpers/shell-function' const entrypointPath = join(repoRoot, 'scripts/docker-entrypoint.sh') let stubDir: string let logPath: string const writeStub = (name: string, body: string) => { const file = join(stubDir, name) writeFileSync(file, `#!/bin/bash\n${body}\n`) chmodSync(file, 0o755) } const extractShellFunction = (name: string) => extractEntrypointFunction(entrypointPath, name) const extractSupportedFloor = () => { const match = readFileSync(entrypointPath, 'utf-8').match(/^OPENCODE_SUPPORTED_FLOOR="[^"]+"$/m) if (!match) throw new Error('OPENCODE_SUPPORTED_FLOOR not found in docker-entrypoint.sh') return match[0] } const releaseHelperPath = join(repoRoot, 'scripts/lib/opencode-release.sh') const installPrelude = () => [ extractSupportedFloor(), readFileSync(releaseHelperPath, 'utf-8'), extractShellFunction('read_opencode_version'), extractShellFunction('install_opencode'), extractShellFunction('reconcile_persisted_opencode'), ].join('\n') beforeEach(() => { stubDir = join(tmpdir(), `ocm-entrypoint-${Date.now()}-${Math.random().toString(36).slice(2)}`) mkdirSync(stubDir, { recursive: true }) logPath = join(stubDir, 'calls.log') writeStub('stat', `echo "${'${OCM_STUB_DEV_GID:-44}'}"`) writeStub('getent', ` if [ "$1" = "group" ] && [ "$2" = "${'${OCM_STUB_DEV_GID:-44}'}" ]; then echo "${'${OCM_STUB_GROUP_HOLDER}'}:x:$2:" exit 0 fi exit 2`) writeStub('groupadd', `echo "groupadd $*" >> "$OCM_STUB_LOG"`) writeStub('usermod', `echo "usermod $*" >> "$OCM_STUB_LOG"`) writeStub('runuser', `echo "runuser $*" >> "$OCM_STUB_LOG" if [ "${'${OCM_STUB_RUNUSER_EXEC:-0}'}" = "1" ]; then while [ "$#" -gt 0 ] && [ "$1" != "--" ]; do shift; done [ "$#" -gt 0 ] && shift OCM_EXECUTED_AS_NODE=1 "$@" exit $? fi exit ${'${OCM_STUB_RUNUSER_EXIT:-0}'}`) }) afterEach(() => { rmSync(stubDir, { recursive: true, force: true }) }) const runScript = (snippet: string, env: Record = {}) => { const scriptPath = join(stubDir, 'test.sh') writeFileSync(scriptPath, `set -e\n${extractShellFunction('grant_kvm_access')}\n${snippet}\n`) return spawnSync('bash', [scriptPath], { encoding: 'utf-8', env: { ...process.env, PATH: `${stubDir}:${process.env.PATH}`, OCM_STUB_LOG: logPath, ...env, }, }) } const stubCalls = () => { if (!existsSync(logPath)) return [] return readFileSync(logPath, 'utf-8').split('\n').filter(Boolean) } const mockDevice = () => { const dev = join(stubDir, 'dev-kvm') writeFileSync(dev, '') return dev } describe('grant_kvm_access', () => { it('is a no-op when the device does not exist', () => { const res = runScript(`grant_kvm_access ${JSON.stringify(join(stubDir, 'missing-device'))}; echo ok`) expect(res.status).toBe(0) expect(res.stdout).toContain('ok') expect(stubCalls()).toEqual([]) }) it('is a no-op when the device gid is not numeric', () => { const res = runScript(`grant_kvm_access ${JSON.stringify(mockDevice())}; echo ok`, { OCM_STUB_DEV_GID: 'abc', }) expect(res.status).toBe(0) expect(res.stdout).toContain('ok') expect(stubCalls()).toEqual([]) }) it('reuses the existing group holding the device gid', () => { const dev = mockDevice() const res = runScript(`grant_kvm_access ${JSON.stringify(dev)}; echo ok`, { OCM_STUB_DEV_GID: '44', OCM_STUB_GROUP_HOLDER: 'video', }) expect(res.status).toBe(0) expect(res.stdout).toContain('Granted node access') expect(stubCalls().some((c) => c === 'usermod -aG video node')).toBe(true) expect(stubCalls().some((c) => c.startsWith('runuser -u node -- test -r'))).toBe(true) expect(stubCalls().some((c) => c.startsWith('runuser -u node -- test -w'))).toBe(true) }) it('creates a matching group when none holds the device gid', () => { const dev = mockDevice() const res = runScript(`grant_kvm_access ${JSON.stringify(dev)}; echo ok`, { OCM_STUB_DEV_GID: '232', OCM_STUB_GROUP_HOLDER: '', }) expect(res.status).toBe(0) expect(res.stdout).toContain('Granted node access') expect(stubCalls().some((c) => c === 'groupadd -g 232 kvm')).toBe(true) expect(stubCalls().some((c) => c.startsWith('usermod -aG kvm node'))).toBe(true) }) it('fails clearly when the group cannot be created', () => { writeStub('groupadd', `echo "groupadd $*" >> "$OCM_STUB_LOG"\nexit 1`) const dev = mockDevice() const res = runScript(`grant_kvm_access ${JSON.stringify(dev)} || echo "failed"`, { OCM_STUB_DEV_GID: '232', OCM_STUB_GROUP_HOLDER: '', }) expect(res.status).toBe(0) expect(res.stdout).toContain('failed') expect(res.stderr).toMatch(/could not create group/) }) it('fails clearly when node cannot be added to the group', () => { writeStub('usermod', `echo "usermod $*" >> "$OCM_STUB_LOG"\nexit 1`) const dev = mockDevice() const res = runScript(`grant_kvm_access ${JSON.stringify(dev)} || echo "failed"`, { OCM_STUB_GROUP_HOLDER: 'video', }) expect(res.status).toBe(0) expect(res.stdout).toContain('failed') expect(res.stderr).toMatch(/could not add node to group/) }) it('fails clearly when the node user cannot open the device', () => { const dev = mockDevice() const res = runScript(`grant_kvm_access ${JSON.stringify(dev)} || echo "failed"`, { OCM_STUB_GROUP_HOLDER: 'video', OCM_STUB_RUNUSER_EXIT: '1', }) expect(res.status).toBe(0) expect(res.stdout).toContain('failed') expect(res.stderr).toMatch(/node cannot access/) }) }) describe('ensure_auth_secret', () => { const runEnsureAuthSecret = (secretFile: string, env: Record = {}) => { const scriptPath = join(stubDir, 'test.sh') writeFileSync( scriptPath, `set -e\n${extractShellFunction('ensure_auth_secret')}\nensure_auth_secret ${JSON.stringify(secretFile)}\nprintf 'SECRET=%s\\n' "$AUTH_SECRET"\n`, ) const baseEnv = { ...process.env } delete baseEnv.AUTH_SECRET return spawnSync('bash', [scriptPath], { encoding: 'utf-8', env: { ...baseEnv, ...env } }) } const exportedSecret = (stdout: string) => stdout.match(/^SECRET=(.*)$/m)?.[1] ?? '' it('generates, persists, and exports a private secret when AUTH_SECRET is unset', () => { const secretFile = join(stubDir, 'data', '.auth-secret') const res = runEnsureAuthSecret(secretFile) expect(res.status).toBe(0) expect(res.stdout).toContain(`Generated AUTH_SECRET and saved it to ${secretFile}`) const secret = exportedSecret(res.stdout) expect(Buffer.from(secret, 'base64')).toHaveLength(32) expect(readFileSync(secretFile, 'utf-8').trim()).toBe(secret) expect(statSync(secretFile).mode & 0o777).toBe(0o600) }) it('reuses the persisted secret on later starts', () => { const secretFile = join(stubDir, 'data', '.auth-secret') const first = exportedSecret(runEnsureAuthSecret(secretFile).stdout) const second = runEnsureAuthSecret(secretFile) expect(second.status).toBe(0) expect(second.stdout).not.toContain('Generated AUTH_SECRET') expect(exportedSecret(second.stdout)).toBe(first) }) it('keeps an explicit AUTH_SECRET and writes nothing', () => { const secretFile = join(stubDir, 'data', '.auth-secret') const res = runEnsureAuthSecret(secretFile, { AUTH_SECRET: 'explicit-secret' }) expect(res.status).toBe(0) expect(exportedSecret(res.stdout)).toBe('explicit-secret') expect(existsSync(secretFile)).toBe(false) }) it('fails when the secret file cannot be written', () => { const blocker = join(stubDir, 'not-a-dir') writeFileSync(blocker, '') const res = runEnsureAuthSecret(join(blocker, '.auth-secret')) expect(res.status).not.toBe(0) expect(res.stdout).not.toContain('SECRET=') }) }) const extractOpenCodeInstallSection = () => { const entrypoint = readFileSync(entrypointPath, 'utf-8') const startMarker = 'echo "Checking OpenCode installation..."' const endMarker = 'echo "Starting OpenCode Manager Backend..."' const start = entrypoint.indexOf(startMarker) const end = entrypoint.indexOf(endMarker) if (start === -1 || end === -1 || end <= start) { throw new Error('OpenCode install section not found in docker-entrypoint.sh') } return entrypoint.slice(start, end) } const runOpenCodeSection = (snippet: string, env: Record = {}) => { const scriptPath = join(stubDir, 'test.sh') const homeDir = join(stubDir, 'home') writeFileSync(scriptPath, `set -e\n${snippet}\n`) return spawnSync('bash', [scriptPath], { encoding: 'utf-8', env: { ...process.env, PATH: `${stubDir}:${homeDir}/.opencode/bin:/usr/bin:/bin`, OCM_STUB_LOG: logPath, OCM_STUB_RUNUSER_EXEC: '1', HOME: homeDir, OPENCODE_BUNDLED_VERSION: '2.0.15', OPENCODE_MIN_VERSION: '2.0.15', ...env, }, }) } const stubInstallTools = () => { writeStub('curl', `echo "curl $*" >> "$OCM_STUB_LOG" out="" while [ "$#" -gt 0 ]; do if [ "$1" = "-o" ]; then out="$2"; fi shift done mkdir -p "$HOME/.opencode/bin" printf '#!/bin/bash\\necho %s\\n' "$OPENCODE_BUNDLED_VERSION" > "$HOME/.opencode/bin/opencode" chmod +x "$HOME/.opencode/bin/opencode" printf 'fake binary\\n' > "$(dirname "$out")/opencode"`) writeStub('tar', `echo "tar $*" >> "$OCM_STUB_LOG"`) } const curlLog = () => stubCalls().filter((c) => c.startsWith('curl ')) describe('install_opencode', () => { it('installs the bundled verified version, never latest', () => { stubInstallTools() const res = runOpenCodeSection(`${installPrelude()}\ninstall_opencode`) expect(res.status).toBe(0) const urls = curlLog().join(' ') expect(urls).toMatch(/https:\/\/opencode\.ai\/files\/bin\/2\.0\.15\/opencode-linux-(x64|arm64)\.tar\.gz/) expect(urls).not.toContain('/releases/latest/download/') expect(urls).not.toContain('/tmp/opencode.tar.gz') }) it('refuses to guess the pinned build when OPENCODE_BUNDLED_VERSION is unset', () => { stubInstallTools() const res = runOpenCodeSection(`${installPrelude()}\ninstall_opencode`, { OPENCODE_BUNDLED_VERSION: '', }) expect(res.status).not.toBe(0) expect(res.stderr).toContain('OPENCODE_BUNDLED_VERSION is not set') expect(curlLog()).toHaveLength(0) }) it('refuses to download a malformed OPENCODE_BUNDLED_VERSION', () => { stubInstallTools() const res = runOpenCodeSection(`${installPrelude()}\ninstall_opencode`, { OPENCODE_BUNDLED_VERSION: '1.18', }) expect(res.status).not.toBe(0) expect(res.stderr).toContain('is not an X.Y.Z version') expect(curlLog()).toHaveLength(0) }) it('honors an OPENCODE_BUNDLED_VERSION override for the download URL', () => { stubInstallTools() const res = runOpenCodeSection(`${installPrelude()}\ninstall_opencode`, { OPENCODE_BUNDLED_VERSION: '2.1.0', }) expect(res.status).toBe(0) const urls = curlLog().join(' ') expect(urls).toMatch(/https:\/\/opencode\.ai\/files\/bin\/2\.1\.0\/opencode-linux-(x64|arm64)\.tar\.gz/) expect(urls).not.toContain('/releases/latest/download/') }) it('reinstalls the bundled verified version when opencode is missing', () => { stubInstallTools() const res = runOpenCodeSection(`${installPrelude()}\n${extractOpenCodeInstallSection()}`) expect(res.status).toBe(0) expect(res.stdout).toContain('OpenCode not found. Installing...') const urls = curlLog().join(' ') expect(urls).toMatch(/https:\/\/opencode\.ai\/files\/bin\/2\.0\.15\/opencode-linux-(x64|arm64)\.tar\.gz/) expect(urls).not.toContain('/releases/latest/download/') }) it('repairs a below-minimum opencode with the bundled verified version, not latest', () => { stubInstallTools() writeStub('opencode', `echo "opencode version 1.0.0"`) const res = runOpenCodeSection(`${installPrelude()}\n${extractOpenCodeInstallSection()}`) expect(res.status).toBe(0) expect(res.stdout).toContain('OpenCode version 1.0.0 is outside the supported range >=2.0.15 <3.0.0') const urls = curlLog().join(' ') expect(urls).toMatch(/https:\/\/opencode\.ai\/files\/bin\/2\.0\.15\/opencode-linux-(x64|arm64)\.tar\.gz/) expect(urls).not.toContain('/releases/latest/download/') }) }) const writeBinary = (dir: string, version: string) => { const binPath = join(dir, 'opencode') writeFileSync(binPath, `#!/bin/bash\necho "opencode version ${version}"\n`) chmodSync(binPath, 0o755) return binPath } const homeBinPath = () => join(stubDir, 'home/.opencode/bin/opencode') const bundledBinPath = () => join(stubDir, 'bundled/opencode') const bundledFirstPath = () => `${join(stubDir, 'home/.opencode/bin')}:${join(stubDir, 'bundled')}:${stubDir}:/usr/bin:/bin` describe('persisted opencode reconciliation', () => { it('probes the persisted binary version through runuser, never directly', () => { mkdirSync(join(stubDir, 'home/.opencode/bin'), { recursive: true }) writeFileSync(homeBinPath(), `#!/bin/bash if [ -z "$OCM_EXECUTED_AS_NODE" ]; then echo "persisted binary executed outside runuser" >&2 exit 1 fi echo "opencode version 2.0.15"`) chmodSync(homeBinPath(), 0o755) const res = runOpenCodeSection(`${installPrelude()}\n${extractOpenCodeInstallSection()}`) expect(res.status).toBe(0) expect(stubCalls()).toContain(`runuser -u node -- ${homeBinPath()} --version`) expect(res.stdout).toContain('retaining it') expect(existsSync(homeBinPath())).toBe(true) }) it('retains a persisted home binary equal to the bundled version', () => { mkdirSync(join(stubDir, 'home/.opencode/bin'), { recursive: true }) writeBinary(join(stubDir, 'home/.opencode/bin'), '2.0.15') const res = runOpenCodeSection(`${installPrelude()}\n${extractOpenCodeInstallSection()}`) expect(res.status).toBe(0) expect(res.stdout).toContain('retaining it') expect(existsSync(homeBinPath())).toBe(true) expect(res.stdout).toContain('OpenCode is installed (version: 2.0.15)') expect(curlLog()).toHaveLength(0) }) it('retains a persisted home binary newer than the bundled version', () => { mkdirSync(join(stubDir, 'home/.opencode/bin'), { recursive: true }) writeBinary(join(stubDir, 'home/.opencode/bin'), '2.1.0') const res = runOpenCodeSection(`${installPrelude()}\n${extractOpenCodeInstallSection()}`) expect(res.status).toBe(0) expect(res.stdout).toContain('retaining it') expect(existsSync(homeBinPath())).toBe(true) expect(res.stdout).toContain('OpenCode is installed (version: 2.1.0)') expect(curlLog()).toHaveLength(0) }) it.each(['2.0.0', '2.0.14', '3.0.0', '3.2.1', '1.0.0'])( 'replaces a persisted home binary %s outside the supported range with the bundled version', (version) => { stubInstallTools() mkdirSync(join(stubDir, 'home/.opencode/bin'), { recursive: true }) writeBinary(join(stubDir, 'home/.opencode/bin'), version) const res = runOpenCodeSection(`${installPrelude()}\n${extractOpenCodeInstallSection()}`) expect(res.status).toBe(0) expect(res.stdout).not.toContain('retaining it') expect(res.stdout).toContain(`Persisted OpenCode ${version} is outside the supported range >=2.0.15 <3.0.0`) expect(res.stdout).toContain('Installing OpenCode 2.0.15...') expect(existsSync(homeBinPath())).toBe(true) const urls = curlLog().join(' ') expect(urls).toMatch(/https:\/\/opencode\.ai\/files\/bin\/2\.0\.15\/opencode-linux-(x64|arm64)\.tar\.gz/) }, ) it.each(['2.0.15-beta.1', '2.0.15+build.5', '2.0.15-rc.1+build.5'])( 'replaces a persisted home binary %s carrying a prerelease or build suffix with the bundled version', (version) => { stubInstallTools() mkdirSync(join(stubDir, 'home/.opencode/bin'), { recursive: true }) writeBinary(join(stubDir, 'home/.opencode/bin'), version) const res = runOpenCodeSection(`${installPrelude()}\n${extractOpenCodeInstallSection()}`) expect(res.status).toBe(0) expect(res.stdout).not.toContain('retaining it') expect(res.stdout).toContain(`Persisted OpenCode ${version} is outside the supported range >=2.0.15 <3.0.0`) expect(res.stdout).toContain('Installing OpenCode 2.0.15...') expect(existsSync(homeBinPath())).toBe(true) const urls = curlLog().join(' ') expect(urls).toMatch(/https:\/\/opencode\.ai\/files\/bin\/2\.0\.15\/opencode-linux-(x64|arm64)\.tar\.gz/) }, ) it.each(['2.0.15-', '2.0.15+', '2.0.15.', '2.0.15.1', '2.0.15_1', '2.0.15-beta_1', '2.0.15+build_1'])( 'replaces a persisted home binary %s whose appended suffix is malformed with the bundled version', (version) => { stubInstallTools() mkdirSync(join(stubDir, 'home/.opencode/bin'), { recursive: true }) writeBinary(join(stubDir, 'home/.opencode/bin'), version) const res = runOpenCodeSection(`${installPrelude()}\n${extractOpenCodeInstallSection()}`) expect(res.status).toBe(0) expect(res.stdout).not.toContain('retaining it') expect(res.stdout).not.toContain('within the supported range') expect(res.stdout).toContain(`Persisted OpenCode ${version} is outside the supported range >=2.0.15 <3.0.0`) expect(res.stdout).toContain('Installing OpenCode 2.0.15...') expect(existsSync(homeBinPath())).toBe(true) const urls = curlLog().join(' ') expect(urls).toMatch(/https:\/\/opencode\.ai\/files\/bin\/2\.0\.15\/opencode-linux-(x64|arm64)\.tar\.gz/) }, ) it('treats a persisted home binary whose --version probe fails as unversioned and installs the bundled version', () => { stubInstallTools() mkdirSync(join(stubDir, 'home/.opencode/bin'), { recursive: true }) writeFileSync(homeBinPath(), `#!/bin/bash echo "opencode version 2.0.15" exit 1`) chmodSync(homeBinPath(), 0o755) const res = runOpenCodeSection(`${installPrelude()}\n${extractOpenCodeInstallSection()}`) expect(res.status).toBe(0) expect(res.stdout).not.toContain('retaining it') expect(res.stdout).toContain('malformed or unversioned') expect(res.stdout).toContain('Installing OpenCode 2.0.15...') expect(existsSync(homeBinPath())).toBe(true) const urls = curlLog().join(' ') expect(urls).toMatch(/https:\/\/opencode\.ai\/files\/bin\/2\.0\.15\/opencode-linux-(x64|arm64)\.tar\.gz/) }) it('falls back to the bundled binary on PATH after removing a persisted 3.x binary', () => { mkdirSync(join(stubDir, 'home/.opencode/bin'), { recursive: true }) writeBinary(join(stubDir, 'home/.opencode/bin'), '3.0.0') mkdirSync(join(stubDir, 'bundled'), { recursive: true }) writeBinary(join(stubDir, 'bundled'), '2.0.15') const res = runOpenCodeSection(`${installPrelude()}\n${extractOpenCodeInstallSection()}`, { PATH: bundledFirstPath(), }) expect(res.status).toBe(0) expect(existsSync(homeBinPath())).toBe(false) expect(res.stdout).toContain('OpenCode is installed (version: 2.0.15)') expect(res.stdout).toContain('within the supported range >=2.0.15 <3.0.0') expect(curlLog()).toHaveLength(0) }) it('treats every persisted binary as unsupported when neither the minimum nor the bundled version is known', () => { mkdirSync(join(stubDir, 'home/.opencode/bin'), { recursive: true }) writeBinary(join(stubDir, 'home/.opencode/bin'), '2.0.15') const res = runOpenCodeSection(`${installPrelude()}\nreconcile_persisted_opencode`, { OPENCODE_MIN_VERSION: '', OPENCODE_BUNDLED_VERSION: '', }) expect(res.status).toBe(0) expect(existsSync(homeBinPath())).toBe(false) }) it('falls back to the bundled version as the floor when the minimum is unset', () => { mkdirSync(join(stubDir, 'home/.opencode/bin'), { recursive: true }) writeBinary(join(stubDir, 'home/.opencode/bin'), '2.0.15') const res = runOpenCodeSection(`${installPrelude()}\nreconcile_persisted_opencode`, { OPENCODE_MIN_VERSION: '', }) expect(res.status).toBe(0) expect(res.stdout).toContain('Persisted OpenCode 2.0.15 is usable; retaining it') expect(existsSync(homeBinPath())).toBe(true) }) it('removes a malformed persisted home binary and selects the bundled binary without downloading', () => { mkdirSync(join(stubDir, 'home/.opencode/bin'), { recursive: true }) writeBinary(join(stubDir, 'home/.opencode/bin'), 'garbage') mkdirSync(join(stubDir, 'bundled'), { recursive: true }) writeBinary(join(stubDir, 'bundled'), '2.0.15') const res = runOpenCodeSection(`${installPrelude()}\n${extractOpenCodeInstallSection()}`, { PATH: bundledFirstPath(), }) expect(res.status).toBe(0) expect(res.stdout).toContain('malformed or unversioned') expect(existsSync(homeBinPath())).toBe(false) expect(existsSync(bundledBinPath())).toBe(true) expect(res.stdout).toContain('OpenCode is installed (version: 2.0.15)') expect(curlLog()).toHaveLength(0) }) it('removes a persisted 1.x binary and installs the bundled 2.x binary', () => { stubInstallTools() mkdirSync(join(stubDir, 'home/.opencode/bin'), { recursive: true }) writeBinary(join(stubDir, 'home/.opencode/bin'), '1.18.16') const res = runOpenCodeSection( `${installPrelude()}\nreconcile_persisted_opencode\nif [ -e "$HOME/.opencode/bin/opencode" ]; then echo "PERSISTED_PRESENT"; else echo "PERSISTED_REMOVED"; fi\n${extractOpenCodeInstallSection()}` ) expect(res.status).toBe(0) expect(res.stdout).toContain('Persisted OpenCode 1.18.16 is outside the supported range >=2.0.15 <3.0.0') expect(res.stdout).toContain('PERSISTED_REMOVED') expect(res.stdout).not.toContain('PERSISTED_PRESENT') expect(existsSync(homeBinPath())).toBe(true) expect(res.stdout).toContain('Installing OpenCode 2.0.15...') const urls = curlLog().join(' ') expect(urls).toMatch(/https:\/\/opencode\.ai\/files\/bin\/2\.0\.15\/opencode-linux-(x64|arm64)\.tar\.gz/) }) it('appends -musl to the arch suffix when the musl loader is present', () => { stubInstallTools() writeStub('ls', 'exit 0') const res = runOpenCodeSection(`${installPrelude()}\ninstall_opencode`) expect(res.status).toBe(0) const urls = curlLog().join(' ') expect(urls).toMatch(/https:\/\/opencode\.ai\/files\/bin\/2\.0\.15\/opencode-linux-(x64|arm64)-musl\.tar\.gz/) }) }) describe('parse_opencode_version_output', () => { const runParser = (output: string) => { const scriptPath = join(stubDir, 'test.sh') writeFileSync( scriptPath, `set -e\n${readFileSync(releaseHelperPath, 'utf-8')}\nprintf '%s' "$(parse_opencode_version_output ${JSON.stringify(output)})"`, ) return spawnSync('bash', [scriptPath], { encoding: 'utf-8' }) } it('preserves a stable version from prefixed --version output', () => { expect(runParser('opencode version 2.0.15').stdout).toBe('2.0.15') expect(runParser('2.0.15').stdout).toBe('2.0.15') }) it('preserves prerelease and build suffixes so the stability check rejects them', () => { expect(runParser('opencode version 2.0.15-beta.1').stdout).toBe('2.0.15-beta.1') expect(runParser('opencode version 2.0.15+build.5').stdout).toBe('2.0.15+build.5') }) it.each(['2.0.15-', '2.0.15+', '2.0.15.', '2.0.15.1', '2.0.15_1', '2.0.15-beta_1', '2.0.15+build_1'])( 'preserves the whole non-whitespace token %s instead of truncating it to a stable version', (output) => { const parsed = runParser(`opencode version ${output}`).stdout expect(parsed).toBe(output) expect(parsed).not.toBe('2.0.15') }, ) it('returns nothing for malformed output', () => { expect(runParser('garbage').stdout).toBe('') expect(runParser('').stdout).toBe('') }) })