config.test.ts41 lines · main
| 1 | import { test } from 'node:test'; |
| 2 | import { strict as assert } from 'node:assert'; |
| 3 | import { mkdtemp, rm } from 'node:fs/promises'; |
| 4 | import { tmpdir } from 'node:os'; |
| 5 | import { join } from 'node:path'; |
| 6 | |
| 7 | // Override XDG_CONFIG_HOME so tests don't pollute the real ~/.config/briven. |
| 8 | let tempDir: string; |
| 9 | test('setup: redirect XDG_CONFIG_HOME to temp dir', async () => { |
| 10 | tempDir = await mkdtemp(join(tmpdir(), 'briven-config-')); |
| 11 | process.env.XDG_CONFIG_HOME = tempDir; |
| 12 | }); |
| 13 | |
| 14 | test('user-credential round-trip preserves project entries', async () => { |
| 15 | const { readCredentials, writeUserCredential, readUserCredential } = await import('./config.js'); |
| 16 | const start = await readCredentials(); |
| 17 | await writeUserCredential({ |
| 18 | token: 't.fake.jwt', |
| 19 | userId: 'u_round_trip', |
| 20 | apiOrigin: 'https://api.briven.tech', |
| 21 | savedAt: new Date().toISOString(), |
| 22 | }); |
| 23 | const after = await readCredentials(); |
| 24 | assert.equal(after.user?.userId, 'u_round_trip'); |
| 25 | assert.deepEqual(after.projects, start.projects); |
| 26 | const u = await readUserCredential(); |
| 27 | assert.equal(u?.userId, 'u_round_trip'); |
| 28 | }); |
| 29 | |
| 30 | test('clearUserCredential removes only user block, preserves projects', async () => { |
| 31 | const { clearUserCredential, readCredentials } = await import('./config.js'); |
| 32 | await clearUserCredential(); |
| 33 | const after = await readCredentials(); |
| 34 | assert.equal(after.user, undefined); |
| 35 | // projects map should still be present (empty or whatever it was). |
| 36 | assert.ok('projects' in after); |
| 37 | }); |
| 38 | |
| 39 | test('teardown: cleanup temp dir', async () => { |
| 40 | await rm(tempDir, { recursive: true, force: true }); |
| 41 | }); |