|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { |
| 3 | + checkSelectionContext, |
| 4 | + MAX_SELECTION_CONTEXT_LENGTH, |
| 5 | + nextSelectionVersion, |
| 6 | + parseSelectionContext, |
| 7 | + SELECTION_CONTEXT_HEADER, |
| 8 | + serializeSelectionContext, |
| 9 | + type SelectionContext, |
| 10 | +} from '../src/core/selection-context.js' |
| 11 | + |
| 12 | +const current: SelectionContext = { browserSetId: 'browser-1', sessionId: 'session-A', version: 4 } |
| 13 | +const encode = (value: unknown) => Buffer.from(JSON.stringify(value)).toString('base64url') |
| 14 | + |
| 15 | +describe('selection context', () => { |
| 16 | + it('serializes a canonical header and round-trips safe metadata only', () => { |
| 17 | + expect(SELECTION_CONTEXT_HEADER).toBe('X-OA-Selection-Context') |
| 18 | + const header = serializeSelectionContext(current) |
| 19 | + expect(header).toBe(encode({ browserSetId: 'browser-1', sessionId: 'session-A', version: 4 })) |
| 20 | + expect(parseSelectionContext(header)).toEqual(current) |
| 21 | + expect(serializeSelectionContext({ version: 4, sessionId: 'session-A', browserSetId: 'browser-1' })).toBe(header) |
| 22 | + expect(serializeSelectionContext({ ...current, actor: 'forged' } as SelectionContext)).toBe(header) |
| 23 | + expect(parseSelectionContext(encode({ ...current, actor: 'forged' }))).toBe(null) |
| 24 | + }) |
| 25 | + |
| 26 | + it.each([null, 'session-A', 'unicode-\u00e9'])('accepts exact selection %s without granting authority', sessionId => { |
| 27 | + const context = { ...current, sessionId } |
| 28 | + expect(parseSelectionContext(serializeSelectionContext(context))).toEqual(context) |
| 29 | + expect(checkSelectionContext(serializeSelectionContext(context), context)).toBe(null) |
| 30 | + }) |
| 31 | + |
| 32 | + it.each([ |
| 33 | + null, '', '!', 'e30=', 'e30', 'x'.repeat(MAX_SELECTION_CONTEXT_LENGTH + 1), |
| 34 | + encode(null), encode([]), encode({}), |
| 35 | + encode({ ...current, version: -1 }), encode({ ...current, version: 0.5 }), |
| 36 | + encode({ ...current, version: Number.MAX_SAFE_INTEGER + 1 }), |
| 37 | + encode({ ...current, version: '4' }), encode({ ...current, sessionId: 1 }), |
| 38 | + encode({ ...current, browserSetId: '' }), encode({ ...current, sessionId: '' }), |
| 39 | + encode({ ...current, browserSetId: 'x'.repeat(257) }), |
| 40 | + encode({ ...current, sessionId: 'x'.repeat(257) }), |
| 41 | + encode({ version: 4, sessionId: 'session-A', browserSetId: 'browser-1' }), |
| 42 | + Buffer.from('{"browserSetId":"browser-1","sessionId":"session-A","version":4,"version":4}').toString('base64url'), |
| 43 | + ])('rejects absent or malformed headers: %s', async header => { |
| 44 | + expect(parseSelectionContext(header)).toBe(null) |
| 45 | + const response = checkSelectionContext(header, current)! |
| 46 | + expect(response.status).toBe(409) |
| 47 | + expect(Object.fromEntries(response.headers)).toEqual({ |
| 48 | + 'cache-control': 'no-store', |
| 49 | + 'content-type': 'application/json; charset=utf-8', |
| 50 | + }) |
| 51 | + expect(await response.text()).toBe('{"error":"selection_context_conflict","recoverable":true,"action":"refresh_selection"}\n') |
| 52 | + }) |
| 53 | + |
| 54 | + it.each([ |
| 55 | + { ...current, version: 3 }, |
| 56 | + { ...current, version: 5 }, |
| 57 | + { ...current, sessionId: 'session-B' }, |
| 58 | + { ...current, sessionId: null }, |
| 59 | + { ...current, browserSetId: 'other-browser-or-gate' }, |
| 60 | + ])('rejects stale, future, signed-out, and cross-browser/gate assertions', async intended => { |
| 61 | + const response = checkSelectionContext(serializeSelectionContext(intended), current)! |
| 62 | + expect(response.status).toBe(409) |
| 63 | + expect(response.headers.get('cache-control')).toBe('no-store') |
| 64 | + expect(await response.json()).toEqual({ error: 'selection_context_conflict', recoverable: true, action: 'refresh_selection' }) |
| 65 | + }) |
| 66 | + |
| 67 | + it('detects A -> B -> A and sign-out without resetting versions', () => { |
| 68 | + const before = serializeSelectionContext(current) |
| 69 | + const switched = { ...current, sessionId: 'session-B', version: nextSelectionVersion(current.version) } |
| 70 | + const returned = { ...current, version: nextSelectionVersion(switched.version) } |
| 71 | + const signedOut = { ...returned, sessionId: null, version: nextSelectionVersion(returned.version) } |
| 72 | + expect([switched.version, returned.version, signedOut.version]).toEqual([5, 6, 7]) |
| 73 | + expect(checkSelectionContext(before, returned)!.status).toBe(409) |
| 74 | + expect(checkSelectionContext(before, signedOut)!.status).toBe(409) |
| 75 | + }) |
| 76 | + |
| 77 | + it.each([-1, -0, 0.1, NaN, Infinity, Number.MAX_SAFE_INTEGER + 1])('throws for invalid trusted version %s', version => { |
| 78 | + expect(() => serializeSelectionContext({ ...current, version })).toThrow('Invalid selection context') |
| 79 | + expect(() => checkSelectionContext(null, { ...current, version })).toThrow('Invalid selection context') |
| 80 | + expect(() => nextSelectionVersion(version)).toThrow('Invalid selection version') |
| 81 | + }) |
| 82 | + |
| 83 | + it('supports safe integer boundaries and refuses overflow', () => { |
| 84 | + for (const version of [0, Number.MAX_SAFE_INTEGER]) { |
| 85 | + const context = { ...current, version } |
| 86 | + expect(parseSelectionContext(serializeSelectionContext(context))).toEqual(context) |
| 87 | + } |
| 88 | + expect(nextSelectionVersion(0)).toBe(1) |
| 89 | + expect(nextSelectionVersion(Number.MAX_SAFE_INTEGER - 1)).toBe(Number.MAX_SAFE_INTEGER) |
| 90 | + expect(() => nextSelectionVersion(Number.MAX_SAFE_INTEGER)).toThrow('Selection version exhausted') |
| 91 | + }) |
| 92 | +}) |
0 commit comments