forked from stripe/sync-engine
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathspecParser.test.ts
More file actions
204 lines (184 loc) · 6.4 KB
/
Copy pathspecParser.test.ts
File metadata and controls
204 lines (184 loc) · 6.4 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { describe, expect, it } from 'vitest'
import { SpecParser, RUNTIME_REQUIRED_TABLES, OPENAPI_RESOURCE_TABLE_ALIASES } from '../specParser'
import { minimalStripeOpenApiSpec } from './fixtures/minimalSpec'
import { resolveOpenApiSpec } from '../specFetchHelper'
describe('SpecParser', () => {
it('parses aliased resources into deterministic tables and column types', () => {
const parser = new SpecParser()
const parsed = parser.parse(minimalStripeOpenApiSpec, {
allowedTables: ['checkout_sessions', 'customers', 'early_fraud_warnings'],
})
expect(parsed.tables.map((table) => table.tableName)).toEqual([
'checkout_sessions',
'customers',
'early_fraud_warnings',
])
const customers = parsed.tables.find((table) => table.tableName === 'customers')
expect(customers?.columns).toEqual([
{ name: 'created', type: 'bigint', nullable: false },
{ name: 'deleted', type: 'boolean', nullable: false },
{ name: 'object', type: 'text', nullable: false },
])
const checkoutSessions = parsed.tables.find((table) => table.tableName === 'checkout_sessions')
expect(checkoutSessions?.columns).toContainEqual({
name: 'amount_total',
type: 'bigint',
nullable: false,
})
})
it('injects compatibility columns for runtime-critical tables', () => {
const parser = new SpecParser()
const parsed = parser.parse(
{
...minimalStripeOpenApiSpec,
components: { schemas: {} },
},
{ allowedTables: ['active_entitlements', 'subscription_items'] }
)
const activeEntitlements = parsed.tables.find(
(table) => table.tableName === 'active_entitlements'
)
expect(activeEntitlements?.columns).toContainEqual({
name: 'customer',
type: 'text',
nullable: true,
})
const subscriptionItems = parsed.tables.find(
(table) => table.tableName === 'subscription_items'
)
expect(subscriptionItems?.columns).toContainEqual({
name: 'deleted',
type: 'boolean',
nullable: true,
})
expect(subscriptionItems?.columns).toContainEqual({
name: 'subscription',
type: 'text',
nullable: true,
})
})
it('is deterministic regardless of schema key order', () => {
const parser = new SpecParser()
const normal = parser.parse(minimalStripeOpenApiSpec, {
allowedTables: ['customers', 'plans', 'prices'],
})
const reversedSchemas = Object.fromEntries(
Object.entries(minimalStripeOpenApiSpec.components?.schemas ?? {}).reverse()
)
const reversed = parser.parse(
{
...minimalStripeOpenApiSpec,
components: {
schemas: reversedSchemas,
},
},
{ allowedTables: ['customers', 'plans', 'prices'] }
)
expect(reversed).toEqual(normal)
})
it('marks expandable references from x-expansionResources metadata', () => {
const parser = new SpecParser()
const parsed = parser.parse(
{
...minimalStripeOpenApiSpec,
components: {
schemas: {
charge: {
'x-resourceId': 'charge',
type: 'object',
properties: {
id: { type: 'string' },
customer: {
anyOf: [{ type: 'string' }, { $ref: '#/components/schemas/customer' }],
'x-expansionResources': {
oneOf: [{ $ref: '#/components/schemas/customer' }],
},
},
},
},
customer: {
'x-resourceId': 'customer',
type: 'object',
properties: {
id: { type: 'string' },
},
},
},
},
},
{ allowedTables: ['charges'] }
)
const charges = parsed.tables.find((table) => table.tableName === 'charges')
expect(charges?.columns).toContainEqual({
name: 'customer',
type: 'json',
nullable: false,
expandableReference: true,
})
})
})
describe('SpecParser - Table Modes (runtime_required vs all_projected)', () => {
it('omitting allowedTables parses every resolvable minimal-spec table', () => {
const parser = new SpecParser()
const parsed = parser.parse(minimalStripeOpenApiSpec, {
resourceAliases: OPENAPI_RESOURCE_TABLE_ALIASES,
})
expect(parsed.tables.map((table) => table.tableName)).toEqual([
'active_entitlements',
'checkout_sessions',
'customers',
'early_fraud_warnings',
'features',
'plans',
'prices',
'products',
'subscription_items',
])
})
it('keeps explicit allowedTables filtering and compatibility fallbacks', () => {
const parser = new SpecParser()
const parsed = parser.parse(minimalStripeOpenApiSpec, {
allowedTables: ['checkout_session_line_items', 'customers'],
resourceAliases: OPENAPI_RESOURCE_TABLE_ALIASES,
})
expect(parsed.tables.map((table) => table.tableName)).toEqual([
'checkout_session_line_items',
'customers',
])
const checkoutSessionLineItems = parsed.tables.find(
(table) => table.tableName === 'checkout_session_line_items'
)
expect(checkoutSessionLineItems?.columns).toContainEqual({
name: 'checkout_session',
type: 'text',
nullable: true,
})
expect(checkoutSessionLineItems?.columns).toContainEqual({
name: 'amount_discount',
type: 'bigint',
nullable: true,
})
})
it('all_projected expands beyond runtime_required on the real Stripe 2020-08-27 spec', async () => {
const apiVersion = '2020-08-27'
const resolvedSpec = await resolveOpenApiSpec({
apiVersion,
})
const parser = new SpecParser()
const runtimeParsed = parser.parse(resolvedSpec.spec, {
allowedTables: [...RUNTIME_REQUIRED_TABLES],
resourceAliases: OPENAPI_RESOURCE_TABLE_ALIASES,
})
const allProjectedParsed = parser.parse(resolvedSpec.spec, {
resourceAliases: OPENAPI_RESOURCE_TABLE_ALIASES,
})
expect(runtimeParsed.tables.map((table) => table.tableName)).toEqual(
[...RUNTIME_REQUIRED_TABLES].sort()
)
expect(allProjectedParsed.tables.length).toBeGreaterThan(runtimeParsed.tables.length)
expect(allProjectedParsed.tables.length).toBeGreaterThan(100)
expect(
allProjectedParsed.tables.some((table) => !RUNTIME_REQUIRED_TABLES.includes(table.tableName))
).toBe(true)
}, 60000) // 60 second timeout for fetching real spec
})