@powersync/service-core 0.0.0-dev-20260909133214 → 0.0.0-dev-20261005112123
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +61 -5
- package/dist/api/diagnostics.js +5 -2
- package/dist/api/diagnostics.js.map +1 -1
- package/dist/auth/JwtPayload.d.ts +2 -2
- package/dist/auth/JwtPayload.js +2 -2
- package/dist/auth/JwtPayload.js.map +1 -1
- package/dist/entry/commands/compact-action.js +1 -1
- package/dist/entry/commands/compact-action.js.map +1 -1
- package/dist/entry/commands/config-command.js +1 -1
- package/dist/entry/commands/config-command.js.map +1 -1
- package/dist/migrations/PowerSyncMigrationManager.js +2 -1
- package/dist/migrations/PowerSyncMigrationManager.js.map +1 -1
- package/dist/replication/AbstractReplicator.js +8 -4
- package/dist/replication/AbstractReplicator.js.map +1 -1
- package/dist/replication/ReplicationModule.d.ts +0 -7
- package/dist/replication/ReplicationModule.js +0 -1
- package/dist/replication/ReplicationModule.js.map +1 -1
- package/dist/routes/configure-fastify.d.ts +3 -3
- package/dist/routes/endpoints/admin.d.ts +6 -6
- package/dist/routes/endpoints/admin.js +15 -11
- package/dist/routes/endpoints/admin.js.map +1 -1
- package/dist/routes/endpoints/socket-route.js +7 -5
- package/dist/routes/endpoints/socket-route.js.map +1 -1
- package/dist/routes/endpoints/sync-rules.js +16 -10
- package/dist/routes/endpoints/sync-rules.js.map +1 -1
- package/dist/storage/BucketStorageFactory.d.ts +21 -2
- package/dist/storage/BucketStorageFactory.js +17 -4
- package/dist/storage/BucketStorageFactory.js.map +1 -1
- package/dist/storage/PersistedReplicationStream.d.ts +2 -1
- package/dist/storage/PersistedReplicationStream.js.map +1 -1
- package/dist/storage/PersistedSyncConfigContent.d.ts +4 -4
- package/dist/storage/PersistedSyncConfigContent.js +12 -5
- package/dist/storage/PersistedSyncConfigContent.js.map +1 -1
- package/dist/storage/StorageEngine.d.ts +4 -0
- package/dist/storage/StorageEngine.js +7 -1
- package/dist/storage/StorageEngine.js.map +1 -1
- package/dist/storage/StorageProvider.d.ts +8 -0
- package/dist/storage/StorageVersionConfig.d.ts +0 -4
- package/dist/storage/StorageVersionConfig.js +0 -4
- package/dist/storage/StorageVersionConfig.js.map +1 -1
- package/dist/storage/SyncConfigParser.d.ts +44 -0
- package/dist/storage/SyncConfigParser.js +73 -0
- package/dist/storage/SyncConfigParser.js.map +1 -0
- package/dist/storage/SyncRulesBucketStorage.d.ts +3 -1
- package/dist/storage/SyncRulesBucketStorage.js.map +1 -1
- package/dist/storage/implementation/IncrementalReprocessingCompatibility.js +9 -5
- package/dist/storage/implementation/IncrementalReprocessingCompatibility.js.map +1 -1
- package/dist/storage/storage-index.d.ts +1 -0
- package/dist/storage/storage-index.js +1 -0
- package/dist/storage/storage-index.js.map +1 -1
- package/dist/sync/BucketChecksumState.d.ts +1 -1
- package/dist/sync/BucketChecksumState.js +5 -4
- package/dist/sync/BucketChecksumState.js.map +1 -1
- package/dist/sync/util.d.ts +1 -1
- package/dist/sync/util.js +3 -0
- package/dist/sync/util.js.map +1 -1
- package/dist/system/ServiceContext.d.ts +20 -0
- package/dist/system/ServiceContext.js +29 -1
- package/dist/system/ServiceContext.js.map +1 -1
- package/dist/util/param-logging.d.ts +5 -2
- package/dist/util/param-logging.js +17 -9
- package/dist/util/param-logging.js.map +1 -1
- package/dist/util/protocol-types.d.ts +7 -7
- package/dist/util/protocol-types.js +3 -3
- package/package.json +5 -5
- package/src/api/diagnostics.ts +6 -2
- package/src/auth/JwtPayload.ts +2 -2
- package/src/entry/commands/compact-action.ts +1 -1
- package/src/entry/commands/config-command.ts +1 -1
- package/src/migrations/PowerSyncMigrationManager.ts +2 -1
- package/src/replication/AbstractReplicator.ts +8 -4
- package/src/replication/ReplicationModule.ts +0 -10
- package/src/routes/endpoints/admin.ts +15 -17
- package/src/routes/endpoints/socket-route.ts +9 -6
- package/src/routes/endpoints/sync-rules.ts +19 -12
- package/src/storage/BucketStorageFactory.ts +33 -6
- package/src/storage/PersistedReplicationStream.ts +2 -1
- package/src/storage/PersistedSyncConfigContent.ts +16 -8
- package/src/storage/StorageEngine.ts +10 -1
- package/src/storage/StorageProvider.ts +8 -0
- package/src/storage/StorageVersionConfig.ts +0 -5
- package/src/storage/SyncConfigParser.ts +124 -0
- package/src/storage/SyncRulesBucketStorage.ts +3 -1
- package/src/storage/implementation/IncrementalReprocessingCompatibility.ts +17 -5
- package/src/storage/storage-index.ts +1 -0
- package/src/sync/BucketChecksumState.ts +5 -4
- package/src/sync/util.ts +4 -1
- package/src/system/ServiceContext.ts +47 -1
- package/src/util/param-logging.ts +21 -14
- package/src/util/protocol-types.ts +5 -5
- package/test/src/AbstractReplicator.test.ts +70 -0
- package/test/src/PersistedSyncConfigContent.test.ts +3 -1
- package/test/src/SyncConfigParser.test.ts +309 -0
- package/test/src/broadcast_iterable.test.ts +26 -8
- package/test/src/diagnostics.test.ts +24 -0
- package/test/src/routes/admin.test.ts +22 -0
- package/test/src/routes/mocks.ts +2 -0
- package/test/src/sync/BucketChecksumState.test.ts +195 -6
- package/test/src/sync/util.test.ts +19 -0
- package/test/src/util/param-logging.test.ts +59 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import { SqlSyncConfigParser } from '@/storage/SyncConfigParser.js';
|
|
2
|
+
import { isCompatible, parsePersistedSyncConfigContent, updateSyncRulesFromConfig } from '@/storage/storage-index.js';
|
|
3
|
+
import { ServiceContextContainer, ServiceContextMode } from '@/system/ServiceContext.js';
|
|
4
|
+
import { logger } from '@powersync/lib-services-framework';
|
|
5
|
+
import * as syncRules from '@powersync/service-sync-rules';
|
|
6
|
+
import {
|
|
7
|
+
AdditionalSyncConfigParser,
|
|
8
|
+
DEFAULT_HYDRATION_STATE,
|
|
9
|
+
HydratedSyncConfig,
|
|
10
|
+
nodeSqlite,
|
|
11
|
+
normalizeSourceTableConfig,
|
|
12
|
+
PrecompiledSyncConfig,
|
|
13
|
+
SyncConfig
|
|
14
|
+
} from '@powersync/service-sync-rules';
|
|
15
|
+
import * as sqlite from 'node:sqlite';
|
|
16
|
+
import { describe, expect, test, vi } from 'vitest';
|
|
17
|
+
|
|
18
|
+
const STREAMS = /* yaml */ `
|
|
19
|
+
# Sync config fixture.
|
|
20
|
+
config:
|
|
21
|
+
edition: 3
|
|
22
|
+
streams:
|
|
23
|
+
orders:
|
|
24
|
+
query: SELECT * FROM orders
|
|
25
|
+
`;
|
|
26
|
+
const CONFIGURED = /* yaml */ `
|
|
27
|
+
# Sync config fixture.
|
|
28
|
+
config:
|
|
29
|
+
edition: 3
|
|
30
|
+
source_table_options:
|
|
31
|
+
orders:
|
|
32
|
+
sample: 10
|
|
33
|
+
streams:
|
|
34
|
+
orders:
|
|
35
|
+
query: SELECT * FROM orders
|
|
36
|
+
`;
|
|
37
|
+
|
|
38
|
+
// This fixture represents an external module. Core only knows the source-table map; sample is module-owned.
|
|
39
|
+
function additionalParser(): AdditionalSyncConfigParser {
|
|
40
|
+
const check = (config: SyncConfig) => {
|
|
41
|
+
for (const options of Object.values(config.sourceTableConfig)) {
|
|
42
|
+
if (typeof (options as { sample?: number })?.sample != 'number') throw new Error('Invalid persisted sample.');
|
|
43
|
+
if ((options as { sample?: number })?.sample === 13) throw new Error('Sample 13 is unsupported.');
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
return {
|
|
47
|
+
id: 'example.tables',
|
|
48
|
+
extendJsonSchema({ schema }) {
|
|
49
|
+
const options = (schema.properties as any).config.properties.source_table_options.additionalProperties;
|
|
50
|
+
options.properties.sample = { type: 'number' };
|
|
51
|
+
},
|
|
52
|
+
parse({ config, context }) {
|
|
53
|
+
const sourceTables = normalizeSourceTableConfig(
|
|
54
|
+
(config as { config?: { source_table_options?: unknown } }).config?.source_table_options
|
|
55
|
+
);
|
|
56
|
+
for (const [table, options] of Object.entries(sourceTables)) {
|
|
57
|
+
if (!Object.hasOwn(options!, 'sample')) continue;
|
|
58
|
+
context.parsedConfig.sourceTableConfig = {
|
|
59
|
+
...context.parsedConfig.sourceTableConfig,
|
|
60
|
+
[table]: options
|
|
61
|
+
};
|
|
62
|
+
const { plan } = context.parsedConfig as PrecompiledSyncConfig;
|
|
63
|
+
plan.moduleData = { ...plan.moduleData, ['example.tables']: null };
|
|
64
|
+
}
|
|
65
|
+
check(context.parsedConfig);
|
|
66
|
+
},
|
|
67
|
+
validatePersisted({ config }) {
|
|
68
|
+
check(config);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function deployOptions(yaml = CONFIGURED, parser = new SqlSyncConfigParser([additionalParser()])) {
|
|
74
|
+
return updateSyncRulesFromConfig(parser.parseContent(yaml, { defaultSchema: 'app' }));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
describe('service sync config parser', () => {
|
|
78
|
+
test('reuses compiled validation until a parser is registered', () => {
|
|
79
|
+
const compile = vi.spyOn(syncRules, 'compileSyncRulesSchemaValidator');
|
|
80
|
+
try {
|
|
81
|
+
const parser = new SqlSyncConfigParser();
|
|
82
|
+
expect(compile).toHaveBeenCalledTimes(1);
|
|
83
|
+
for (let i = 0; i < 2; i++) {
|
|
84
|
+
expect(parser.parseContent(STREAMS, { defaultSchema: 'app' }).errors).toEqual([]);
|
|
85
|
+
expect(() => parser.parseContent(CONFIGURED, { defaultSchema: 'app' })).toThrow();
|
|
86
|
+
}
|
|
87
|
+
expect(compile).toHaveBeenCalledTimes(1);
|
|
88
|
+
|
|
89
|
+
parser.registerParser(additionalParser());
|
|
90
|
+
expect(compile).toHaveBeenCalledTimes(2);
|
|
91
|
+
for (let i = 0; i < 2; i++) {
|
|
92
|
+
expect(parser.parseContent(CONFIGURED, { defaultSchema: 'app' }).errors).toEqual([]);
|
|
93
|
+
expect(() =>
|
|
94
|
+
parser.parseContent(CONFIGURED.replace('sample: 10', 'sample: wrong'), { defaultSchema: 'app' })
|
|
95
|
+
).toThrow();
|
|
96
|
+
}
|
|
97
|
+
expect(compile).toHaveBeenCalledTimes(2);
|
|
98
|
+
} finally {
|
|
99
|
+
compile.mockRestore();
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test('composes generic registrations atomically and keeps returned schema copies isolated', () => {
|
|
104
|
+
const parser = new SqlSyncConfigParser();
|
|
105
|
+
const baseSchema = parser.jsonSchema;
|
|
106
|
+
expect(() => parser.parseContent(CONFIGURED, { defaultSchema: 'app' })).toThrow();
|
|
107
|
+
parser.registerParser(additionalParser());
|
|
108
|
+
expect(parser.parseContent(CONFIGURED, { defaultSchema: 'app' }).errors).toEqual([]);
|
|
109
|
+
expect(baseSchema).toEqual(new SqlSyncConfigParser().jsonSchema);
|
|
110
|
+
const composed = parser.jsonSchema;
|
|
111
|
+
delete (composed.properties as any).config.properties.source_table_options;
|
|
112
|
+
expect(parser.parseContent(CONFIGURED, { defaultSchema: 'app' }).errors).toEqual([]);
|
|
113
|
+
|
|
114
|
+
expect(() => parser.registerParser(additionalParser())).toThrow('already registered');
|
|
115
|
+
expect(() => parser.registerParser({ id: '', parse() {} })).toThrow('must not be empty');
|
|
116
|
+
expect(() =>
|
|
117
|
+
parser.registerParser({
|
|
118
|
+
id: 'bad-schema',
|
|
119
|
+
parse() {},
|
|
120
|
+
extendJsonSchema({ schema }) {
|
|
121
|
+
schema.misspelledKeyword = true;
|
|
122
|
+
}
|
|
123
|
+
})
|
|
124
|
+
).toThrow();
|
|
125
|
+
// The failed schema is not retained and its ID can be used by a corrected registration.
|
|
126
|
+
parser.registerParser({ id: 'bad-schema', parse() {} });
|
|
127
|
+
expect(parser.parseContent(CONFIGURED, { defaultSchema: 'app' }).errors).toEqual([]);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test('supplies one parser to storage and closes registration before asynchronous startup completes', async () => {
|
|
131
|
+
const context = new ServiceContextContainer({
|
|
132
|
+
serviceMode: ServiceContextMode.TEST_CONNECTION,
|
|
133
|
+
configuration: {
|
|
134
|
+
storage: { type: 'example' },
|
|
135
|
+
api_parameters: {
|
|
136
|
+
max_data_fetch_concurrency: 1,
|
|
137
|
+
max_buckets_per_connection: 1,
|
|
138
|
+
max_parameter_query_results: 1
|
|
139
|
+
}
|
|
140
|
+
} as never
|
|
141
|
+
});
|
|
142
|
+
const parser = context.syncConfigParser;
|
|
143
|
+
parser.registerParser(additionalParser());
|
|
144
|
+
let finish!: () => void;
|
|
145
|
+
const ready = new Promise<void>((resolve) => {
|
|
146
|
+
finish = resolve;
|
|
147
|
+
});
|
|
148
|
+
const getStorage = vi.fn(async () => {
|
|
149
|
+
await ready;
|
|
150
|
+
return { storage: {} as never, reportStorage: {} as never, shutDown: async () => {}, tearDown: async () => true };
|
|
151
|
+
});
|
|
152
|
+
context.storageEngine.registerProvider({ type: 'example', getStorage });
|
|
153
|
+
const startup = context.storageEngine.start();
|
|
154
|
+
try {
|
|
155
|
+
expect(() => parser.registerParser({ id: 'too-late', parse() {} })).toThrow(
|
|
156
|
+
'after the storage engine has started'
|
|
157
|
+
);
|
|
158
|
+
finish();
|
|
159
|
+
await startup;
|
|
160
|
+
expect(getStorage).toHaveBeenCalledWith({ resolvedConfig: context.configuration, syncConfigParser: parser });
|
|
161
|
+
expect(context.syncConfigParser).toBe(parser);
|
|
162
|
+
} finally {
|
|
163
|
+
finish();
|
|
164
|
+
await startup;
|
|
165
|
+
await context.storageEngine.shutDown();
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
test('requires recorded modules and semantic validation when restoring a saved plan', () => {
|
|
170
|
+
const parser = new SqlSyncConfigParser([additionalParser()]);
|
|
171
|
+
const compiled = deployOptions();
|
|
172
|
+
const restore = (syncConfigParser = parser, compiledPlan = compiled.config.plan) =>
|
|
173
|
+
parsePersistedSyncConfigContent({
|
|
174
|
+
content: CONFIGURED,
|
|
175
|
+
compiledPlan,
|
|
176
|
+
storageVersion: 2,
|
|
177
|
+
parseOptions: { defaultSchema: 'app' },
|
|
178
|
+
syncConfigParser
|
|
179
|
+
});
|
|
180
|
+
const restored = restore();
|
|
181
|
+
expect(restored.config.sourceTableConfig).toEqual(compiled.config.parsed.config.sourceTableConfig);
|
|
182
|
+
expect(
|
|
183
|
+
restored.config.hydrate({ hydrationState: DEFAULT_HYDRATION_STATE, sqlite: nodeSqlite(sqlite) }).sourceTableConfig
|
|
184
|
+
).toEqual(restored.config.sourceTableConfig);
|
|
185
|
+
expect(() => restore(new SqlSyncConfigParser())).toThrow('Missing required sync config parsers: example.tables');
|
|
186
|
+
const malformed = structuredClone(compiled.config.plan!);
|
|
187
|
+
(malformed.plan.sourceTableConfig!.orders as any).sample = 'wrong';
|
|
188
|
+
expect(() => restore(parser, malformed)).toThrow();
|
|
189
|
+
(malformed.plan.sourceTableConfig!.orders as any).sample = 13;
|
|
190
|
+
expect(() => restore(parser, malformed)).toThrow('Sample 13');
|
|
191
|
+
// A compiled plan is authoritative; its failure must not be hidden by reparsing valid source YAML.
|
|
192
|
+
expect(() => restore(new SqlSyncConfigParser(), null)).toThrow();
|
|
193
|
+
expect(restore(parser, null).config.sourceTableConfig).toEqual(restored.config.sourceTableConfig);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
test('reports missing dependencies, runs required installed validators and ignores unused registered parsers', () => {
|
|
197
|
+
const validatePersisted = vi.fn();
|
|
198
|
+
const parser = new SqlSyncConfigParser([{ id: 'installed', parse() {}, validatePersisted }]);
|
|
199
|
+
const { config } = parser.parseContent(STREAMS, { defaultSchema: 'app' });
|
|
200
|
+
expect(parser.validatePersisted({ config, context: { defaultSchema: 'app' } })).toEqual([]);
|
|
201
|
+
expect(validatePersisted).not.toHaveBeenCalled();
|
|
202
|
+
(config as PrecompiledSyncConfig).plan.moduleData = { installed: null, missing: null, 'also-missing': null };
|
|
203
|
+
expect(() => parser.validatePersisted({ config, context: { defaultSchema: 'app' } })).toThrow(
|
|
204
|
+
'Missing required sync config parsers: missing, also-missing'
|
|
205
|
+
);
|
|
206
|
+
expect(validatePersisted).toHaveBeenCalledTimes(1);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
test('restores transformed module state without applying the authoring schema', () => {
|
|
210
|
+
const parser = new SqlSyncConfigParser([
|
|
211
|
+
{
|
|
212
|
+
id: 'transformed',
|
|
213
|
+
parse({ context }) {
|
|
214
|
+
const { plan } = context.parsedConfig as PrecompiledSyncConfig;
|
|
215
|
+
plan.moduleData = { ...plan.moduleData, ['transformed']: null };
|
|
216
|
+
context.parsedConfig.sourceTableConfig = { orders: { compiled: true } };
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
]);
|
|
220
|
+
const compiled = deployOptions(STREAMS, parser);
|
|
221
|
+
const restore = (syncConfigParser: SqlSyncConfigParser) =>
|
|
222
|
+
parsePersistedSyncConfigContent({
|
|
223
|
+
content: STREAMS,
|
|
224
|
+
compiledPlan: compiled.config.plan,
|
|
225
|
+
storageVersion: 2,
|
|
226
|
+
parseOptions: { defaultSchema: 'app' },
|
|
227
|
+
syncConfigParser
|
|
228
|
+
});
|
|
229
|
+
expect((restore(parser).config as PrecompiledSyncConfig).plan.moduleData).toEqual({ transformed: null });
|
|
230
|
+
expect(restore(parser).config.sourceTableConfig).toEqual(compiled.config.parsed.config.sourceTableConfig);
|
|
231
|
+
expect(() => restore(new SqlSyncConfigParser())).toThrow('Missing required sync config parsers: transformed');
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
test('persists module dependencies without source-table options and accepts legacy plans without module data', () => {
|
|
235
|
+
const parser = new SqlSyncConfigParser([
|
|
236
|
+
{
|
|
237
|
+
id: 'required',
|
|
238
|
+
parse({ context }) {
|
|
239
|
+
const { plan } = context.parsedConfig as PrecompiledSyncConfig;
|
|
240
|
+
plan.moduleData = { ...plan.moduleData, ['required']: null };
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
]);
|
|
244
|
+
const compiled = deployOptions(STREAMS, parser);
|
|
245
|
+
expect(compiled.config.plan!.plan.version).toBe(3);
|
|
246
|
+
expect(compiled.config.plan!.plan.moduleData).toEqual({ required: null });
|
|
247
|
+
const restore = (compiledPlan = compiled.config.plan) =>
|
|
248
|
+
parsePersistedSyncConfigContent({
|
|
249
|
+
content: STREAMS,
|
|
250
|
+
compiledPlan,
|
|
251
|
+
storageVersion: 2,
|
|
252
|
+
parseOptions: { defaultSchema: 'app' },
|
|
253
|
+
syncConfigParser: new SqlSyncConfigParser()
|
|
254
|
+
});
|
|
255
|
+
expect(() => restore()).toThrow('Missing required sync config parsers: required');
|
|
256
|
+
const malformed = structuredClone(compiled.config.plan!);
|
|
257
|
+
(malformed.plan as any).moduleData = 'required';
|
|
258
|
+
expect(() => restore(malformed)).toThrow('Invalid sync config module data');
|
|
259
|
+
const legacy = deployOptions(STREAMS, new SqlSyncConfigParser());
|
|
260
|
+
expect((restore(legacy.config.plan).config as PrecompiledSyncConfig).plan.moduleData).toBeUndefined();
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
test('runs generic persisted validators with context and retains their warnings', () => {
|
|
264
|
+
const hook: AdditionalSyncConfigParser = {
|
|
265
|
+
id: 'core-check',
|
|
266
|
+
parse({ context }) {
|
|
267
|
+
const { plan } = context.parsedConfig as PrecompiledSyncConfig;
|
|
268
|
+
plan.moduleData = { ...plan.moduleData, ['core-check']: null };
|
|
269
|
+
},
|
|
270
|
+
validatePersisted({ config, context }) {
|
|
271
|
+
expect(config.getSourceTables()[0].name).toBe('orders');
|
|
272
|
+
expect(context.defaultSchema).toBe('app');
|
|
273
|
+
context.reportDiagnostic({ level: 'warning', message: 'Review this configuration.' });
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
const parser = new SqlSyncConfigParser([hook]);
|
|
277
|
+
const parsed = parser.parseContent(STREAMS, { defaultSchema: 'app' });
|
|
278
|
+
expect(parser.validatePersisted({ config: parsed.config, context: { defaultSchema: 'app' } })).toEqual([
|
|
279
|
+
expect.objectContaining({ type: 'warning', message: 'Review this configuration.' })
|
|
280
|
+
]);
|
|
281
|
+
hook.validatePersisted = ({ context }) => context.reportDiagnostic({ level: 'fatal', message: 'Rejected.' });
|
|
282
|
+
expect(() => parser.validatePersisted({ config: parsed.config, context: { defaultSchema: 'app' } })).toThrow(
|
|
283
|
+
'Rejected.'
|
|
284
|
+
);
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
test('reuses equal source-table options but requires replacement for additions, changes and removals', () => {
|
|
288
|
+
const first = deployOptions();
|
|
289
|
+
const same = deployOptions(CONFIGURED.replace('SELECT * FROM orders', 'SELECT id FROM orders'));
|
|
290
|
+
const changed = deployOptions(CONFIGURED.replace('sample: 10', 'sample: 20'));
|
|
291
|
+
const absent = deployOptions(STREAMS);
|
|
292
|
+
expect(isCompatible([first.config.plan], same.config, logger)).toBe(true);
|
|
293
|
+
expect(isCompatible([first.config.plan], changed.config, logger)).toBe(false);
|
|
294
|
+
expect(isCompatible([first.config.plan], absent.config, logger)).toBe(false);
|
|
295
|
+
expect(isCompatible([absent.config.plan], first.config, logger)).toBe(false);
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
test('merges equal source-table config once', () => {
|
|
299
|
+
const first = deployOptions().config.parsed.config;
|
|
300
|
+
const same = deployOptions(CONFIGURED.replace('SELECT * FROM orders', 'SELECT id FROM orders')).config.parsed
|
|
301
|
+
.config;
|
|
302
|
+
const hydrate = (definitions: SyncConfig[]) =>
|
|
303
|
+
new HydratedSyncConfig({
|
|
304
|
+
definitions,
|
|
305
|
+
createParams: { hydrationState: DEFAULT_HYDRATION_STATE, sqlite: nodeSqlite(sqlite) }
|
|
306
|
+
});
|
|
307
|
+
expect(hydrate([first, same]).sourceTableConfig).toEqual(first.sourceTableConfig);
|
|
308
|
+
});
|
|
309
|
+
});
|
|
@@ -4,10 +4,16 @@ import { delayEach } from 'ix/asynciterable/operators/delayeach.js';
|
|
|
4
4
|
import { take } from 'ix/asynciterable/operators/take.js';
|
|
5
5
|
import { wrapWithAbort } from 'ix/asynciterable/operators/withabort.js';
|
|
6
6
|
import { toArray } from 'ix/asynciterable/toarray.js';
|
|
7
|
-
import
|
|
8
|
-
|
|
7
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
8
|
+
|
|
9
|
+
// The global setTimeout, not the one from timers/promises: vi.useFakeTimers() only replaces the global timers
|
|
10
|
+
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
9
11
|
|
|
10
12
|
describe('BroadcastIterable', () => {
|
|
13
|
+
afterEach(() => {
|
|
14
|
+
vi.useRealTimers();
|
|
15
|
+
});
|
|
16
|
+
|
|
11
17
|
it('should iterate', async () => {
|
|
12
18
|
const range = AsyncIterableX.from([1, 2, 3]);
|
|
13
19
|
const broadcast = new BroadcastIterable(() => range);
|
|
@@ -47,6 +53,7 @@ describe('BroadcastIterable', () => {
|
|
|
47
53
|
});
|
|
48
54
|
|
|
49
55
|
it('should handle indefinite sources', async () => {
|
|
56
|
+
vi.useFakeTimers();
|
|
50
57
|
const source: IterableSource<number> = (signal) => {
|
|
51
58
|
return wrapWithAbort(interval(1), signal);
|
|
52
59
|
};
|
|
@@ -55,7 +62,9 @@ describe('BroadcastIterable', () => {
|
|
|
55
62
|
|
|
56
63
|
const delayed = delayEach(10)(broadcast);
|
|
57
64
|
expect(broadcast.active).toBe(false);
|
|
58
|
-
const
|
|
65
|
+
const resultsPromise = toArray(take(5)(delayed));
|
|
66
|
+
await vi.advanceTimersByTimeAsync(100);
|
|
67
|
+
const results = await resultsPromise;
|
|
59
68
|
|
|
60
69
|
expect(results.length).toEqual(5);
|
|
61
70
|
expect(results[0]).toEqual(0);
|
|
@@ -66,6 +75,7 @@ describe('BroadcastIterable', () => {
|
|
|
66
75
|
});
|
|
67
76
|
|
|
68
77
|
it('should handle multiple subscribers', async () => {
|
|
78
|
+
vi.useFakeTimers();
|
|
69
79
|
let sourceIndex = 0;
|
|
70
80
|
const source = async function* (signal: AbortSignal) {
|
|
71
81
|
// Test value out by 1000 means it may have used the wrong iteration of the source
|
|
@@ -75,7 +85,7 @@ describe('BroadcastIterable', () => {
|
|
|
75
85
|
});
|
|
76
86
|
for (let i = 0; !signal.aborted; i++) {
|
|
77
87
|
yield base + i;
|
|
78
|
-
await Promise.race([abortedPromise,
|
|
88
|
+
await Promise.race([abortedPromise, sleep(1)]);
|
|
79
89
|
}
|
|
80
90
|
// Test value out by 100 means this wasn't reached
|
|
81
91
|
sourceIndex += 100;
|
|
@@ -88,6 +98,7 @@ describe('BroadcastIterable', () => {
|
|
|
88
98
|
expect(broadcast.active).toBe(false);
|
|
89
99
|
const results1Promise = toArray(take(5)(delayed1));
|
|
90
100
|
const results2Promise = toArray(take(5)(delayed2));
|
|
101
|
+
await vi.advanceTimersByTimeAsync(100);
|
|
91
102
|
const [results1, results2] = [await results1Promise, await results2Promise];
|
|
92
103
|
|
|
93
104
|
expect(broadcast.active).toBe(false);
|
|
@@ -104,7 +115,9 @@ describe('BroadcastIterable', () => {
|
|
|
104
115
|
|
|
105
116
|
// This starts a new source
|
|
106
117
|
const delayed3 = delayEach(10)(broadcast);
|
|
107
|
-
const
|
|
118
|
+
const results3Promise = toArray(take(5)(delayed3));
|
|
119
|
+
await vi.advanceTimersByTimeAsync(100);
|
|
120
|
+
const results3 = await results3Promise;
|
|
108
121
|
expect(results3.length).toEqual(5);
|
|
109
122
|
expect(results3[0]).toEqual(2100);
|
|
110
123
|
expect(results3[4]).toBeGreaterThan(2110);
|
|
@@ -112,6 +125,7 @@ describe('BroadcastIterable', () => {
|
|
|
112
125
|
});
|
|
113
126
|
|
|
114
127
|
it('should handle errors on multiple subscribers', async () => {
|
|
128
|
+
vi.useFakeTimers();
|
|
115
129
|
let sourceIndex = 0;
|
|
116
130
|
const source = async function* (signal: AbortSignal) {
|
|
117
131
|
// Test value out by 1000 means it may have used the wrong iteration of the source
|
|
@@ -124,7 +138,7 @@ describe('BroadcastIterable', () => {
|
|
|
124
138
|
throw new Error('simulated failure');
|
|
125
139
|
}
|
|
126
140
|
yield base + i;
|
|
127
|
-
await Promise.race([abortedPromise,
|
|
141
|
+
await Promise.race([abortedPromise, sleep(1)]);
|
|
128
142
|
}
|
|
129
143
|
// Test value out by 100 means this wasn't reached
|
|
130
144
|
sourceIndex += 100;
|
|
@@ -138,7 +152,9 @@ describe('BroadcastIterable', () => {
|
|
|
138
152
|
const results1Promise = toArray(take(5)(delayed1)) as Promise<number[]>;
|
|
139
153
|
const results2Promise = toArray(take(5)(delayed2)) as Promise<number[]>;
|
|
140
154
|
|
|
141
|
-
const
|
|
155
|
+
const settled = Promise.allSettled([results1Promise, results2Promise]);
|
|
156
|
+
await vi.advanceTimersByTimeAsync(100);
|
|
157
|
+
const [r1, r2] = await settled;
|
|
142
158
|
|
|
143
159
|
expect(r1).toEqual({ status: 'rejected', reason: new Error('simulated failure') });
|
|
144
160
|
expect(r2).toEqual({ status: 'rejected', reason: new Error('simulated failure') });
|
|
@@ -147,7 +163,9 @@ describe('BroadcastIterable', () => {
|
|
|
147
163
|
|
|
148
164
|
// This starts a new source
|
|
149
165
|
const delayed3 = delayEach(10)(broadcast);
|
|
150
|
-
const
|
|
166
|
+
const results3Promise = toArray(take(5)(delayed3));
|
|
167
|
+
await vi.advanceTimersByTimeAsync(100);
|
|
168
|
+
const results3 = await results3Promise;
|
|
151
169
|
expect(results3.length).toEqual(5);
|
|
152
170
|
expect(results3[0]).toEqual(2000);
|
|
153
171
|
expect(results3[4]).toBeGreaterThan(2010);
|
|
@@ -205,4 +205,28 @@ describe('getSyncRulesStatus WAL budget warnings', () => {
|
|
|
205
205
|
expect(result!.version_label).toBe('v6');
|
|
206
206
|
expect(result!.errors).toEqual([expect.objectContaining({ level: 'fatal', message: 'Invalid sync config' })]);
|
|
207
207
|
});
|
|
208
|
+
|
|
209
|
+
test('reports each sync config error with its source location when parsing fails', async () => {
|
|
210
|
+
const yaml = `
|
|
211
|
+
config:
|
|
212
|
+
edition: 3
|
|
213
|
+
streams:
|
|
214
|
+
global:
|
|
215
|
+
query: SELECT id FROM test_table
|
|
216
|
+
unknown_key: true
|
|
217
|
+
`;
|
|
218
|
+
const content = makeSyncRulesContent();
|
|
219
|
+
content.parsed = (options?: any) => {
|
|
220
|
+
SqlSyncRules.fromYaml(yaml, { ...options, defaultSchema: 'public', throwOnError: true });
|
|
221
|
+
throw new Error('Expected parsing to fail');
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
const result = await getSyncRulesStatus(makeRouteAPI(), content, OPTIONS, makeSystemStorage());
|
|
225
|
+
|
|
226
|
+
expect(result!.errors).toHaveLength(1);
|
|
227
|
+
const [error] = result!.errors;
|
|
228
|
+
expect(error.level).toBe('fatal');
|
|
229
|
+
expect(error.message).not.toContain('Expected parsing to fail');
|
|
230
|
+
expect(yaml.slice(error.location!.start_offset, error.location!.end_offset)).toBe('unknown_key');
|
|
231
|
+
});
|
|
208
232
|
});
|
|
@@ -87,6 +87,28 @@ bucket_definitions:
|
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
describe('validate', () => {
|
|
90
|
+
it('uses the service parser and includes its diagnostics', async () => {
|
|
91
|
+
const context = makeContext();
|
|
92
|
+
context.service_context.syncConfigParser.registerParser({
|
|
93
|
+
id: 'example.validation',
|
|
94
|
+
parse({ context }) {
|
|
95
|
+
expect(context.defaultSchema).toBe('public');
|
|
96
|
+
context.reportDiagnostic({ level: 'fatal', message: 'Rejected by module validation.' });
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
const response = await validate.handler({
|
|
100
|
+
context,
|
|
101
|
+
request,
|
|
102
|
+
params: { sync_rules: 'config: { edition: 3 }\nstreams: {}\n' }
|
|
103
|
+
});
|
|
104
|
+
expect(response.errors).toContainEqual(
|
|
105
|
+
expect.objectContaining({
|
|
106
|
+
level: 'fatal',
|
|
107
|
+
message: 'Rejected by module validation.'
|
|
108
|
+
})
|
|
109
|
+
);
|
|
110
|
+
});
|
|
111
|
+
|
|
90
112
|
it('reports errors with source location', async () => {
|
|
91
113
|
const context = makeContext();
|
|
92
114
|
|
package/test/src/routes/mocks.ts
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
RouteAPI,
|
|
8
8
|
RouterEngine,
|
|
9
9
|
ServiceContext,
|
|
10
|
+
SqlSyncConfigParser,
|
|
10
11
|
StorageEngine,
|
|
11
12
|
SyncContext,
|
|
12
13
|
SyncRulesBucketStorage
|
|
@@ -38,6 +39,7 @@ export function mockServiceContext(storage: Partial<SyncRulesBucketStorage> | nu
|
|
|
38
39
|
});
|
|
39
40
|
createCoreAPIMetrics(metricsEngine);
|
|
40
41
|
const service_context: Partial<ServiceContext> = {
|
|
42
|
+
syncConfigParser: new SqlSyncConfigParser(),
|
|
41
43
|
syncContext: new SyncContext({ maxBuckets: 1, maxDataFetchConcurrency: 1, maxParameterQueryResults: 1 }),
|
|
42
44
|
eventsEngine: new EventsEngine(),
|
|
43
45
|
routerEngine: {
|