|
1 | 1 | import { describe, expect, test } from 'bun:test' |
2 | 2 |
|
3 | 3 | import { |
| 4 | + buildSandboxRuntimeCheck, |
4 | 5 | checkNodeVersion, |
5 | 6 | formatReachabilityFailureDetail, |
| 7 | + isCliSandboxRuntimeStubbed, |
6 | 8 | readNodeExecutableVersion, |
7 | 9 | } from './system-check.ts' |
8 | 10 |
|
@@ -119,3 +121,131 @@ describe('checkNodeVersion', () => { |
119 | 121 | }) |
120 | 122 | }) |
121 | 123 | }) |
| 124 | + |
| 125 | +describe('sandbox runtime diagnostics', () => { |
| 126 | + test('fails when sandbox runtime inspection throws an Error', () => { |
| 127 | + const result = buildSandboxRuntimeCheck({ |
| 128 | + inspectionError: new Error('EACCES: permission denied, open dist/cli.mjs'), |
| 129 | + }) |
| 130 | + |
| 131 | + expect(result).toEqual({ |
| 132 | + ok: false, |
| 133 | + label: 'Sandbox runtime', |
| 134 | + detail: |
| 135 | + 'Unable to inspect CLI sandbox runtime: EACCES: permission denied, open dist/cli.mjs', |
| 136 | + }) |
| 137 | + }) |
| 138 | + |
| 139 | + test('fails when sandbox runtime inspection throws a non-Error value', () => { |
| 140 | + const result = buildSandboxRuntimeCheck({ |
| 141 | + inspectionError: 'bundle read failed', |
| 142 | + }) |
| 143 | + |
| 144 | + expect(result).toEqual({ |
| 145 | + ok: false, |
| 146 | + label: 'Sandbox runtime', |
| 147 | + detail: 'Unable to inspect CLI sandbox runtime: bundle read failed', |
| 148 | + }) |
| 149 | + }) |
| 150 | + |
| 151 | + test('detects sandbox-runtime native stubs in the CLI bundle', () => { |
| 152 | + expect( |
| 153 | + isCliSandboxRuntimeStubbed( |
| 154 | + '// native-stub:@anthropic-ai/sandbox-runtime\nconst noop = () => null', |
| 155 | + ), |
| 156 | + ).toBe(true) |
| 157 | + expect(isCliSandboxRuntimeStubbed('bubblewrap (bwrap) not installed')).toBe( |
| 158 | + false, |
| 159 | + ) |
| 160 | + }) |
| 161 | + |
| 162 | + test('fails when the CLI bundle contains a sandbox runtime stub', () => { |
| 163 | + const result = buildSandboxRuntimeCheck({ |
| 164 | + cliRuntimeStubbed: true, |
| 165 | + sandboxEnabled: true, |
| 166 | + failIfUnavailable: true, |
| 167 | + sandboxingEnabled: false, |
| 168 | + unavailableReason: 'sandbox.enabled is set but the runtime is stubbed', |
| 169 | + }) |
| 170 | + |
| 171 | + expect(result.ok).toBe(false) |
| 172 | + expect(result.label).toBe('Sandbox runtime') |
| 173 | + expect(result.detail).toContain('CLI bundle: stubbed') |
| 174 | + expect(result.detail).toContain('effective behavior: fail-closed') |
| 175 | + expect(result.detail).toContain( |
| 176 | + 'reason: sandbox.enabled is set but the runtime is stubbed', |
| 177 | + ) |
| 178 | + }) |
| 179 | + |
| 180 | + test('reports warning-only behavior when sandbox is enabled but unavailable', () => { |
| 181 | + const result = buildSandboxRuntimeCheck({ |
| 182 | + cliRuntimeStubbed: false, |
| 183 | + sandboxEnabled: true, |
| 184 | + failIfUnavailable: false, |
| 185 | + sandboxingEnabled: false, |
| 186 | + unavailableReason: 'bubblewrap (bwrap) not installed', |
| 187 | + }) |
| 188 | + |
| 189 | + expect(result.ok).toBe(true) |
| 190 | + expect(result.detail).toContain('CLI bundle: real runtime') |
| 191 | + expect(result.detail).toContain('effective behavior: warning-only') |
| 192 | + expect(result.detail).toContain('reason: bubblewrap (bwrap) not installed') |
| 193 | + }) |
| 194 | + |
| 195 | + test('flags fail-closed behavior when sandbox is required but unavailable', () => { |
| 196 | + const result = buildSandboxRuntimeCheck({ |
| 197 | + cliRuntimeStubbed: false, |
| 198 | + sandboxEnabled: true, |
| 199 | + failIfUnavailable: true, |
| 200 | + sandboxingEnabled: false, |
| 201 | + unavailableReason: 'bubblewrap (bwrap) not installed', |
| 202 | + }) |
| 203 | + |
| 204 | + expect(result.ok).toBe(false) |
| 205 | + expect(result.detail).toContain('CLI bundle: real runtime') |
| 206 | + expect(result.detail).toContain('effective behavior: fail-closed') |
| 207 | + expect(result.detail).toContain('reason: bubblewrap (bwrap) not installed') |
| 208 | + }) |
| 209 | + |
| 210 | + test('reports enforcing behavior when sandboxing is active', () => { |
| 211 | + const result = buildSandboxRuntimeCheck({ |
| 212 | + cliRuntimeStubbed: false, |
| 213 | + sandboxEnabled: true, |
| 214 | + failIfUnavailable: true, |
| 215 | + sandboxingEnabled: true, |
| 216 | + }) |
| 217 | + |
| 218 | + expect(result.ok).toBe(true) |
| 219 | + expect(result.detail).toBe( |
| 220 | + 'CLI bundle: real runtime; sandbox.enabled: true; failIfUnavailable: true; effective behavior: enforcing', |
| 221 | + ) |
| 222 | + }) |
| 223 | + |
| 224 | + test('reports disabled behavior without failing when sandbox is not enabled', () => { |
| 225 | + const result = buildSandboxRuntimeCheck({ |
| 226 | + cliRuntimeStubbed: false, |
| 227 | + sandboxEnabled: false, |
| 228 | + failIfUnavailable: false, |
| 229 | + sandboxingEnabled: false, |
| 230 | + }) |
| 231 | + |
| 232 | + expect(result.ok).toBe(true) |
| 233 | + expect(result.detail).toBe( |
| 234 | + 'CLI bundle: real runtime; sandbox.enabled: false; failIfUnavailable: false; effective behavior: disabled', |
| 235 | + ) |
| 236 | + }) |
| 237 | + |
| 238 | + test('reports disabled behavior without failing when sandbox is off and the CLI runtime is stubbed', () => { |
| 239 | + const result = buildSandboxRuntimeCheck({ |
| 240 | + cliRuntimeStubbed: true, |
| 241 | + sandboxEnabled: false, |
| 242 | + failIfUnavailable: false, |
| 243 | + sandboxingEnabled: false, |
| 244 | + }) |
| 245 | + |
| 246 | + expect(result.ok).toBe(true) |
| 247 | + expect(result.detail).toBe( |
| 248 | + 'CLI bundle: stubbed; sandbox.enabled: false; failIfUnavailable: false; effective behavior: disabled', |
| 249 | + ) |
| 250 | + }) |
| 251 | +}) |
0 commit comments