companion-token-custody-stress.test.mjs
sha256:8915fe406161f95c1681f9469375e7bae5b28c884f00bedbdef65e4b0cd0738d
docs(flow): commit FLOW-V0-SPEC.md hygiene for 7A-INT merge
Human
23 hours ago
| 1 | /** |
| 2 | * Tier 4 — STRESS: many store/rotate/load cycles stay consistent and bounded; the store never |
| 3 | * accumulates stale secrets across rotations. |
| 4 | */ |
| 5 | import { describe, it } from 'node:test'; |
| 6 | import assert from 'node:assert/strict'; |
| 7 | import { KEYCHAIN_ACCOUNTS, buildSessionMeta, createTokenCustody } from '../lib/companion-token-custody.mjs'; |
| 8 | import { makeSyncKeychain } from './helpers/companion-keychain-fake.mjs'; |
| 9 | |
| 10 | describe('Stress — repeated rotation keeps a single live secret per account', () => { |
| 11 | it('10k access-token rotations leave exactly one access token and bounded store size', async () => { |
| 12 | const kc = makeSyncKeychain(); |
| 13 | const custody = createTokenCustody(kc); |
| 14 | const baseMeta = buildSessionMeta({ expiresIn: 60, refreshToken: 'r0', scope: null, tokenType: 'Bearer' }, { now: 0, refreshTtlMs: 1_000_000 }); |
| 15 | await custody.storeSession({ accessToken: 'jwt0', refreshToken: 'r0', meta: baseMeta }); |
| 16 | for (let i = 1; i <= 10_000; i++) { |
| 17 | await custody.updateAccessToken({ accessToken: 'jwt' + i, refreshToken: 'r' + i, meta: buildSessionMeta({ expiresIn: 60, refreshToken: 'r' + i, scope: null, tokenType: 'Bearer' }, { now: i, refreshTtlMs: 1_000_000 }) }); |
| 18 | } |
| 19 | const loaded = await custody.loadSession(); |
| 20 | assert.equal(loaded.accessToken, 'jwt10000'); |
| 21 | assert.equal(loaded.refreshToken, 'r10000'); |
| 22 | // Store holds only the 3 session accounts (no per-rotation accumulation). |
| 23 | assert.ok(kc._store.size <= 4); |
| 24 | }); |
| 25 | |
| 26 | it('10k loopback rotations always yield the latest token only', async () => { |
| 27 | const kc = makeSyncKeychain(); |
| 28 | const custody = createTokenCustody(kc); |
| 29 | for (let i = 0; i < 10_000; i++) await custody.rotateLoopbackToken('lb' + i); |
| 30 | assert.equal(await custody.getLoopbackToken(), 'lb9999'); |
| 31 | assert.equal(kc._store.get(KEYCHAIN_ACCOUNTS.LOOPBACK_TOKEN), 'lb9999'); |
| 32 | }); |
| 33 | }); |
File History
1 commit
sha256:8915fe406161f95c1681f9469375e7bae5b28c884f00bedbdef65e4b0cd0738d
docs(flow): commit FLOW-V0-SPEC.md hygiene for 7A-INT merge
Human
23 hours ago