-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathblueprint.test.ts
More file actions
149 lines (130 loc) · 5.39 KB
/
Copy pathblueprint.test.ts
File metadata and controls
149 lines (130 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import {
computeBlueprintKey,
hasBm25Indexes,
stripSchemaHashPrefix
} from '../blueprint';
describe('hasBm25Indexes', () => {
it('is true when the catalog reports a bm25 index in the served schemas', async () => {
const query = jest.fn().mockResolvedValueOnce({ rows: [{ exists: true }] });
await expect(hasBm25Indexes({ query } as any, ['s1', 's2'])).resolves.toBe(true);
// The probe is a single qualified catalog query over the served schemas.
expect(query).toHaveBeenCalledTimes(1);
expect(query).toHaveBeenCalledWith(expect.stringContaining("am.amname = 'bm25'"), [['s1', 's2']]);
});
it('is false when the catalog reports none (or an empty result)', async () => {
const q1 = jest.fn().mockResolvedValueOnce({ rows: [{ exists: false }] });
await expect(hasBm25Indexes({ query: q1 } as any, ['s1'])).resolves.toBe(false);
const q2 = jest.fn().mockResolvedValueOnce({ rows: [] });
await expect(hasBm25Indexes({ query: q2 } as any, ['s1'])).resolves.toBe(false);
});
});
describe('stripSchemaHashPrefix', () => {
it('strips a hashed tenant prefix down to the logical schema', () => {
expect(stripSchemaHashPrefix('marketplace-db-tenant1-5e6b13b2-app-public')).toBe(
'app-public'
);
});
it('returns control-plane / non-hashed schema names unchanged', () => {
expect(stripSchemaHashPrefix('services_public')).toBe('services_public');
expect(stripSchemaHashPrefix('metaschema_public')).toBe('metaschema_public');
expect(stripSchemaHashPrefix('app-public')).toBe('app-public');
});
it('handles multi-dash database names in the prefix', () => {
expect(stripSchemaHashPrefix('my-cool-db-name-deadbeef-auth-private')).toBe(
'auth-private'
);
expect(stripSchemaHashPrefix('app-5e6b13b2-public')).toBe('public');
});
it('only strips through the FIRST 8-hex segment, keeping later ones intact', () => {
expect(
stripSchemaHashPrefix('marketplace-db-tenant1-5e6b13b2-app-deadbeef-zone')
).toBe('app-deadbeef-zone');
});
it('does not treat non-hex or wrong-length segments as a hash', () => {
// 'tenant11' is 8 chars but not all hex; 'abcdef1' is 7 hex chars.
expect(stripSchemaHashPrefix('marketplace-tenant11-app-public')).toBe(
'marketplace-tenant11-app-public'
);
expect(stripSchemaHashPrefix('marketplace-abcdef1-app-public')).toBe(
'marketplace-abcdef1-app-public'
);
});
});
describe('computeBlueprintKey', () => {
const base = {
logicalSchemas: ['app-public', 'auth-public'],
shapeFingerprint: 'fingerprint-a',
flags: { a: 1, b: 2 } as Record<string, any>,
apiName: 'customer-api',
mode: 'domain-lookup'
};
it('produces a stable "bp:"-prefixed sha256 hex key', () => {
const key = computeBlueprintKey(base);
expect(key).toMatch(/^bp:[0-9a-f]{64}$/);
expect(computeBlueprintKey(base)).toBe(key);
});
it('is stable across the key order of flags', () => {
const key1 = computeBlueprintKey({ ...base, flags: { a: 1, b: 2 } });
const key2 = computeBlueprintKey({ ...base, flags: { b: 2, a: 1 } });
expect(key1).toBe(key2);
});
it('is stable across the order of logical schemas', () => {
const key1 = computeBlueprintKey({
...base,
logicalSchemas: ['app-public', 'auth-public']
});
const key2 = computeBlueprintKey({
...base,
logicalSchemas: ['auth-public', 'app-public']
});
expect(key1).toBe(key2);
});
it('differs when the shape fingerprint differs', () => {
const key1 = computeBlueprintKey({ ...base, shapeFingerprint: 'fingerprint-a' });
const key2 = computeBlueprintKey({ ...base, shapeFingerprint: 'fingerprint-b' });
expect(key1).not.toBe(key2);
});
it('differs when flag values differ', () => {
const key1 = computeBlueprintKey({ ...base, flags: { a: 1, b: 2 } });
const key2 = computeBlueprintKey({ ...base, flags: { a: 1, b: 3 } });
expect(key1).not.toBe(key2);
});
it('treats undefined flags like an empty flag set', () => {
const key1 = computeBlueprintKey({ ...base, flags: undefined });
const key2 = computeBlueprintKey({ ...base, flags: {} });
expect(key1).toBe(key2);
});
it('differs when the mode or api name differs', () => {
expect(computeBlueprintKey({ ...base, mode: 'domain-lookup' })).not.toBe(
computeBlueprintKey({ ...base, mode: 'api-name-header' })
);
expect(computeBlueprintKey({ ...base, apiName: 'customer-api' })).not.toBe(
computeBlueprintKey({ ...base, apiName: 'other-api' })
);
});
it('treats null and empty api name identically', () => {
expect(computeBlueprintKey({ ...base, apiName: null })).toBe(
computeBlueprintKey({ ...base, apiName: '' })
);
});
});
describe('computeBlueprintKey dbname isolation (W3 fix)', () => {
const { computeBlueprintKey } = require('../blueprint');
const base = {
logicalSchemas: ['app-public'],
shapeFingerprint: 'f'.repeat(64),
flags: undefined as Record<string, any> | undefined,
apiName: 'api',
mode: 'public'
};
it('same-shape tenants in DIFFERENT physical databases get DIFFERENT keys', () => {
const a = computeBlueprintKey({ ...base, dbname: 'db_a' });
const b = computeBlueprintKey({ ...base, dbname: 'db_b' });
expect(a).not.toBe(b);
});
it('same dbname yields a stable key', () => {
expect(computeBlueprintKey({ ...base, dbname: 'db_a' })).toBe(
computeBlueprintKey({ ...base, dbname: 'db_a' })
);
});
});