@peers-app/peers-sdk 0.27.3 → 0.27.5
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/dist/contracts/contract-proxy.d.ts +7 -0
- package/dist/contracts/contract-proxy.js +19 -2
- package/dist/contracts/contract-transport.d.ts +2 -1
- package/dist/contracts/contract-transport.js +24 -2
- package/dist/contracts/create-local-consumer.js +16 -1
- package/dist/contracts/index.d.ts +2 -0
- package/dist/contracts/index.js +2 -0
- package/dist/contracts/system/index.d.ts +2 -0
- package/dist/contracts/system/index.js +2 -0
- package/dist/contracts/system/invites-contract.js +2 -1
- package/dist/contracts/system/sync-check-contract.d.ts +97 -0
- package/dist/contracts/system/sync-check-contract.js +115 -0
- package/dist/contracts/tool-arguments.d.ts +15 -0
- package/dist/contracts/tool-arguments.js +41 -0
- package/dist/contracts/tool-catalog-service.js +2 -12
- package/dist/contracts/tool-catalog.js +6 -2
- package/dist/data/change-tracking.d.ts +40 -0
- package/dist/data/change-tracking.js +89 -0
- package/dist/data/orm/types.d.ts +24 -7
- package/dist/data/orm/types.js +97 -24
- package/dist/define/manifest.js +6 -34
- package/dist/device/binary-peer-connection-v2.d.ts +7 -15
- package/dist/device/binary-peer-connection-v2.js +14 -30
- package/dist/device/connection.d.ts +34 -43
- package/dist/device/connection.js +188 -186
- package/dist/device/device.d.ts +1 -8
- package/dist/device/device.js +1 -30
- package/dist/device/socket.type.d.ts +6 -12
- package/dist/package-loader/package-loader.js +27 -22
- package/dist/system-ids.d.ts +5 -0
- package/dist/system-ids.js +6 -1
- package/package.json +4 -2
- package/dist/context/user-context-identity.test.d.ts +0 -1
- package/dist/context/user-context-identity.test.js +0 -220
- package/dist/contracts/__tests__/action-dispatch.test.d.ts +0 -1
- package/dist/contracts/__tests__/action-dispatch.test.js +0 -319
- package/dist/contracts/__tests__/also-implements.test.d.ts +0 -1
- package/dist/contracts/__tests__/also-implements.test.js +0 -110
- package/dist/contracts/__tests__/builder.test.d.ts +0 -1
- package/dist/contracts/__tests__/builder.test.js +0 -669
- package/dist/contracts/__tests__/contract-events.test.d.ts +0 -1
- package/dist/contracts/__tests__/contract-events.test.js +0 -199
- package/dist/contracts/__tests__/contract-observables.test.d.ts +0 -1
- package/dist/contracts/__tests__/contract-observables.test.js +0 -290
- package/dist/contracts/__tests__/contract-provider-router.test.d.ts +0 -1
- package/dist/contracts/__tests__/contract-provider-router.test.js +0 -315
- package/dist/contracts/__tests__/contract-proxy.test.d.ts +0 -1
- package/dist/contracts/__tests__/contract-proxy.test.js +0 -657
- package/dist/contracts/__tests__/contract-resolvers.test.d.ts +0 -1
- package/dist/contracts/__tests__/contract-resolvers.test.js +0 -758
- package/dist/contracts/__tests__/contract-subscription-lifecycle.test.d.ts +0 -1
- package/dist/contracts/__tests__/contract-subscription-lifecycle.test.js +0 -353
- package/dist/contracts/__tests__/contract-table-events.test.d.ts +0 -1
- package/dist/contracts/__tests__/contract-table-events.test.js +0 -209
- package/dist/contracts/__tests__/contract-transport.test.d.ts +0 -1
- package/dist/contracts/__tests__/contract-transport.test.js +0 -163
- package/dist/contracts/__tests__/extract.test.d.ts +0 -1
- package/dist/contracts/__tests__/extract.test.js +0 -145
- package/dist/contracts/__tests__/frozen-contracts.test.d.ts +0 -1
- package/dist/contracts/__tests__/frozen-contracts.test.js +0 -191
- package/dist/contracts/__tests__/generic-tool-dispatch.test.d.ts +0 -1
- package/dist/contracts/__tests__/generic-tool-dispatch.test.js +0 -130
- package/dist/contracts/__tests__/host-contract-client.test.d.ts +0 -1
- package/dist/contracts/__tests__/host-contract-client.test.js +0 -793
- package/dist/contracts/__tests__/host-contract-consumer.test.d.ts +0 -1
- package/dist/contracts/__tests__/host-contract-consumer.test.js +0 -163
- package/dist/contracts/__tests__/host-contract-transport.test.d.ts +0 -1
- package/dist/contracts/__tests__/host-contract-transport.test.js +0 -66
- package/dist/contracts/__tests__/integration.test.d.ts +0 -1
- package/dist/contracts/__tests__/integration.test.js +0 -348
- package/dist/contracts/__tests__/legacy-removal.test.d.ts +0 -1
- package/dist/contracts/__tests__/legacy-removal.test.js +0 -128
- package/dist/contracts/__tests__/persistent-registry.test.d.ts +0 -1
- package/dist/contracts/__tests__/persistent-registry.test.js +0 -109
- package/dist/contracts/__tests__/playground.test.d.ts +0 -1
- package/dist/contracts/__tests__/playground.test.js +0 -216
- package/dist/contracts/__tests__/registry.test.d.ts +0 -1
- package/dist/contracts/__tests__/registry.test.js +0 -238
- package/dist/contracts/__tests__/remote-contract-transport.test.d.ts +0 -1
- package/dist/contracts/__tests__/remote-contract-transport.test.js +0 -119
- package/dist/contracts/__tests__/system-contracts.test.d.ts +0 -1
- package/dist/contracts/__tests__/system-contracts.test.js +0 -247
- package/dist/contracts/__tests__/target-dispatch.d.ts +0 -11
- package/dist/contracts/__tests__/target-dispatch.js +0 -24
- package/dist/contracts/__tests__/tool-catalog-inventory.test.d.ts +0 -1
- package/dist/contracts/__tests__/tool-catalog-inventory.test.js +0 -44
- package/dist/contracts/__tests__/tool-catalog-service.test.d.ts +0 -1
- package/dist/contracts/__tests__/tool-catalog-service.test.js +0 -482
- package/dist/contracts/__tests__/tool-catalog.test.d.ts +0 -1
- package/dist/contracts/__tests__/tool-catalog.test.js +0 -1199
- package/dist/contracts/__tests__/validate.test.d.ts +0 -1
- package/dist/contracts/__tests__/validate.test.js +0 -991
- package/dist/contracts/__tests__/workflow-context.test.d.ts +0 -1
- package/dist/contracts/__tests__/workflow-context.test.js +0 -50
- package/dist/data/access-level.test.d.ts +0 -1
- package/dist/data/access-level.test.js +0 -53
- package/dist/data/change-tracking.test.d.ts +0 -1
- package/dist/data/change-tracking.test.js +0 -62
- package/dist/data/files/files.test.d.ts +0 -1
- package/dist/data/files/files.test.js +0 -770
- package/dist/data/group-members.test.d.ts +0 -1
- package/dist/data/group-members.test.js +0 -336
- package/dist/data/group-share.test.d.ts +0 -1
- package/dist/data/group-share.test.js +0 -110
- package/dist/data/groups.test.d.ts +0 -1
- package/dist/data/groups.test.js +0 -153
- package/dist/data/orm/cursor.test.d.ts +0 -1
- package/dist/data/orm/cursor.test.js +0 -314
- package/dist/data/orm/data-query.mongo.test.d.ts +0 -1
- package/dist/data/orm/data-query.mongo.test.js +0 -367
- package/dist/data/orm/data-query.sqlite.test.d.ts +0 -1
- package/dist/data/orm/data-query.sqlite.test.js +0 -365
- package/dist/data/orm/data-query.test.d.ts +0 -1
- package/dist/data/orm/data-query.test.js +0 -545
- package/dist/data/orm/dependency-injection.test.d.ts +0 -1
- package/dist/data/orm/dependency-injection.test.js +0 -171
- package/dist/data/orm/event-registry.test.d.ts +0 -1
- package/dist/data/orm/event-registry.test.js +0 -44
- package/dist/data/orm/multi-cursors.test.d.ts +0 -1
- package/dist/data/orm/multi-cursors.test.js +0 -463
- package/dist/data/orm/sql.data-source.test.d.ts +0 -1
- package/dist/data/orm/sql.data-source.test.js +0 -731
- package/dist/data/orm/table-container-events.test.d.ts +0 -1
- package/dist/data/orm/table-container-events.test.js +0 -101
- package/dist/data/orm/table-container.test.d.ts +0 -1
- package/dist/data/orm/table-container.test.js +0 -273
- package/dist/data/orm/table.event-source.test.d.ts +0 -1
- package/dist/data/orm/table.event-source.test.js +0 -331
- package/dist/data/orm/types.test.d.ts +0 -1
- package/dist/data/orm/types.test.js +0 -409
- package/dist/data/package-table-identity.test.d.ts +0 -1
- package/dist/data/package-table-identity.test.js +0 -46
- package/dist/data/package-version-permissions.test.d.ts +0 -1
- package/dist/data/package-version-permissions.test.js +0 -107
- package/dist/data/package-version-resolver.test.d.ts +0 -4
- package/dist/data/package-version-resolver.test.js +0 -543
- package/dist/data/package-versions.test.d.ts +0 -1
- package/dist/data/package-versions.test.js +0 -227
- package/dist/data/persistent-vars.conflict.test.d.ts +0 -1
- package/dist/data/persistent-vars.conflict.test.js +0 -126
- package/dist/data/persistent-vars.migration.test.d.ts +0 -1
- package/dist/data/persistent-vars.migration.test.js +0 -133
- package/dist/data/persistent-vars.package-scope.test.d.ts +0 -1
- package/dist/data/persistent-vars.package-scope.test.js +0 -27
- package/dist/data/sync-recovery-queue.test.d.ts +0 -1
- package/dist/data/sync-recovery-queue.test.js +0 -215
- package/dist/data/table-signing.test.d.ts +0 -1
- package/dist/data/table-signing.test.js +0 -118
- package/dist/data/tool-access.test.d.ts +0 -1
- package/dist/data/tool-access.test.js +0 -166
- package/dist/data/user-permissions.test.d.ts +0 -1
- package/dist/data/user-permissions.test.js +0 -305
- package/dist/data/user-trust-levels.test.d.ts +0 -1
- package/dist/data/user-trust-levels.test.js +0 -37
- package/dist/define/also-implements.test.d.ts +0 -1
- package/dist/define/also-implements.test.js +0 -123
- package/dist/define/declare-contract.test.d.ts +0 -1
- package/dist/define/declare-contract.test.js +0 -573
- package/dist/define/define.test.d.ts +0 -1
- package/dist/define/define.test.js +0 -1010
- package/dist/define/empty-fields.test.d.ts +0 -1
- package/dist/define/empty-fields.test.js +0 -55
- package/dist/define/events-observables.test.d.ts +0 -1
- package/dist/define/events-observables.test.js +0 -1013
- package/dist/define/serve-members.test.d.ts +0 -1
- package/dist/define/serve-members.test.js +0 -630
- package/dist/define/vars.test.d.ts +0 -1
- package/dist/define/vars.test.js +0 -39
- package/dist/device/binary-peer-connection-v2.test.d.ts +0 -1
- package/dist/device/binary-peer-connection-v2.test.js +0 -730
- package/dist/device/binary-peer-connection.d.ts +0 -58
- package/dist/device/binary-peer-connection.js +0 -516
- package/dist/device/binary-peer-connection.test.d.ts +0 -1
- package/dist/device/binary-peer-connection.test.js +0 -184
- package/dist/device/connection.test.d.ts +0 -1
- package/dist/device/connection.test.js +0 -613
- package/dist/device/device-election.test.d.ts +0 -1
- package/dist/device/device-election.test.js +0 -516
- package/dist/device/device.test.d.ts +0 -1
- package/dist/device/device.test.js +0 -243
- package/dist/device/frame-cipher.test.d.ts +0 -1
- package/dist/device/frame-cipher.test.js +0 -219
- package/dist/device/get-trust-level-fn.test.d.ts +0 -1
- package/dist/device/get-trust-level-fn.test.js +0 -685
- package/dist/device/streamed-socket.d.ts +0 -23
- package/dist/device/streamed-socket.js +0 -255
- package/dist/device/streamed-socket.test.d.ts +0 -1
- package/dist/device/streamed-socket.test.js +0 -216
- package/dist/device/tx-encoding.test.d.ts +0 -1
- package/dist/device/tx-encoding.test.js +0 -295
- package/dist/device-pairing/cpace.test.d.ts +0 -1
- package/dist/device-pairing/cpace.test.js +0 -116
- package/dist/device-pairing/device-pairing.test.d.ts +0 -1
- package/dist/device-pairing/device-pairing.test.js +0 -211
- package/dist/hlc.test.d.ts +0 -1
- package/dist/hlc.test.js +0 -45
- package/dist/host-credentials.test.d.ts +0 -1
- package/dist/host-credentials.test.js +0 -88
- package/dist/identity/account-email.test.d.ts +0 -1
- package/dist/identity/account-email.test.js +0 -59
- package/dist/identity/anchor-document.test.d.ts +0 -1
- package/dist/identity/anchor-document.test.js +0 -16
- package/dist/identity/anchor-resolution.test.d.ts +0 -1
- package/dist/identity/anchor-resolution.test.js +0 -130
- package/dist/identity/key-change.test.d.ts +0 -1
- package/dist/identity/key-change.test.js +0 -811
- package/dist/invites/group-approval.test.d.ts +0 -1
- package/dist/invites/group-approval.test.js +0 -223
- package/dist/invites/invite-token.test.d.ts +0 -1
- package/dist/invites/invite-token.test.js +0 -123
- package/dist/keys.test.d.ts +0 -1
- package/dist/keys.test.js +0 -336
- package/dist/logging/console-logger.test.d.ts +0 -1
- package/dist/logging/console-logger.test.js +0 -300
- package/dist/logging/console-logs.table.test.d.ts +0 -1
- package/dist/logging/console-logs.table.test.js +0 -156
- package/dist/observable.test.d.ts +0 -1
- package/dist/observable.test.js +0 -309
- package/dist/package-build/build-package.test.d.ts +0 -1
- package/dist/package-build/build-package.test.js +0 -204
- package/dist/package-build/contract-add.test.d.ts +0 -1
- package/dist/package-build/contract-add.test.js +0 -467
- package/dist/package-build/freeze-contracts.test.d.ts +0 -1
- package/dist/package-build/freeze-contracts.test.js +0 -355
- package/dist/package-installer/package-author-signing.test.d.ts +0 -1
- package/dist/package-installer/package-author-signing.test.js +0 -200
- package/dist/package-installer/package-cloner.test.d.ts +0 -1
- package/dist/package-installer/package-cloner.test.js +0 -278
- package/dist/package-installer/package-creator.test.d.ts +0 -1
- package/dist/package-installer/package-creator.test.js +0 -908
- package/dist/package-installer/package-dev-tools.test.d.ts +0 -1
- package/dist/package-installer/package-dev-tools.test.js +0 -64
- package/dist/package-installer/package-installer.test.d.ts +0 -1
- package/dist/package-installer/package-installer.test.js +0 -882
- package/dist/package-installer/package-propagation.test.d.ts +0 -1
- package/dist/package-installer/package-propagation.test.js +0 -1269
- package/dist/package-installer/package-publisher.test.d.ts +0 -1
- package/dist/package-installer/package-publisher.test.js +0 -153
- package/dist/package-installer/package-remote-checker.test.d.ts +0 -1
- package/dist/package-installer/package-remote-checker.test.js +0 -334
- package/dist/package-installer/package-seed-installer.test.d.ts +0 -1
- package/dist/package-installer/package-seed-installer.test.js +0 -123
- package/dist/package-installer/package-tarball.test.d.ts +0 -1
- package/dist/package-installer/package-tarball.test.js +0 -75
- package/dist/package-installer/peers-core-installer.test.d.ts +0 -1
- package/dist/package-installer/peers-core-installer.test.js +0 -49
- package/dist/package-installer/peers-core-trust-anchor.test.d.ts +0 -1
- package/dist/package-installer/peers-core-trust-anchor.test.js +0 -155
- package/dist/package-loader/contract-package-loader.test.d.ts +0 -1
- package/dist/package-loader/contract-package-loader.test.js +0 -274
- package/dist/package-loader/host-authority-loading.test.d.ts +0 -1
- package/dist/package-loader/host-authority-loading.test.js +0 -104
- package/dist/package-loader/host-dispatch-policy.test.d.ts +0 -1
- package/dist/package-loader/host-dispatch-policy.test.js +0 -56
- package/dist/package-loader/isolated-artifact.test.d.ts +0 -1
- package/dist/package-loader/isolated-artifact.test.js +0 -688
- package/dist/package-loader/isolated-persistent-var-gateway.test.d.ts +0 -1
- package/dist/package-loader/isolated-persistent-var-gateway.test.js +0 -56
- package/dist/package-loader/isolated-table-gateway.test.d.ts +0 -1
- package/dist/package-loader/isolated-table-gateway.test.js +0 -633
- package/dist/package-loader/package-loader.test.d.ts +0 -1
- package/dist/package-loader/package-loader.test.js +0 -1764
- package/dist/package-loader/resolve-consumed-contracts.test.d.ts +0 -1
- package/dist/package-loader/resolve-consumed-contracts.test.js +0 -166
- package/dist/serial-json.compat.test.d.ts +0 -1
- package/dist/serial-json.compat.test.js +0 -369
- package/dist/serial-json.test.d.ts +0 -1
- package/dist/serial-json.test.js +0 -401
- package/dist/testing/mock-host-contract.test.d.ts +0 -1
- package/dist/testing/mock-host-contract.test.js +0 -782
- package/dist/utils.test.d.ts +0 -1
- package/dist/utils.test.js +0 -232
|
@@ -1,731 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
require("../../index");
|
|
4
|
-
const SQLiteDB = require("better-sqlite3");
|
|
5
|
-
const lodash_1 = require("lodash");
|
|
6
|
-
const zod_1 = require("zod");
|
|
7
|
-
const registry_1 = require("../../contracts/registry");
|
|
8
|
-
const tool_catalog_1 = require("../../contracts/tool-catalog");
|
|
9
|
-
const field_type_1 = require("../../types/field-type");
|
|
10
|
-
const zod_types_1 = require("../../types/zod-types");
|
|
11
|
-
const utils_1 = require("../../utils");
|
|
12
|
-
const tool_catalog_fields_1 = require("../tool-catalog-fields");
|
|
13
|
-
const sql_data_source_1 = require("./sql.data-source");
|
|
14
|
-
const subscribable_data_source_1 = require("./subscribable.data-source");
|
|
15
|
-
const types_1 = require("./types");
|
|
16
|
-
class DBHarness {
|
|
17
|
-
_db = null;
|
|
18
|
-
get db() {
|
|
19
|
-
if (!this._db) {
|
|
20
|
-
this._db = new SQLiteDB(":memory:");
|
|
21
|
-
this._db.pragma("journal_mode = WAL");
|
|
22
|
-
}
|
|
23
|
-
return this._db;
|
|
24
|
-
}
|
|
25
|
-
async get(sql, params = []) {
|
|
26
|
-
return this.db.prepare(sql).get(params);
|
|
27
|
-
}
|
|
28
|
-
async all(sql, params = []) {
|
|
29
|
-
return this.db.prepare(sql).all(params);
|
|
30
|
-
}
|
|
31
|
-
async exec(sql, params = []) {
|
|
32
|
-
const result = await this.db.prepare(sql).run(params);
|
|
33
|
-
return { changes: result.changes };
|
|
34
|
-
}
|
|
35
|
-
execSync(sql, params = []) {
|
|
36
|
-
return this.db.prepare(sql).run(params);
|
|
37
|
-
}
|
|
38
|
-
getSync(sql, params = []) {
|
|
39
|
-
return this.db.prepare(sql).get(params);
|
|
40
|
-
}
|
|
41
|
-
async close() {
|
|
42
|
-
this._db?.close();
|
|
43
|
-
this._db = null;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
const db = new DBHarness();
|
|
47
|
-
describe("table", () => {
|
|
48
|
-
const fiveMinutesMs = 5 * 60 * 1000; // 5 minutes in milliseconds
|
|
49
|
-
jest.setTimeout(fiveMinutesMs);
|
|
50
|
-
afterAll(async () => {
|
|
51
|
-
db.db?.close();
|
|
52
|
-
});
|
|
53
|
-
describe("when type is defined in code", () => {
|
|
54
|
-
const taskSchema = zod_1.z.object({
|
|
55
|
-
taskId: zod_types_1.zodPeerId.default(() => (0, utils_1.newid)()),
|
|
56
|
-
title: zod_1.z.string(),
|
|
57
|
-
body: zod_1.z.string().default(""),
|
|
58
|
-
dueDate: zod_1.z.date().optional(),
|
|
59
|
-
completeDT: zod_1.z.date().optional(),
|
|
60
|
-
parentTaskId: zod_types_1.zodPeerId.optional(),
|
|
61
|
-
});
|
|
62
|
-
const taskFields = (0, types_1.schemaToFields)(taskSchema);
|
|
63
|
-
const tasksTableType = {
|
|
64
|
-
name: "test_tasks_code_driven",
|
|
65
|
-
description: "Task",
|
|
66
|
-
primaryKeyName: "taskId",
|
|
67
|
-
fields: taskFields,
|
|
68
|
-
indexes: [
|
|
69
|
-
{ fields: ["title"] },
|
|
70
|
-
{ unique: true, fields: ["taskId", { name: "parentTaskId", order: "DESC" }] },
|
|
71
|
-
],
|
|
72
|
-
};
|
|
73
|
-
const TasksTable = new sql_data_source_1.SQLDataSource(db, tasksTableType);
|
|
74
|
-
beforeAll(async () => {
|
|
75
|
-
await TasksTable.dropTableIfExists();
|
|
76
|
-
});
|
|
77
|
-
it("should do compile time checks and runtime typechecks", async () => {
|
|
78
|
-
const badSavePromise = TasksTable.save({
|
|
79
|
-
id: "test",
|
|
80
|
-
title: 1,
|
|
81
|
-
body: "This is a test task",
|
|
82
|
-
dueDate: new Date(),
|
|
83
|
-
});
|
|
84
|
-
// await expect(badSavePromise).rejects.toThrow(`SQLITE_ERROR: table ${tableName} has no column named id`);
|
|
85
|
-
await expect(badSavePromise).rejects.toThrow(/validation.*failed.*title.*expected string, received number/ims);
|
|
86
|
-
});
|
|
87
|
-
it("should do all CRUD operations", async () => {
|
|
88
|
-
const task1 = {
|
|
89
|
-
taskId: (0, utils_1.newid)(),
|
|
90
|
-
title: "Test Task",
|
|
91
|
-
body: "This is a test task",
|
|
92
|
-
dueDate: new Date(),
|
|
93
|
-
};
|
|
94
|
-
// CREATE
|
|
95
|
-
await TasksTable.save(task1);
|
|
96
|
-
// READ
|
|
97
|
-
let task1Db = await TasksTable.get(task1.taskId);
|
|
98
|
-
expect(task1Db).toEqual(task1);
|
|
99
|
-
// UPDATE
|
|
100
|
-
task1.title = "Updated Task";
|
|
101
|
-
await TasksTable.save(task1);
|
|
102
|
-
task1Db = await TasksTable.get(task1.taskId);
|
|
103
|
-
expect(task1Db).toEqual(task1);
|
|
104
|
-
// DELETE
|
|
105
|
-
await TasksTable.delete(task1.taskId);
|
|
106
|
-
task1Db = await TasksTable.get(task1.taskId);
|
|
107
|
-
expect(task1Db).toBeUndefined();
|
|
108
|
-
});
|
|
109
|
-
it("should enable type-checked queries", async () => {
|
|
110
|
-
const task1 = {
|
|
111
|
-
taskId: (0, utils_1.newid)(),
|
|
112
|
-
title: "Test Task",
|
|
113
|
-
body: "This is a test task",
|
|
114
|
-
dueDate: new Date(),
|
|
115
|
-
};
|
|
116
|
-
// CREATE
|
|
117
|
-
await TasksTable.save(task1);
|
|
118
|
-
// query
|
|
119
|
-
const results = await TasksTable.list({ title: "Test Task" });
|
|
120
|
-
expect(results[0]).toEqual(task1);
|
|
121
|
-
// // @ts-expect-error - making sure type checking is working
|
|
122
|
-
// const badQueryResults = await TasksTable.list({ id: 'test' });
|
|
123
|
-
// // note that sqlite does not check that columns exist, so this query succeeds with no matches
|
|
124
|
-
// expect(badQueryResults).toEqual([]);
|
|
125
|
-
// @ts-expect-error - making sure type checking is working
|
|
126
|
-
const badQueryPromise = TasksTable.list({ id: "test" });
|
|
127
|
-
// SQLite error messages vary by version - some throw "no such column", others succeed with empty results
|
|
128
|
-
// The important thing is that TypeScript catches this at compile time with @ts-expect-error
|
|
129
|
-
// await expect(badQueryPromise).rejects.toThrow(`SQLITE_ERROR: table ${tableName} has no column named id`);
|
|
130
|
-
// await expect(badQueryPromise).rejects.toThrow(`no such column: "id" - should this be a string literal in single-quotes?`);
|
|
131
|
-
// Instead of checking for a specific error, we check that either:
|
|
132
|
-
// 1. It throws an error (newer SQLite versions), OR
|
|
133
|
-
// 2. It returns empty results (older SQLite versions that don't validate column names)
|
|
134
|
-
try {
|
|
135
|
-
const results = await badQueryPromise;
|
|
136
|
-
// If no error is thrown, expect empty results (SQLite doesn't always validate column existence)
|
|
137
|
-
expect(results).toEqual([]);
|
|
138
|
-
}
|
|
139
|
-
catch (error) {
|
|
140
|
-
// If an error is thrown, it should mention the column
|
|
141
|
-
expect(error.message).toMatch(/id|column/i);
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
|
-
it("should validate the data before saving", async () => {
|
|
145
|
-
// test validates a bad date
|
|
146
|
-
{
|
|
147
|
-
const badTask = {
|
|
148
|
-
taskId: (0, utils_1.newid)(),
|
|
149
|
-
title: "Test Task",
|
|
150
|
-
body: "This is a test task",
|
|
151
|
-
dueDate: "not a date",
|
|
152
|
-
};
|
|
153
|
-
const badSavePromise = TasksTable.save(badTask);
|
|
154
|
-
await expect(badSavePromise).rejects.toThrow("Expected date, received string");
|
|
155
|
-
}
|
|
156
|
-
// test validates a bad id
|
|
157
|
-
{
|
|
158
|
-
const badTask = {
|
|
159
|
-
taskId: "not an id",
|
|
160
|
-
title: "Test Task",
|
|
161
|
-
body: "This is a test task",
|
|
162
|
-
dueDate: new Date(),
|
|
163
|
-
};
|
|
164
|
-
const badSavePromise = TasksTable.save(badTask);
|
|
165
|
-
await expect(badSavePromise).rejects.toThrow("Invalid id");
|
|
166
|
-
}
|
|
167
|
-
// add task to database
|
|
168
|
-
const existingTaskId = (0, utils_1.newid)();
|
|
169
|
-
const task1 = {
|
|
170
|
-
taskId: existingTaskId,
|
|
171
|
-
title: "Test Task",
|
|
172
|
-
body: "This is a test task",
|
|
173
|
-
dueDate: new Date(),
|
|
174
|
-
};
|
|
175
|
-
await TasksTable.save(task1);
|
|
176
|
-
// test validates a duplicate id
|
|
177
|
-
{
|
|
178
|
-
// First verify the record actually exists
|
|
179
|
-
const existingRecord = await TasksTable.get(existingTaskId);
|
|
180
|
-
expect(existingRecord).toBeDefined();
|
|
181
|
-
expect(existingRecord?.taskId).toBe(existingTaskId);
|
|
182
|
-
const badTask = {
|
|
183
|
-
taskId: existingTaskId,
|
|
184
|
-
title: "Test Task",
|
|
185
|
-
body: "This is a test task",
|
|
186
|
-
dueDate: new Date(),
|
|
187
|
-
};
|
|
188
|
-
// Try-catch approach to handle environments where UNIQUE constraint might not throw
|
|
189
|
-
// or might throw with different error messages
|
|
190
|
-
try {
|
|
191
|
-
await TasksTable.insert(badTask);
|
|
192
|
-
// If we get here without throwing, the test should fail
|
|
193
|
-
// This can happen in some SQLite configurations that don't enforce UNIQUE constraints properly
|
|
194
|
-
fail("Expected insert to throw UNIQUE constraint error, but it succeeded");
|
|
195
|
-
}
|
|
196
|
-
catch (error) {
|
|
197
|
-
// Verify the error is related to UNIQUE constraint or primary key
|
|
198
|
-
expect(error.message).toMatch(/UNIQUE|constraint|PRIMARY KEY|taskId/i);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
// test validates a bad date on update
|
|
202
|
-
{
|
|
203
|
-
const badTask = {
|
|
204
|
-
taskId: existingTaskId,
|
|
205
|
-
title: "Test Task",
|
|
206
|
-
body: "This is a test task",
|
|
207
|
-
dueDate: "not a date",
|
|
208
|
-
};
|
|
209
|
-
const badSavePromise = TasksTable.update(badTask);
|
|
210
|
-
await expect(badSavePromise).rejects.toThrow("Expected date, received string");
|
|
211
|
-
}
|
|
212
|
-
});
|
|
213
|
-
it("should allow initializing a record with only part of the data", async () => {
|
|
214
|
-
const newTask = TasksTable.initRecord();
|
|
215
|
-
expect(() => TasksTable.schema.parse(newTask)).toThrow();
|
|
216
|
-
newTask.title = "Test Task";
|
|
217
|
-
expect(() => TasksTable.schema.parse(newTask)).not.toThrow();
|
|
218
|
-
});
|
|
219
|
-
it("applies schema defaults on insert and preserves required fields on partial update", async () => {
|
|
220
|
-
const inserted = await TasksTable.insert({ title: "Defaulted body" });
|
|
221
|
-
expect(inserted.body).toBe("");
|
|
222
|
-
const stored = await TasksTable.get(inserted.taskId);
|
|
223
|
-
expect(stored?.body).toBe("");
|
|
224
|
-
await TasksTable.update({
|
|
225
|
-
taskId: inserted.taskId,
|
|
226
|
-
title: "Renamed",
|
|
227
|
-
});
|
|
228
|
-
const afterUpdate = await TasksTable.get(inserted.taskId);
|
|
229
|
-
expect(afterUpdate?.title).toBe("Renamed");
|
|
230
|
-
expect(afterUpdate?.body).toBe("");
|
|
231
|
-
});
|
|
232
|
-
afterAll(async () => {
|
|
233
|
-
await TasksTable.dropTableIfExists();
|
|
234
|
-
await TasksTable.db.close();
|
|
235
|
-
});
|
|
236
|
-
});
|
|
237
|
-
describe("when type is loaded from database", () => {
|
|
238
|
-
const tasksTableType = {
|
|
239
|
-
name: "test_tasks_data_driven",
|
|
240
|
-
description: "Task",
|
|
241
|
-
primaryKeyName: "taskId",
|
|
242
|
-
fields: [
|
|
243
|
-
{ name: "taskId", description: "Task ID", type: field_type_1.FieldType.id },
|
|
244
|
-
{ name: "title", description: "Title", type: field_type_1.FieldType.string },
|
|
245
|
-
{ name: "body", description: "Body", type: field_type_1.FieldType.string },
|
|
246
|
-
{ name: "dueDate", description: "Due Date", type: field_type_1.FieldType.Date, optional: true },
|
|
247
|
-
{ name: "completeDT", description: "Complete Date", type: field_type_1.FieldType.Date, optional: true },
|
|
248
|
-
{ name: "parentTaskId", description: "Parent Task ID", type: field_type_1.FieldType.id, optional: true },
|
|
249
|
-
],
|
|
250
|
-
};
|
|
251
|
-
const TasksTable = new sql_data_source_1.SQLDataSource(db, tasksTableType);
|
|
252
|
-
beforeAll(async () => {
|
|
253
|
-
await TasksTable.dropTableIfExists();
|
|
254
|
-
});
|
|
255
|
-
it("should do runtime typechecks", async () => {
|
|
256
|
-
const badSavePromise = TasksTable.save({
|
|
257
|
-
id: "test",
|
|
258
|
-
title: 1,
|
|
259
|
-
body: "This is a test task",
|
|
260
|
-
dueDate: new Date(),
|
|
261
|
-
});
|
|
262
|
-
// await expect(badSavePromise).rejects.toThrow(`SQLITE_ERROR: table ${tableName} has no column named id`);
|
|
263
|
-
await expect(badSavePromise).rejects.toThrow(/validation.*failed.*title.*expected string, received number/ims);
|
|
264
|
-
});
|
|
265
|
-
it("should do all CRUD operations", async () => {
|
|
266
|
-
const task1 = {
|
|
267
|
-
taskId: (0, utils_1.newid)(),
|
|
268
|
-
title: "Test Task",
|
|
269
|
-
body: "This is a test task",
|
|
270
|
-
dueDate: new Date(),
|
|
271
|
-
};
|
|
272
|
-
// CREATE
|
|
273
|
-
await TasksTable.save(task1);
|
|
274
|
-
// READ
|
|
275
|
-
let task1Db = await TasksTable.get(task1.taskId);
|
|
276
|
-
expect(task1Db).toEqual(task1);
|
|
277
|
-
// UPDATE
|
|
278
|
-
task1.title = "Updated Task";
|
|
279
|
-
await TasksTable.save(task1);
|
|
280
|
-
task1Db = await TasksTable.get(task1.taskId);
|
|
281
|
-
expect(task1Db).toEqual(task1);
|
|
282
|
-
// DELETE
|
|
283
|
-
await TasksTable.delete(task1.taskId);
|
|
284
|
-
task1Db = await TasksTable.get(task1.taskId);
|
|
285
|
-
expect(task1Db).toBeUndefined();
|
|
286
|
-
});
|
|
287
|
-
afterAll(async () => {
|
|
288
|
-
await TasksTable.dropTableIfExists();
|
|
289
|
-
await TasksTable.db.close();
|
|
290
|
-
});
|
|
291
|
-
});
|
|
292
|
-
describe("when the schema has changed", () => {
|
|
293
|
-
const notesTableTypeV1 = {
|
|
294
|
-
name: "test_table_notes",
|
|
295
|
-
description: "Notes",
|
|
296
|
-
primaryKeyName: "entryId",
|
|
297
|
-
fields: [
|
|
298
|
-
{ name: "entryId", description: "Entry ID", type: field_type_1.FieldType.id },
|
|
299
|
-
{ name: "title", description: "Title", type: field_type_1.FieldType.string },
|
|
300
|
-
{ name: "body", description: "Body", type: field_type_1.FieldType.string, defaultValue: "" },
|
|
301
|
-
],
|
|
302
|
-
indexes: [{ fields: ["title"] }],
|
|
303
|
-
};
|
|
304
|
-
const NotesTableV1 = new sql_data_source_1.SQLDataSource(db, notesTableTypeV1);
|
|
305
|
-
beforeAll(async () => {
|
|
306
|
-
await NotesTableV1.dropTableIfExists();
|
|
307
|
-
await NotesTableV1.list();
|
|
308
|
-
});
|
|
309
|
-
it("should not update table if already up to date", async () => {
|
|
310
|
-
const updated = await NotesTableV1.initTable(true);
|
|
311
|
-
expect(updated).toBe(false);
|
|
312
|
-
});
|
|
313
|
-
it("should detect column changes and modify tables to match", async () => {
|
|
314
|
-
const v1Note = await NotesTableV1.insert({ title: "Test Note", body: "This is a test note" });
|
|
315
|
-
// add optional number column
|
|
316
|
-
const notesTableTypeV2 = (0, lodash_1.cloneDeep)(notesTableTypeV1);
|
|
317
|
-
notesTableTypeV2.fields.push({
|
|
318
|
-
name: "priority",
|
|
319
|
-
description: "Priority of note",
|
|
320
|
-
type: field_type_1.FieldType.number,
|
|
321
|
-
optional: true,
|
|
322
|
-
});
|
|
323
|
-
const NotesTableV2 = new sql_data_source_1.SQLDataSource(db, notesTableTypeV2);
|
|
324
|
-
const updatedToV2 = await NotesTableV2.initTable(true);
|
|
325
|
-
expect(updatedToV2).toBe(true);
|
|
326
|
-
const v2Note = await NotesTableV2.get(v1Note.entryId);
|
|
327
|
-
expect(v2Note).toEqual({ ...v1Note });
|
|
328
|
-
// add required date column
|
|
329
|
-
const notesTableTypeV3 = (0, lodash_1.cloneDeep)(notesTableTypeV2);
|
|
330
|
-
notesTableTypeV3.fields.push({
|
|
331
|
-
name: "modified",
|
|
332
|
-
description: "Last time this was modified",
|
|
333
|
-
type: field_type_1.FieldType.Date,
|
|
334
|
-
defaultValue: () => new Date(),
|
|
335
|
-
});
|
|
336
|
-
const NotesTableV3 = new sql_data_source_1.SQLDataSource(db, notesTableTypeV3);
|
|
337
|
-
const updatedToV3 = await NotesTableV3.initTable(true);
|
|
338
|
-
expect(updatedToV3).toBe(true);
|
|
339
|
-
const v3Note = await NotesTableV3.get(v1Note.entryId);
|
|
340
|
-
expect(v3Note).toEqual({ ...v1Note, modified: v3Note?.modified });
|
|
341
|
-
// remove column
|
|
342
|
-
const notesTableTypeV4 = (0, lodash_1.cloneDeep)(notesTableTypeV3);
|
|
343
|
-
notesTableTypeV4.fields.pop();
|
|
344
|
-
const NotesTableV4 = new sql_data_source_1.SQLDataSource(db, notesTableTypeV4);
|
|
345
|
-
const updatedToV4 = await NotesTableV4.initTable(true);
|
|
346
|
-
expect(updatedToV4).toBe(true);
|
|
347
|
-
const v4Note = await NotesTableV4.get(v1Note.entryId);
|
|
348
|
-
expect(v4Note).toEqual({ ...v1Note });
|
|
349
|
-
// change column type and make required
|
|
350
|
-
const notesTableTypeV5 = (0, lodash_1.cloneDeep)(notesTableTypeV4);
|
|
351
|
-
notesTableTypeV5.fields[3].type = field_type_1.FieldType.boolean;
|
|
352
|
-
notesTableTypeV5.fields[3].optional = false;
|
|
353
|
-
const NotesTableV5 = new sql_data_source_1.SQLDataSource(db, notesTableTypeV5);
|
|
354
|
-
const updatedToV5 = await NotesTableV5.initTable(true);
|
|
355
|
-
expect(updatedToV5).toBe(true);
|
|
356
|
-
const v5Note = await NotesTableV5.get(v1Note.entryId);
|
|
357
|
-
expect(v5Note).toEqual({ ...v1Note, priority: false });
|
|
358
|
-
const updatedToV5Again = await NotesTableV5.initTable(true);
|
|
359
|
-
expect(updatedToV5Again).toBe(false);
|
|
360
|
-
});
|
|
361
|
-
it("should detect and change column indexes", async () => {
|
|
362
|
-
const notesTableTypeV1Indexes = (0, lodash_1.cloneDeep)(notesTableTypeV1);
|
|
363
|
-
notesTableTypeV1Indexes.name += "_indexes";
|
|
364
|
-
const NotesTableV1 = new sql_data_source_1.SQLDataSource(db, notesTableTypeV1Indexes);
|
|
365
|
-
await NotesTableV1.dropTableIfExists();
|
|
366
|
-
await NotesTableV1.initTable(true);
|
|
367
|
-
const indexesV1 = await NotesTableV1.db.all(`PRAGMA index_list("${NotesTableV1.tableName}")`, []);
|
|
368
|
-
expect(indexesV1).toHaveLength(2);
|
|
369
|
-
// remove indexes
|
|
370
|
-
const notesTableTypeV2Indexes = (0, lodash_1.cloneDeep)(notesTableTypeV1Indexes);
|
|
371
|
-
notesTableTypeV2Indexes.indexes = [];
|
|
372
|
-
const NotesTableV2 = new sql_data_source_1.SQLDataSource(db, notesTableTypeV2Indexes);
|
|
373
|
-
const updatedToV2 = await NotesTableV2.initTable(true);
|
|
374
|
-
// indexes are updated separately from tables
|
|
375
|
-
expect(updatedToV2).toBe(false);
|
|
376
|
-
const indexesV2 = await NotesTableV1.db.all(`PRAGMA index_list("${NotesTableV1.tableName}")`, []);
|
|
377
|
-
expect(indexesV2).toHaveLength(1);
|
|
378
|
-
});
|
|
379
|
-
});
|
|
380
|
-
describe("when the field type is `object`", () => {
|
|
381
|
-
const tableType = {
|
|
382
|
-
name: "test_table_objects",
|
|
383
|
-
description: "Objects",
|
|
384
|
-
primaryKeyName: "objectId",
|
|
385
|
-
fields: [
|
|
386
|
-
{ name: "objectId", description: "Object ID", type: field_type_1.FieldType.id },
|
|
387
|
-
{ name: "name", description: "Name", type: field_type_1.FieldType.string },
|
|
388
|
-
{ name: "data", description: "Data", type: field_type_1.FieldType.object },
|
|
389
|
-
],
|
|
390
|
-
};
|
|
391
|
-
const ObjectsTable = new sql_data_source_1.SQLDataSource(db, tableType);
|
|
392
|
-
beforeAll(async () => {
|
|
393
|
-
await ObjectsTable.dropTableIfExists();
|
|
394
|
-
});
|
|
395
|
-
it("should store objects as JSON strings", async () => {
|
|
396
|
-
const obj = {
|
|
397
|
-
objectId: (0, utils_1.newid)(),
|
|
398
|
-
name: "Test Object",
|
|
399
|
-
data: { test: "data" },
|
|
400
|
-
};
|
|
401
|
-
await ObjectsTable.save(obj);
|
|
402
|
-
const objDb = await ObjectsTable.get(obj.objectId);
|
|
403
|
-
expect(objDb).toEqual(obj);
|
|
404
|
-
});
|
|
405
|
-
afterAll(async () => {
|
|
406
|
-
await ObjectsTable.dropTableIfExists();
|
|
407
|
-
await ObjectsTable.db.close();
|
|
408
|
-
});
|
|
409
|
-
});
|
|
410
|
-
describe("when the field type is an array of primitives", () => {
|
|
411
|
-
const tableType = {
|
|
412
|
-
name: "test_table_arrays",
|
|
413
|
-
description: "Arrays",
|
|
414
|
-
primaryKeyName: "arrayId",
|
|
415
|
-
fields: [
|
|
416
|
-
{ name: "arrayId", description: "Array ID", type: field_type_1.FieldType.id },
|
|
417
|
-
{ name: "name", description: "Name", type: field_type_1.FieldType.string },
|
|
418
|
-
{ name: "data", description: "Data", type: field_type_1.FieldType.string, isArray: true },
|
|
419
|
-
],
|
|
420
|
-
};
|
|
421
|
-
const ArraysTable = new sql_data_source_1.SQLDataSource(db, tableType);
|
|
422
|
-
beforeAll(async () => {
|
|
423
|
-
await ArraysTable.dropTableIfExists();
|
|
424
|
-
});
|
|
425
|
-
it("should store arrays as JSON strings and automatically rehydrate them", async () => {
|
|
426
|
-
const obj = {
|
|
427
|
-
arrayId: (0, utils_1.newid)(),
|
|
428
|
-
name: "Test Array",
|
|
429
|
-
data: ["test", "data"],
|
|
430
|
-
};
|
|
431
|
-
await ArraysTable.save(obj);
|
|
432
|
-
const objDb = await ArraysTable.get(obj.arrayId);
|
|
433
|
-
expect(objDb).toEqual(obj);
|
|
434
|
-
});
|
|
435
|
-
afterAll(async () => {
|
|
436
|
-
await ArraysTable.dropTableIfExists();
|
|
437
|
-
await ArraysTable.db.close();
|
|
438
|
-
});
|
|
439
|
-
});
|
|
440
|
-
describe("when a field is empty", () => {
|
|
441
|
-
const tableType = {
|
|
442
|
-
name: "test_table_empty_values",
|
|
443
|
-
description: "Empty values",
|
|
444
|
-
primaryKeyName: "rowId",
|
|
445
|
-
fields: [
|
|
446
|
-
{ name: "rowId", type: field_type_1.FieldType.id },
|
|
447
|
-
{ name: "title", type: field_type_1.FieldType.string },
|
|
448
|
-
{ name: "note", type: field_type_1.FieldType.string, optional: true },
|
|
449
|
-
{ name: "count", type: field_type_1.FieldType.number, optional: true },
|
|
450
|
-
{ name: "when", type: field_type_1.FieldType.Date, optional: true },
|
|
451
|
-
{
|
|
452
|
-
name: "detail",
|
|
453
|
-
type: field_type_1.FieldType.object,
|
|
454
|
-
optional: true,
|
|
455
|
-
fields: [
|
|
456
|
-
{ name: "city", type: field_type_1.FieldType.string },
|
|
457
|
-
{ name: "zip", type: field_type_1.FieldType.string, optional: true },
|
|
458
|
-
],
|
|
459
|
-
},
|
|
460
|
-
{ name: "tags", type: field_type_1.FieldType.string, isArray: true, optional: true },
|
|
461
|
-
{ name: "rank", type: field_type_1.FieldType.number, defaultValue: 5 },
|
|
462
|
-
{ name: "priority", type: field_type_1.FieldType.number, optional: true, defaultValue: 2 },
|
|
463
|
-
],
|
|
464
|
-
};
|
|
465
|
-
const EmptyTable = new sql_data_source_1.SQLDataSource(db, tableType);
|
|
466
|
-
const empty = { note: null, count: undefined, when: null, tags: null };
|
|
467
|
-
beforeAll(async () => {
|
|
468
|
-
await EmptyTable.dropTableIfExists();
|
|
469
|
-
});
|
|
470
|
-
it("leaves out null and undefined optional fields and reads them back unset", async () => {
|
|
471
|
-
const detail = { city: "Austin", zip: null };
|
|
472
|
-
const row = { rowId: (0, utils_1.newid)(), title: "a", ...empty, detail };
|
|
473
|
-
const expected = {
|
|
474
|
-
rowId: row.rowId,
|
|
475
|
-
title: "a",
|
|
476
|
-
detail: { city: "Austin" },
|
|
477
|
-
rank: 5,
|
|
478
|
-
priority: 2,
|
|
479
|
-
};
|
|
480
|
-
expect(await EmptyTable.save(row)).toEqual(expected);
|
|
481
|
-
expect(row).not.toHaveProperty("count");
|
|
482
|
-
expect(await EmptyTable.get(row.rowId)).toEqual(expected);
|
|
483
|
-
const stored = await db.get(`SELECT * FROM "${EmptyTable.tableName}" WHERE rowId = ?`, [
|
|
484
|
-
row.rowId,
|
|
485
|
-
]);
|
|
486
|
-
expect(stored.note).toBeNull();
|
|
487
|
-
// The caller's nested object is left as it was.
|
|
488
|
-
expect(detail).toEqual({ city: "Austin", zip: null });
|
|
489
|
-
});
|
|
490
|
-
it("clears a field saved empty, and gives an empty field with a default its default", async () => {
|
|
491
|
-
const row = { rowId: (0, utils_1.newid)(), title: "a", note: "set", rank: 7 };
|
|
492
|
-
await EmptyTable.save(row);
|
|
493
|
-
expect(await EmptyTable.get(row.rowId)).toEqual(row);
|
|
494
|
-
expect(await EmptyTable.save({ ...row, note: null, rank: null })).toEqual({
|
|
495
|
-
rowId: row.rowId,
|
|
496
|
-
title: "a",
|
|
497
|
-
rank: 5,
|
|
498
|
-
priority: 2,
|
|
499
|
-
});
|
|
500
|
-
expect(await EmptyTable.get(row.rowId)).toEqual({
|
|
501
|
-
rowId: row.rowId,
|
|
502
|
-
title: "a",
|
|
503
|
-
rank: 5,
|
|
504
|
-
priority: 2,
|
|
505
|
-
});
|
|
506
|
-
});
|
|
507
|
-
it("does the same in synchronous inserts and updates", async () => {
|
|
508
|
-
await EmptyTable.initTable();
|
|
509
|
-
const row = { rowId: (0, utils_1.newid)(), title: "a", note: "set", rank: 7 };
|
|
510
|
-
db.db.transaction(() => EmptyTable.saveSync({ ...row, note: null }))();
|
|
511
|
-
expect(await EmptyTable.get(row.rowId)).toEqual({
|
|
512
|
-
rowId: row.rowId,
|
|
513
|
-
title: "a",
|
|
514
|
-
rank: 7,
|
|
515
|
-
priority: 2,
|
|
516
|
-
});
|
|
517
|
-
db.db.transaction(() => EmptyTable.saveSync({ ...row, rank: null }))();
|
|
518
|
-
expect(await EmptyTable.get(row.rowId)).toEqual({ ...row, rank: 5, priority: 2 });
|
|
519
|
-
});
|
|
520
|
-
it.each([
|
|
521
|
-
null,
|
|
522
|
-
undefined,
|
|
523
|
-
"missing",
|
|
524
|
-
])("resets an optional default for %s in either update path", async (emptyValue) => {
|
|
525
|
-
const row = { rowId: (0, utils_1.newid)(), title: "a", rank: 7, priority: 9 };
|
|
526
|
-
await EmptyTable.save(row);
|
|
527
|
-
const cleared = {
|
|
528
|
-
rowId: row.rowId,
|
|
529
|
-
title: "a",
|
|
530
|
-
rank: 7,
|
|
531
|
-
...(emptyValue === "missing" ? {} : { priority: emptyValue }),
|
|
532
|
-
};
|
|
533
|
-
const expected = { rowId: row.rowId, title: "a", rank: 7, priority: 2 };
|
|
534
|
-
expect(await EmptyTable.update({ ...cleared })).toStrictEqual(expected);
|
|
535
|
-
expect(await EmptyTable.get(row.rowId)).toEqual(expected);
|
|
536
|
-
await EmptyTable.update(row);
|
|
537
|
-
expect(EmptyTable.updateSync({ ...cleared })).toStrictEqual(expected);
|
|
538
|
-
expect(await EmptyTable.get(row.rowId)).toEqual(expected);
|
|
539
|
-
});
|
|
540
|
-
it("still refuses an empty required field", async () => {
|
|
541
|
-
const row = { rowId: (0, utils_1.newid)(), title: null };
|
|
542
|
-
await expect(EmptyTable.save(row)).rejects.toThrow(/Expected string/);
|
|
543
|
-
expect(() => EmptyTable.saveSync(row)).toThrow(/Expected string/);
|
|
544
|
-
expect(await EmptyTable.get(row.rowId)).toBeUndefined();
|
|
545
|
-
});
|
|
546
|
-
afterAll(async () => {
|
|
547
|
-
await EmptyTable.dropTableIfExists();
|
|
548
|
-
await EmptyTable.db.close();
|
|
549
|
-
});
|
|
550
|
-
});
|
|
551
|
-
it("keeps legacy nullable enum defaults working without changing their field IR", async () => {
|
|
552
|
-
// FrameMembers.role uses this schema; legacy schemaToFields must remain unchanged.
|
|
553
|
-
const schema = zod_1.z.object({
|
|
554
|
-
rowId: zod_types_1.zodPeerId,
|
|
555
|
-
role: zod_1.z.enum(["observation", "value"]).nullable().default(null),
|
|
556
|
-
});
|
|
557
|
-
const fields = (0, types_1.schemaToFields)(schema);
|
|
558
|
-
expect(fields[1]).toEqual({
|
|
559
|
-
name: "role",
|
|
560
|
-
type: field_type_1.FieldType.any,
|
|
561
|
-
defaultValue: expect.any(Function),
|
|
562
|
-
});
|
|
563
|
-
const source = new sql_data_source_1.SQLDataSource(db, {
|
|
564
|
-
name: "legacy_empty_default",
|
|
565
|
-
description: "Legacy nullable enum default.",
|
|
566
|
-
primaryKeyName: "rowId",
|
|
567
|
-
fields,
|
|
568
|
-
});
|
|
569
|
-
const row = { rowId: (0, utils_1.newid)(), role: null };
|
|
570
|
-
expect(await source.insert(row)).toEqual(row);
|
|
571
|
-
await source.update({ rowId: row.rowId, role: "value" });
|
|
572
|
-
expect(await source.update({ rowId: row.rowId, role: null })).toEqual(row);
|
|
573
|
-
expect(source.updateSync({ rowId: row.rowId, role: null })).toEqual(row);
|
|
574
|
-
await source.dropTableIfExists();
|
|
575
|
-
});
|
|
576
|
-
});
|
|
577
|
-
describe("Tools additive catalog migration", () => {
|
|
578
|
-
test("catalog readiness retries failed SQLite schema initialization on the same source", async () => {
|
|
579
|
-
const { toolSchema } = await Promise.resolve().then(() => require("../tools"));
|
|
580
|
-
const harness = new DBHarness();
|
|
581
|
-
const source = new sql_data_source_1.SQLDataSource(harness, {
|
|
582
|
-
name: "Tools",
|
|
583
|
-
description: "",
|
|
584
|
-
primaryKeyName: "toolId",
|
|
585
|
-
fields: (0, types_1.schemaToFields)(toolSchema),
|
|
586
|
-
indexes: [{ fields: ["contractId", "contractVersion", "toolName"] }],
|
|
587
|
-
versionNumber: 2,
|
|
588
|
-
}, toolSchema);
|
|
589
|
-
const table = new subscribable_data_source_1.SubscribableDataSource(source);
|
|
590
|
-
const catalog = new tool_catalog_1.ToolCatalog({
|
|
591
|
-
dataContextId: (0, utils_1.newid)(),
|
|
592
|
-
tableContainer: { getTable: () => table },
|
|
593
|
-
}, new registry_1.ContractRegistry());
|
|
594
|
-
const exec = jest
|
|
595
|
-
.spyOn(harness, "exec")
|
|
596
|
-
.mockRejectedValueOnce(new Error("schema temporarily unavailable"));
|
|
597
|
-
try {
|
|
598
|
-
const first = catalog.ready();
|
|
599
|
-
expect(catalog.ready()).toBe(first);
|
|
600
|
-
await expect(first).rejects.toThrow("schema temporarily unavailable");
|
|
601
|
-
expect(exec).toHaveBeenCalledTimes(1);
|
|
602
|
-
await catalog.ready();
|
|
603
|
-
const rows = await table.list();
|
|
604
|
-
expect(rows.length).toBeGreaterThan(0);
|
|
605
|
-
expect(rows.every((row) => row.catalogState === "resolved")).toBe(true);
|
|
606
|
-
expect(await harness.all('PRAGMA index_list("Tools")')).toEqual(expect.arrayContaining([
|
|
607
|
-
expect.objectContaining({
|
|
608
|
-
name: expect.stringContaining("_contractId_contractVersion_toolName_"),
|
|
609
|
-
}),
|
|
610
|
-
]));
|
|
611
|
-
expect(catalog.registry.getProviderPackageId(rows[0].contractId, rows[0].contractVersion)).toBeDefined();
|
|
612
|
-
}
|
|
613
|
-
finally {
|
|
614
|
-
await catalog.dispose();
|
|
615
|
-
await harness.close();
|
|
616
|
-
}
|
|
617
|
-
});
|
|
618
|
-
test("an older failed initialization cannot evict a newer forced initialization", async () => {
|
|
619
|
-
const { toolSchema } = await Promise.resolve().then(() => require("../tools"));
|
|
620
|
-
const harness = new DBHarness();
|
|
621
|
-
const source = new sql_data_source_1.SQLDataSource(harness, {
|
|
622
|
-
name: "Tools",
|
|
623
|
-
description: "",
|
|
624
|
-
primaryKeyName: "toolId",
|
|
625
|
-
fields: (0, types_1.schemaToFields)(toolSchema),
|
|
626
|
-
}, toolSchema);
|
|
627
|
-
let reject;
|
|
628
|
-
const exec = jest.spyOn(harness, "exec").mockImplementationOnce(() => new Promise((_, fail) => {
|
|
629
|
-
reject = fail;
|
|
630
|
-
}));
|
|
631
|
-
try {
|
|
632
|
-
const first = source.initTable();
|
|
633
|
-
const failed = expect(first).rejects.toThrow("older failure");
|
|
634
|
-
await source.initTable(true);
|
|
635
|
-
const attempts = exec.mock.calls.length;
|
|
636
|
-
reject(new Error("older failure"));
|
|
637
|
-
await failed;
|
|
638
|
-
await source.initTable();
|
|
639
|
-
expect(exec).toHaveBeenCalledTimes(attempts);
|
|
640
|
-
}
|
|
641
|
-
finally {
|
|
642
|
-
await harness.close();
|
|
643
|
-
}
|
|
644
|
-
});
|
|
645
|
-
test("preserves legacy values and recreates all indexes after table-copy migration", async () => {
|
|
646
|
-
const { toolSchema } = await Promise.resolve().then(() => require("../tools"));
|
|
647
|
-
const harness = new DBHarness();
|
|
648
|
-
const oldSchema = toolSchema.omit({
|
|
649
|
-
contractId: true,
|
|
650
|
-
contractVersion: true,
|
|
651
|
-
toolName: true,
|
|
652
|
-
catalogIdentity: true,
|
|
653
|
-
catalogState: true,
|
|
654
|
-
catalogReason: true,
|
|
655
|
-
});
|
|
656
|
-
const metadata = {
|
|
657
|
-
name: "Tools",
|
|
658
|
-
description: "",
|
|
659
|
-
primaryKeyName: "toolId",
|
|
660
|
-
fields: (0, types_1.schemaToFields)(oldSchema),
|
|
661
|
-
indexes: [{ fields: ["name"] }, { fields: ["isAssistantRunner"] }],
|
|
662
|
-
versionNumber: 1,
|
|
663
|
-
};
|
|
664
|
-
const old = new sql_data_source_1.SQLDataSource(harness, metadata, oldSchema);
|
|
665
|
-
const row = oldSchema.parse({
|
|
666
|
-
toolId: (0, utils_1.newid)(),
|
|
667
|
-
name: "preserve me",
|
|
668
|
-
usageDescription: "legacy",
|
|
669
|
-
code: "executable",
|
|
670
|
-
inputSchema: { type: "none" },
|
|
671
|
-
outputSchema: { type: "none" },
|
|
672
|
-
accessLevel: 70,
|
|
673
|
-
});
|
|
674
|
-
try {
|
|
675
|
-
await old.save(row);
|
|
676
|
-
const upgraded = new sql_data_source_1.SQLDataSource(harness, {
|
|
677
|
-
...metadata,
|
|
678
|
-
fields: (0, types_1.schemaToFields)(toolSchema),
|
|
679
|
-
versionNumber: 2,
|
|
680
|
-
indexes: [...metadata.indexes, { fields: ["contractId", "contractVersion", "toolName"] }],
|
|
681
|
-
}, toolSchema);
|
|
682
|
-
await upgraded.initTable();
|
|
683
|
-
expect(await upgraded.get(row.toolId)).toMatchObject(row);
|
|
684
|
-
// Runtime backfill owns only the new columns. Updating them must work
|
|
685
|
-
// without resending (and potentially overwriting) legacy provider metadata.
|
|
686
|
-
await upgraded.update((0, tool_catalog_fields_1.toolCatalogPatch)({ toolId: row.toolId, catalogState: "unresolved" }));
|
|
687
|
-
expect(await upgraded.get(row.toolId)).toMatchObject({ ...row, catalogState: "unresolved" });
|
|
688
|
-
// A descriptive refresh patch keeps policy; an unmarked full row still replaces it.
|
|
689
|
-
await upgraded.update((0, tool_catalog_fields_1.toolCatalogPatch)({
|
|
690
|
-
toolId: row.toolId,
|
|
691
|
-
usageDescription: "refreshed",
|
|
692
|
-
detailedDescription: "new",
|
|
693
|
-
}));
|
|
694
|
-
expect(await upgraded.get(row.toolId)).toMatchObject({
|
|
695
|
-
...row,
|
|
696
|
-
usageDescription: "refreshed",
|
|
697
|
-
detailedDescription: "new",
|
|
698
|
-
accessLevel: 70,
|
|
699
|
-
});
|
|
700
|
-
const { accessLevel: _policy, ...withoutPolicy } = row;
|
|
701
|
-
await upgraded.update({ ...withoutPolicy, catalogState: "unresolved" });
|
|
702
|
-
expect((await upgraded.get(row.toolId))?.accessLevel).toBeUndefined();
|
|
703
|
-
await upgraded.update({ ...row, catalogState: "unresolved" });
|
|
704
|
-
expect(() => (0, tool_catalog_fields_1.toolCatalogPatch)({ toolId: row.toolId, accessLevel: 0 })).toThrow(/accessLevel/);
|
|
705
|
-
for (const field of ["code", "codeRepository", "packageId"])
|
|
706
|
-
expect(() => (0, tool_catalog_fields_1.toolCatalogPatch)({ toolId: row.toolId, [field]: "historical" })).toThrow(field);
|
|
707
|
-
const indexes = await harness.all('PRAGMA index_list("Tools")');
|
|
708
|
-
expect(indexes
|
|
709
|
-
.filter((index) => !index.name.startsWith("sqlite_autoindex_"))
|
|
710
|
-
.map((index) => index.name)).toEqual(expect.arrayContaining([
|
|
711
|
-
expect.stringContaining("_name_"),
|
|
712
|
-
expect.stringContaining("_isAssistantRunner_"),
|
|
713
|
-
expect.stringContaining("_contractId_contractVersion_toolName_"),
|
|
714
|
-
]));
|
|
715
|
-
expect(indexes
|
|
716
|
-
.filter((index) => index.name.includes("contractId"))
|
|
717
|
-
.every((index) => index.unique === 0)).toBe(true);
|
|
718
|
-
const target = {
|
|
719
|
-
contractId: "00muxgugn6xvs8f2r80ru205t",
|
|
720
|
-
contractVersion: 1,
|
|
721
|
-
toolName: "invoke",
|
|
722
|
-
};
|
|
723
|
-
await upgraded.save({ ...row, ...target, toolId: "00muxoa8xeet2lpi8bdq55q03" });
|
|
724
|
-
await upgraded.save({ ...row, ...target, toolId: "00muxob3hbo3f6g49mipy1hyt" });
|
|
725
|
-
expect(await upgraded.count()).toBe(3);
|
|
726
|
-
}
|
|
727
|
-
finally {
|
|
728
|
-
await harness.close();
|
|
729
|
-
}
|
|
730
|
-
});
|
|
731
|
-
});
|