-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsync-manifest.test.ts
More file actions
223 lines (199 loc) · 6.91 KB
/
sync-manifest.test.ts
File metadata and controls
223 lines (199 loc) · 6.91 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { describe, expect, test } from "bun:test";
import { z } from "zod";
import { SyncManifestSchema } from "./manifest.ts";
import { SyncManifest } from "./sync-manifest.ts";
type Manifest = z.infer<typeof SyncManifestSchema>;
// Helper to create a basic manifest with minimal required fields
const createEmptyManifest = (slug: string): Manifest => ({
slug,
manifestVersion: 1,
connections: [],
dataStores: [],
eventStores: [],
publicSchemas: [],
consumerSchemas: [],
});
// Helper to add a consumer schema to a manifest that references an external public schema
const addConsumerSchemaWithExternalReference = (
manifest: Manifest,
{
externalManifestSlug,
schemaName,
majorVersion,
destinationSlug = "ds1",
}: {
externalManifestSlug: string;
schemaName: string;
majorVersion: number;
destinationSlug?: string;
},
): Manifest => ({
...manifest,
consumerSchemas: [
...(manifest.consumerSchemas ?? []),
{
name: "consume-public-account",
sourceManifestSlug: externalManifestSlug,
publicSchema: {
name: schemaName,
majorVersion,
},
config: {
consumerSchemaType: "postgres",
destinationDataStoreSlug: destinationSlug,
sql: "SELECT * FROM some_table", // dummy SQL for test
},
},
],
});
describe("SyncManifest", () => {
test("SyncManifest - getExternalConsumerSchemas with no external references", () => {
// When there are no external references (all manifests in one set)
const manifestA = createEmptyManifest("manifest-a");
const manifestB = createEmptyManifest("manifest-b");
// Add a consumer schema that references another manifest within the same set
const manifestBWithConsumer = addConsumerSchemaWithExternalReference(manifestB, {
externalManifestSlug: "manifest-a", // This is NOT external since it's included in our manifests array
schemaName: "internal-schema",
majorVersion: 1,
});
// Add a public schema to match the consumer schema
const manifestAWithPublicSchema = {
...manifestA,
connections: [
{
slug: "ds1",
config: {
connectionType: "in-memory",
},
},
],
dataStores: [
{
connectionSlug: "ds1",
config: {
connectionType: "in-memory",
},
},
],
publicSchemas: [
{
name: "internal-schema",
version: { major: 1, minor: 0 },
source: { dataStoreSlug: "ds1" },
config: {
publicSchemaType: "postgres",
transformations: [
{
operation: "insert",
table: "table1",
sql: "SELECT * FROM table1",
},
],
},
outputSchema: {
type: "object" as const,
properties: {},
},
},
],
} satisfies Manifest;
// Initialize SyncManifest with both manifests
const syncManifest = new SyncManifest([
{
path: "test-manifest.json",
manifest: manifestAWithPublicSchema,
},
{
path: "test-manifest2.json",
manifest: manifestBWithConsumer,
},
]);
// There should be no external schemas since all references are resolved internally
const externalSchemas = syncManifest.getExternalConsumerSchemas();
expect(Object.keys(externalSchemas).length).toBe(0);
});
test("SyncManifest - getExternalConsumerSchemas with multiple external references", () => {
// Create a manifest that references two external manifests
const manifest = createEmptyManifest("local-manifest");
// Add consumer schemas referencing two different external manifests
const manifestWithExternalReferences = addConsumerSchemaWithExternalReference(
addConsumerSchemaWithExternalReference(manifest, {
externalManifestSlug: "external-manifest-1",
schemaName: "users",
majorVersion: 1,
}),
{
externalManifestSlug: "external-manifest-2",
schemaName: "products",
majorVersion: 2,
},
);
// Initialize SyncManifest with just our local manifest
const syncManifest = new SyncManifest(
[
{
path: "test-manifest.json",
manifest: manifestWithExternalReferences,
},
],
{
checkPublicSchemaReferences: false,
},
);
// Get external consumer schemas
const externalSchemas = syncManifest.getExternalConsumerSchemas();
// Should have two external manifests
expect(Object.keys(externalSchemas).length).toBe(2);
// Check external-manifest-1 reference
expect(externalSchemas["external-manifest-1"]).toBeDefined();
expect(externalSchemas["external-manifest-1"].length).toBe(1);
expect(externalSchemas["external-manifest-1"][0].publicSchema.name).toBe("users");
expect(externalSchemas["external-manifest-1"][0].publicSchema.majorVersion).toBe(1);
// Check external-manifest-2 reference
expect(externalSchemas["external-manifest-2"]).toBeDefined();
expect(externalSchemas["external-manifest-2"].length).toBe(1);
expect(externalSchemas["external-manifest-2"][0].publicSchema.name).toBe("products");
expect(externalSchemas["external-manifest-2"][0].publicSchema.majorVersion).toBe(2);
});
test("SyncManifest - getExternalConsumerSchemas with multiple references to same external manifest", () => {
// Create a manifest with multiple consumer schemas referencing the same external manifest
const manifest = createEmptyManifest("local-manifest");
// Add two consumer schemas that reference the same external manifest but different schemas
const manifestWithExternalReferences = addConsumerSchemaWithExternalReference(
addConsumerSchemaWithExternalReference(manifest, {
externalManifestSlug: "external-manifest",
schemaName: "users",
majorVersion: 1,
}),
{
externalManifestSlug: "external-manifest",
schemaName: "products",
majorVersion: 2,
},
);
// Initialize SyncManifest with our local manifest
const syncManifest = new SyncManifest(
[
{
path: "test-manifest.json",
manifest: manifestWithExternalReferences,
},
],
{
checkPublicSchemaReferences: false,
},
);
// Get external consumer schemas
const externalSchemas = syncManifest.getExternalConsumerSchemas();
// Should have one external manifest
expect(Object.keys(externalSchemas).length).toBe(1);
// Check external-manifest references
expect(externalSchemas["external-manifest"]).toBeDefined();
expect(externalSchemas["external-manifest"].length).toBe(2);
// Both schemas should be present
const schemaNames = externalSchemas["external-manifest"].map((cs) => cs.publicSchema.name);
expect(schemaNames).toContain("users");
expect(schemaNames).toContain("products");
});
});