Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { sameScope } from './store'
3
4describe('sameScope', () => {
5 const full = {
6 threadId: 's',
7 userId: 'u',
8 tenantId: 'a',
9 }
10
11 it('matches when threadId and optional dims agree', () => {
12 expect(sameScope(full, full)).toBe(true)
13 expect(sameScope(full, { threadId: 's', userId: 'u', tenantId: 'a' })).toBe(
14 true,
15 )
16 })
17
18 it('rejects different threadId', () => {
19 expect(sameScope(full, { ...full, threadId: 'other' })).toBe(false)
20 })
21
22 it('rejects different userId', () => {
23 expect(
24 sameScope(full, { threadId: 's', userId: 'other', tenantId: 'a' }),
25 ).toBe(false)
26 })
27
28 it('rejects different tenantId', () => {
29 expect(sameScope(full, { ...full, tenantId: 'b' })).toBe(false)
30 })
31
32 it('treats omitted optional dims as exact (not wildcards)', () => {
33 // A query without tenant/user must not match a record that has them —
34 // same isolation model as Redis composite index keys.
35 expect(sameScope(full, { threadId: 's', userId: 'u' })).toBe(false)
36 expect(sameScope(full, { threadId: 's' })).toBe(false)
37 expect(sameScope(full, { threadId: 's', userId: '', tenantId: '' })).toBe(
38 false,
39 )
40 })
41
42 it('matches when both sides omit the same optional dims', () => {
43 expect(sameScope({ threadId: 's' }, { threadId: 's' })).toBe(true)
44 expect(
45 sameScope({ threadId: 's', userId: 'u' }, { threadId: 's', userId: 'u' }),
46 ).toBe(true)
47 })
48
49 it('treats empty string as unset for optional dims', () => {
50 expect(sameScope({ threadId: 's', userId: '' }, { threadId: 's' })).toBe(
51 true,
52 )
53 })
54
55 it('ignores namespace (reserved — no subsystem keys on it yet)', () => {
56 expect(
57 sameScope(
58 { ...full, namespace: 'bank-a' },
59 { ...full, namespace: 'bank-b' },
60 ),
61 ).toBe(true)
62 })
63})