|
| 1 | +import { mkdtempSync, readFileSync, rmSync } from 'node:fs' |
| 2 | +import os from 'node:os' |
1 | 3 | import path from 'node:path' |
2 | 4 |
|
3 | 5 | import { describe, expect } from 'vitest' |
@@ -114,4 +116,48 @@ describe('socket config get', async () => { |
114 | 116 | expect(code, 'dry-run should exit with code 0 if input ok').toBe(0) |
115 | 117 | }, |
116 | 118 | ) |
| 119 | + |
| 120 | + cmdit( |
| 121 | + ['config', 'set', 'defaultOrg', 'my-test-org', FLAG_CONFIG, '{}'], |
| 122 | + 'should fail (not report OK) when a full config override prevents persisting', |
| 123 | + async cmd => { |
| 124 | + const { code, stderr, stdout } = await spawnSocketCli(binCliPath, cmd) |
| 125 | + // A full --config override makes the config read-only, so the value cannot |
| 126 | + // be saved. `config set` is a no-op here, so it must fail rather than |
| 127 | + // report a misleading "OK". |
| 128 | + const combined = `${stdout}\n${stderr}` |
| 129 | + expect(combined).toContain('was not saved') |
| 130 | + expect(stdout).not.toContain('OK') |
| 131 | + expect(code, 'an unpersistable set should exit non-zero').toBe(1) |
| 132 | + }, |
| 133 | + ) |
| 134 | + |
| 135 | + cmdit( |
| 136 | + ['config', 'set', 'defaultOrg', 'my-test-org'], |
| 137 | + 'should persist a non-token key when only the API token is overridden via env', |
| 138 | + async cmd => { |
| 139 | + const dataHome = mkdtempSync(path.join(os.tmpdir(), 'socket-cfg-')) |
| 140 | + try { |
| 141 | + const { code, stdout } = await spawnSocketCli(binCliPath, cmd, { |
| 142 | + env: { |
| 143 | + SOCKET_SECURITY_API_TOKEN: 'sktsec_faketoken', |
| 144 | + XDG_DATA_HOME: dataHome, |
| 145 | + }, |
| 146 | + }) |
| 147 | + expect(code, 'a persistable set should exit 0').toBe(0) |
| 148 | + expect(stdout).toContain('OK') |
| 149 | + |
| 150 | + const raw = readFileSync( |
| 151 | + path.join(dataHome, 'socket', 'settings', 'config.json'), |
| 152 | + 'utf8', |
| 153 | + ) |
| 154 | + const saved = JSON.parse(Buffer.from(raw, 'base64').toString('utf8')) |
| 155 | + expect(saved.defaultOrg).toBe('my-test-org') |
| 156 | + // The env token must never be written to disk. |
| 157 | + expect(saved.apiToken).toBeUndefined() |
| 158 | + } finally { |
| 159 | + rmSync(dataHome, { recursive: true, force: true }) |
| 160 | + } |
| 161 | + }, |
| 162 | + ) |
117 | 163 | }) |
0 commit comments