@rebasepro/server-core 0.0.1-canary.000dc36
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/LICENSE +6 -0
- package/README.md +40 -0
- package/build-errors.txt +52 -0
- package/coverage/clover.xml +3739 -0
- package/coverage/coverage-final.json +31 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +266 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +210 -0
- package/coverage/lcov-report/src/api/ast-schema-editor.ts.html +952 -0
- package/coverage/lcov-report/src/api/errors.ts.html +472 -0
- package/coverage/lcov-report/src/api/graphql/graphql-schema-generator.ts.html +1069 -0
- package/coverage/lcov-report/src/api/graphql/index.html +116 -0
- package/coverage/lcov-report/src/api/index.html +176 -0
- package/coverage/lcov-report/src/api/openapi-generator.ts.html +565 -0
- package/coverage/lcov-report/src/api/rest/api-generator.ts.html +994 -0
- package/coverage/lcov-report/src/api/rest/index.html +131 -0
- package/coverage/lcov-report/src/api/rest/query-parser.ts.html +550 -0
- package/coverage/lcov-report/src/api/schema-editor-routes.ts.html +202 -0
- package/coverage/lcov-report/src/api/server.ts.html +823 -0
- package/coverage/lcov-report/src/auth/admin-routes.ts.html +973 -0
- package/coverage/lcov-report/src/auth/index.html +176 -0
- package/coverage/lcov-report/src/auth/jwt.ts.html +574 -0
- package/coverage/lcov-report/src/auth/middleware.ts.html +745 -0
- package/coverage/lcov-report/src/auth/password.ts.html +310 -0
- package/coverage/lcov-report/src/auth/services.ts.html +2074 -0
- package/coverage/lcov-report/src/collections/index.html +116 -0
- package/coverage/lcov-report/src/collections/loader.ts.html +232 -0
- package/coverage/lcov-report/src/db/auth-schema.ts.html +523 -0
- package/coverage/lcov-report/src/db/data-transformer.ts.html +1753 -0
- package/coverage/lcov-report/src/db/entityService.ts.html +700 -0
- package/coverage/lcov-report/src/db/index.html +146 -0
- package/coverage/lcov-report/src/db/services/EntityFetchService.ts.html +4048 -0
- package/coverage/lcov-report/src/db/services/EntityPersistService.ts.html +883 -0
- package/coverage/lcov-report/src/db/services/RelationService.ts.html +3121 -0
- package/coverage/lcov-report/src/db/services/entity-helpers.ts.html +442 -0
- package/coverage/lcov-report/src/db/services/index.html +176 -0
- package/coverage/lcov-report/src/db/services/index.ts.html +124 -0
- package/coverage/lcov-report/src/generate-drizzle-schema-logic.ts.html +1960 -0
- package/coverage/lcov-report/src/index.html +116 -0
- package/coverage/lcov-report/src/services/driver-registry.ts.html +631 -0
- package/coverage/lcov-report/src/services/index.html +131 -0
- package/coverage/lcov-report/src/services/postgresDataDriver.ts.html +3025 -0
- package/coverage/lcov-report/src/storage/LocalStorageController.ts.html +1189 -0
- package/coverage/lcov-report/src/storage/S3StorageController.ts.html +970 -0
- package/coverage/lcov-report/src/storage/index.html +161 -0
- package/coverage/lcov-report/src/storage/storage-registry.ts.html +646 -0
- package/coverage/lcov-report/src/storage/types.ts.html +451 -0
- package/coverage/lcov-report/src/utils/drizzle-conditions.ts.html +3082 -0
- package/coverage/lcov-report/src/utils/index.html +116 -0
- package/coverage/lcov.info +7179 -0
- package/dist/common/src/collections/CollectionRegistry.d.ts +56 -0
- package/dist/common/src/collections/index.d.ts +1 -0
- package/dist/common/src/data/buildRebaseData.d.ts +14 -0
- package/dist/common/src/index.d.ts +3 -0
- package/dist/common/src/util/builders.d.ts +57 -0
- package/dist/common/src/util/callbacks.d.ts +6 -0
- package/dist/common/src/util/collections.d.ts +11 -0
- package/dist/common/src/util/common.d.ts +2 -0
- package/dist/common/src/util/conditions.d.ts +26 -0
- package/dist/common/src/util/entities.d.ts +58 -0
- package/dist/common/src/util/enums.d.ts +3 -0
- package/dist/common/src/util/index.d.ts +16 -0
- package/dist/common/src/util/navigation_from_path.d.ts +34 -0
- package/dist/common/src/util/navigation_utils.d.ts +20 -0
- package/dist/common/src/util/parent_references_from_path.d.ts +6 -0
- package/dist/common/src/util/paths.d.ts +14 -0
- package/dist/common/src/util/permissions.d.ts +5 -0
- package/dist/common/src/util/references.d.ts +2 -0
- package/dist/common/src/util/relations.d.ts +22 -0
- package/dist/common/src/util/resolutions.d.ts +72 -0
- package/dist/common/src/util/storage.d.ts +24 -0
- package/dist/index-DXVBFp5V.js +37 -0
- package/dist/index-DXVBFp5V.js.map +1 -0
- package/dist/index.es.js +49249 -0
- package/dist/index.es.js.map +1 -0
- package/dist/index.umd.js +49283 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/server-core/src/api/ast-schema-editor.d.ts +21 -0
- package/dist/server-core/src/api/collections_for_test/callbacks_test_collection.d.ts +2 -0
- package/dist/server-core/src/api/errors.d.ts +35 -0
- package/dist/server-core/src/api/graphql/graphql-schema-generator.d.ts +35 -0
- package/dist/server-core/src/api/graphql/index.d.ts +1 -0
- package/dist/server-core/src/api/index.d.ts +9 -0
- package/dist/server-core/src/api/openapi-generator.d.ts +16 -0
- package/dist/server-core/src/api/rest/api-generator.d.ts +76 -0
- package/dist/server-core/src/api/rest/index.d.ts +1 -0
- package/dist/server-core/src/api/rest/query-parser.d.ts +9 -0
- package/dist/server-core/src/api/schema-editor-routes.d.ts +3 -0
- package/dist/server-core/src/api/server.d.ts +40 -0
- package/dist/server-core/src/api/types.d.ts +90 -0
- package/dist/server-core/src/auth/admin-routes.d.ts +21 -0
- package/dist/server-core/src/auth/apple-oauth.d.ts +30 -0
- package/dist/server-core/src/auth/bitbucket-oauth.d.ts +11 -0
- package/dist/server-core/src/auth/discord-oauth.d.ts +14 -0
- package/dist/server-core/src/auth/facebook-oauth.d.ts +14 -0
- package/dist/server-core/src/auth/github-oauth.d.ts +15 -0
- package/dist/server-core/src/auth/gitlab-oauth.d.ts +13 -0
- package/dist/server-core/src/auth/google-oauth.d.ts +14 -0
- package/dist/server-core/src/auth/index.d.ts +23 -0
- package/dist/server-core/src/auth/interfaces.d.ts +309 -0
- package/dist/server-core/src/auth/jwt.d.ts +43 -0
- package/dist/server-core/src/auth/linkedin-oauth.d.ts +18 -0
- package/dist/server-core/src/auth/microsoft-oauth.d.ts +16 -0
- package/dist/server-core/src/auth/middleware.d.ts +81 -0
- package/dist/server-core/src/auth/password.d.ts +22 -0
- package/dist/server-core/src/auth/rate-limiter.d.ts +31 -0
- package/dist/server-core/src/auth/routes.d.ts +27 -0
- package/dist/server-core/src/auth/slack-oauth.d.ts +12 -0
- package/dist/server-core/src/auth/spotify-oauth.d.ts +12 -0
- package/dist/server-core/src/auth/twitter-oauth.d.ts +18 -0
- package/dist/server-core/src/bootstrappers/index.d.ts +0 -0
- package/dist/server-core/src/collections/BackendCollectionRegistry.d.ts +13 -0
- package/dist/server-core/src/collections/loader.d.ts +5 -0
- package/dist/server-core/src/cron/cron-loader.d.ts +17 -0
- package/dist/server-core/src/cron/cron-routes.d.ts +14 -0
- package/dist/server-core/src/cron/cron-scheduler.d.ts +106 -0
- package/dist/server-core/src/cron/cron-store.d.ts +32 -0
- package/dist/server-core/src/cron/index.d.ts +6 -0
- package/dist/server-core/src/db/interfaces.d.ts +18 -0
- package/dist/server-core/src/email/index.d.ts +6 -0
- package/dist/server-core/src/email/smtp-email-service.d.ts +25 -0
- package/dist/server-core/src/email/templates.d.ts +42 -0
- package/dist/server-core/src/email/types.d.ts +107 -0
- package/dist/server-core/src/functions/function-loader.d.ts +17 -0
- package/dist/server-core/src/functions/function-routes.d.ts +10 -0
- package/dist/server-core/src/functions/index.d.ts +3 -0
- package/dist/server-core/src/history/history-routes.d.ts +23 -0
- package/dist/server-core/src/history/index.d.ts +1 -0
- package/dist/server-core/src/index.d.ts +29 -0
- package/dist/server-core/src/init.d.ts +168 -0
- package/dist/server-core/src/serve-spa.d.ts +30 -0
- package/dist/server-core/src/services/driver-registry.d.ts +78 -0
- package/dist/server-core/src/singleton.d.ts +35 -0
- package/dist/server-core/src/storage/LocalStorageController.d.ts +46 -0
- package/dist/server-core/src/storage/S3StorageController.d.ts +36 -0
- package/dist/server-core/src/storage/index.d.ts +25 -0
- package/dist/server-core/src/storage/routes.d.ts +38 -0
- package/dist/server-core/src/storage/storage-registry.d.ts +78 -0
- package/dist/server-core/src/storage/types.d.ts +103 -0
- package/dist/server-core/src/types/index.d.ts +11 -0
- package/dist/server-core/src/utils/dev-port.d.ts +35 -0
- package/dist/server-core/src/utils/logger.d.ts +31 -0
- package/dist/server-core/src/utils/logging.d.ts +9 -0
- package/dist/server-core/src/utils/request-logger.d.ts +19 -0
- package/dist/server-core/src/utils/sql.d.ts +27 -0
- package/dist/types/src/controllers/analytics_controller.d.ts +7 -0
- package/dist/types/src/controllers/auth.d.ts +119 -0
- package/dist/types/src/controllers/client.d.ts +170 -0
- package/dist/types/src/controllers/collection_registry.d.ts +46 -0
- package/dist/types/src/controllers/customization_controller.d.ts +60 -0
- package/dist/types/src/controllers/data.d.ts +168 -0
- package/dist/types/src/controllers/data_driver.d.ts +195 -0
- package/dist/types/src/controllers/database_admin.d.ts +11 -0
- package/dist/types/src/controllers/dialogs_controller.d.ts +36 -0
- package/dist/types/src/controllers/effective_role.d.ts +4 -0
- package/dist/types/src/controllers/email.d.ts +34 -0
- package/dist/types/src/controllers/index.d.ts +18 -0
- package/dist/types/src/controllers/local_config_persistence.d.ts +20 -0
- package/dist/types/src/controllers/navigation.d.ts +213 -0
- package/dist/types/src/controllers/registry.d.ts +54 -0
- package/dist/types/src/controllers/side_dialogs_controller.d.ts +67 -0
- package/dist/types/src/controllers/side_entity_controller.d.ts +90 -0
- package/dist/types/src/controllers/snackbar.d.ts +24 -0
- package/dist/types/src/controllers/storage.d.ts +171 -0
- package/dist/types/src/index.d.ts +4 -0
- package/dist/types/src/rebase_context.d.ts +105 -0
- package/dist/types/src/types/backend.d.ts +536 -0
- package/dist/types/src/types/backend_hooks.d.ts +187 -0
- package/dist/types/src/types/builders.d.ts +15 -0
- package/dist/types/src/types/chips.d.ts +5 -0
- package/dist/types/src/types/collections.d.ts +857 -0
- package/dist/types/src/types/cron.d.ts +102 -0
- package/dist/types/src/types/data_source.d.ts +64 -0
- package/dist/types/src/types/entities.d.ts +145 -0
- package/dist/types/src/types/entity_actions.d.ts +98 -0
- package/dist/types/src/types/entity_callbacks.d.ts +173 -0
- package/dist/types/src/types/entity_link_builder.d.ts +7 -0
- package/dist/types/src/types/entity_overrides.d.ts +10 -0
- package/dist/types/src/types/entity_views.d.ts +59 -0
- package/dist/types/src/types/export_import.d.ts +21 -0
- package/dist/types/src/types/formex.d.ts +40 -0
- package/dist/types/src/types/index.d.ts +25 -0
- package/dist/types/src/types/locales.d.ts +4 -0
- package/dist/types/src/types/modify_collections.d.ts +5 -0
- package/dist/types/src/types/plugins.d.ts +282 -0
- package/dist/types/src/types/properties.d.ts +1148 -0
- package/dist/types/src/types/property_config.d.ts +70 -0
- package/dist/types/src/types/relations.d.ts +336 -0
- package/dist/types/src/types/slots.d.ts +262 -0
- package/dist/types/src/types/translations.d.ts +874 -0
- package/dist/types/src/types/user_management_delegate.d.ts +121 -0
- package/dist/types/src/types/websockets.d.ts +78 -0
- package/dist/types/src/users/index.d.ts +2 -0
- package/dist/types/src/users/roles.d.ts +22 -0
- package/dist/types/src/users/user.d.ts +46 -0
- package/history_diff.log +385 -0
- package/jest.config.cjs +16 -0
- package/package.json +86 -0
- package/scratch.ts +9 -0
- package/src/api/ast-schema-editor.ts +289 -0
- package/src/api/collections_for_test/callbacks_test_collection.ts +60 -0
- package/src/api/errors.ts +179 -0
- package/src/api/graphql/graphql-schema-generator.ts +336 -0
- package/src/api/graphql/index.ts +2 -0
- package/src/api/index.ts +11 -0
- package/src/api/openapi-generator.ts +715 -0
- package/src/api/rest/api-generator-count.test.ts +113 -0
- package/src/api/rest/api-generator.ts +573 -0
- package/src/api/rest/index.ts +2 -0
- package/src/api/rest/query-parser.ts +155 -0
- package/src/api/schema-editor-routes.ts +41 -0
- package/src/api/server.ts +249 -0
- package/src/api/types.ts +90 -0
- package/src/auth/admin-routes.ts +605 -0
- package/src/auth/apple-oauth.ts +120 -0
- package/src/auth/bitbucket-oauth.ts +82 -0
- package/src/auth/discord-oauth.ts +83 -0
- package/src/auth/facebook-oauth.ts +72 -0
- package/src/auth/github-oauth.ts +110 -0
- package/src/auth/gitlab-oauth.ts +70 -0
- package/src/auth/google-oauth.ts +48 -0
- package/src/auth/index.ts +34 -0
- package/src/auth/interfaces.ts +363 -0
- package/src/auth/jwt.ts +181 -0
- package/src/auth/linkedin-oauth.ts +81 -0
- package/src/auth/microsoft-oauth.ts +88 -0
- package/src/auth/middleware.ts +384 -0
- package/src/auth/password.ts +77 -0
- package/src/auth/rate-limiter.ts +133 -0
- package/src/auth/routes.ts +788 -0
- package/src/auth/slack-oauth.ts +71 -0
- package/src/auth/spotify-oauth.ts +67 -0
- package/src/auth/twitter-oauth.ts +120 -0
- package/src/bootstrappers/index.ts +1 -0
- package/src/collections/BackendCollectionRegistry.ts +20 -0
- package/src/collections/loader.ts +49 -0
- package/src/cron/cron-loader.ts +89 -0
- package/src/cron/cron-routes.test.ts +265 -0
- package/src/cron/cron-routes.ts +85 -0
- package/src/cron/cron-scheduler.test.ts +547 -0
- package/src/cron/cron-scheduler.ts +576 -0
- package/src/cron/cron-store.ts +163 -0
- package/src/cron/index.ts +6 -0
- package/src/db/interfaces.ts +60 -0
- package/src/email/index.ts +18 -0
- package/src/email/smtp-email-service.ts +91 -0
- package/src/email/templates.ts +388 -0
- package/src/email/types.ts +105 -0
- package/src/functions/function-loader.ts +119 -0
- package/src/functions/function-routes.ts +31 -0
- package/src/functions/index.ts +3 -0
- package/src/history/history-routes.ts +129 -0
- package/src/history/index.ts +2 -0
- package/src/index.ts +66 -0
- package/src/init.ts +737 -0
- package/src/serve-spa.ts +81 -0
- package/src/services/driver-registry.ts +182 -0
- package/src/singleton.test.ts +28 -0
- package/src/singleton.ts +70 -0
- package/src/storage/LocalStorageController.ts +365 -0
- package/src/storage/S3StorageController.ts +298 -0
- package/src/storage/index.ts +43 -0
- package/src/storage/routes.ts +264 -0
- package/src/storage/storage-registry.ts +187 -0
- package/src/storage/types.ts +134 -0
- package/src/types/index.ts +27 -0
- package/src/utils/dev-port.ts +176 -0
- package/src/utils/logger.ts +143 -0
- package/src/utils/logging.ts +38 -0
- package/src/utils/request-logger.ts +66 -0
- package/src/utils/sql.ts +38 -0
- package/test/admin-routes.test.ts +640 -0
- package/test/api-generator.test.ts +501 -0
- package/test/ast-schema-editor.test.ts +63 -0
- package/test/auth-middleware-hono.test.ts +556 -0
- package/test/auth-routes.test.ts +1047 -0
- package/test/backend-hooks-admin.test.ts +394 -0
- package/test/backend-hooks-data.test.ts +408 -0
- package/test/driver-registry.test.ts +282 -0
- package/test/error-propagation.test.ts +226 -0
- package/test/errors-hono.test.ts +133 -0
- package/test/errors.test.ts +155 -0
- package/test/jwt-security.test.ts +182 -0
- package/test/jwt.test.ts +324 -0
- package/test/middleware.test.ts +300 -0
- package/test/password.test.ts +165 -0
- package/test/query-parser.test.ts +263 -0
- package/test/rate-limiter.test.ts +102 -0
- package/test/safe-compare.test.ts +66 -0
- package/test/singleton.test.ts +59 -0
- package/test/storage-local.test.ts +271 -0
- package/test/storage-registry.test.ts +282 -0
- package/test/storage-routes.test.ts +222 -0
- package/test/storage-s3.test.ts +304 -0
- package/test-ast.ts +28 -0
- package/test.ts +6 -0
- package/test_output.txt +1133 -0
- package/tsconfig.json +49 -0
- package/tsconfig.prod.json +20 -0
- package/vite.config.ts +80 -0
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BackendHooks — Data (REST API) Integration Tests
|
|
3
|
+
*
|
|
4
|
+
* Verifies that DataHooks are correctly applied within RestApiGenerator
|
|
5
|
+
* for all collection CRUD operations.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { Hono } from "hono";
|
|
9
|
+
import { RestApiGenerator } from "../src/api/rest/api-generator";
|
|
10
|
+
import { errorHandler } from "../src/api/errors";
|
|
11
|
+
import type { DataDriver } from "@rebasepro/types";
|
|
12
|
+
import type { EntityCollection } from "@rebasepro/types";
|
|
13
|
+
import type { DataHooks } from "@rebasepro/types";
|
|
14
|
+
|
|
15
|
+
// ── Helpers ─────────────────────────────────────────────────────────────────
|
|
16
|
+
|
|
17
|
+
function createMockDriver(): jest.Mocked<DataDriver> {
|
|
18
|
+
return {
|
|
19
|
+
key: "postgres",
|
|
20
|
+
initialised: true,
|
|
21
|
+
fetchCollection: jest.fn(),
|
|
22
|
+
listenCollection: jest.fn(),
|
|
23
|
+
fetchEntity: jest.fn(),
|
|
24
|
+
listenEntity: jest.fn(),
|
|
25
|
+
saveEntity: jest.fn(),
|
|
26
|
+
deleteEntity: jest.fn(),
|
|
27
|
+
checkUniqueField: jest.fn(),
|
|
28
|
+
countEntities: jest.fn(),
|
|
29
|
+
withAuth: jest.fn(),
|
|
30
|
+
admin: {} as any
|
|
31
|
+
} as unknown as jest.Mocked<DataDriver>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const mockCollections: EntityCollection[] = [
|
|
35
|
+
{ slug: "products", name: "Products", singularName: "Product", properties: {} } as any,
|
|
36
|
+
{ slug: "orders", name: "Orders", singularName: "Order", properties: {} } as any
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
function createApp(mockDriver: jest.Mocked<DataDriver>, hooks?: DataHooks) {
|
|
40
|
+
const app = new Hono();
|
|
41
|
+
app.onError(errorHandler);
|
|
42
|
+
const generator = new RestApiGenerator(mockCollections, mockDriver, hooks);
|
|
43
|
+
app.route("/api", generator.generateRoutes());
|
|
44
|
+
return app;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// ═════════════════════════════════════════════════════════════════════════════
|
|
48
|
+
// TESTS
|
|
49
|
+
// ═════════════════════════════════════════════════════════════════════════════
|
|
50
|
+
|
|
51
|
+
describe("DataHooks — REST API", () => {
|
|
52
|
+
let mockDriver: jest.Mocked<DataDriver>;
|
|
53
|
+
|
|
54
|
+
beforeEach(() => {
|
|
55
|
+
mockDriver = createMockDriver();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// ── afterRead ─────────────────────────────────────────────────────
|
|
59
|
+
describe("data.afterRead", () => {
|
|
60
|
+
it("transforms entities in GET list response", async () => {
|
|
61
|
+
const hooks: DataHooks = {
|
|
62
|
+
afterRead(slug, entity) {
|
|
63
|
+
// Mask price for non-premium entities
|
|
64
|
+
if (slug === "products") {
|
|
65
|
+
return { ...entity, price: "***" };
|
|
66
|
+
}
|
|
67
|
+
return entity;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
const app = createApp(mockDriver, hooks);
|
|
71
|
+
|
|
72
|
+
mockDriver.fetchCollection.mockResolvedValue([
|
|
73
|
+
{ id: "p1", path: "products", values: { name: "Widget", price: 99 } } as any,
|
|
74
|
+
{ id: "p2", path: "products", values: { name: "Gadget", price: 199 } } as any
|
|
75
|
+
]);
|
|
76
|
+
mockDriver.countEntities!.mockResolvedValue(2);
|
|
77
|
+
|
|
78
|
+
const res = await app.request("/api/products");
|
|
79
|
+
expect(res.status).toBe(200);
|
|
80
|
+
|
|
81
|
+
const body = await res.json() as any;
|
|
82
|
+
expect(body.data).toHaveLength(2);
|
|
83
|
+
expect(body.data[0].price).toBe("***");
|
|
84
|
+
expect(body.data[1].price).toBe("***");
|
|
85
|
+
// Original field should still be there
|
|
86
|
+
expect(body.data[0].name).toBe("Widget");
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("filters out entities by returning null", async () => {
|
|
90
|
+
const hooks: DataHooks = {
|
|
91
|
+
afterRead(slug, entity) {
|
|
92
|
+
// Hide draft products
|
|
93
|
+
if (entity.status === "draft") return null;
|
|
94
|
+
return entity;
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
const app = createApp(mockDriver, hooks);
|
|
98
|
+
|
|
99
|
+
mockDriver.fetchCollection.mockResolvedValue([
|
|
100
|
+
{ id: "p1", path: "products", values: { name: "Published", status: "active" } } as any,
|
|
101
|
+
{ id: "p2", path: "products", values: { name: "Draft", status: "draft" } } as any,
|
|
102
|
+
{ id: "p3", path: "products", values: { name: "Also Published", status: "active" } } as any
|
|
103
|
+
]);
|
|
104
|
+
mockDriver.countEntities!.mockResolvedValue(3);
|
|
105
|
+
|
|
106
|
+
const res = await app.request("/api/products");
|
|
107
|
+
const body = await res.json() as any;
|
|
108
|
+
expect(body.data).toHaveLength(2);
|
|
109
|
+
expect(body.data.map((d: any) => d.name)).toEqual(["Published", "Also Published"]);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("transforms single entity GET response", async () => {
|
|
113
|
+
const hooks: DataHooks = {
|
|
114
|
+
afterRead(slug, entity) {
|
|
115
|
+
return { ...entity, _readAt: "2024-01-01" };
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
const app = createApp(mockDriver, hooks);
|
|
119
|
+
|
|
120
|
+
mockDriver.fetchEntity.mockResolvedValue(
|
|
121
|
+
{ id: "p1", path: "products", values: { name: "Widget" } } as any
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
const res = await app.request("/api/products/p1");
|
|
125
|
+
expect(res.status).toBe(200);
|
|
126
|
+
|
|
127
|
+
const body = await res.json() as any;
|
|
128
|
+
expect(body.name).toBe("Widget");
|
|
129
|
+
expect(body._readAt).toBe("2024-01-01");
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("returns 404 when afterRead filters a single entity", async () => {
|
|
133
|
+
const hooks: DataHooks = {
|
|
134
|
+
afterRead(slug, entity) {
|
|
135
|
+
if (entity.id === "hidden") return null;
|
|
136
|
+
return entity;
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
const app = createApp(mockDriver, hooks);
|
|
140
|
+
|
|
141
|
+
mockDriver.fetchEntity.mockResolvedValue(
|
|
142
|
+
{ id: "hidden", path: "products", values: { name: "Secret" } } as any
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
const res = await app.request("/api/products/hidden");
|
|
146
|
+
expect(res.status).toBe(404);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("only affects targeted collection slug", async () => {
|
|
150
|
+
const hooks: DataHooks = {
|
|
151
|
+
afterRead(slug, entity) {
|
|
152
|
+
if (slug === "products") {
|
|
153
|
+
return { ...entity, hooked: true };
|
|
154
|
+
}
|
|
155
|
+
return entity;
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
const app = createApp(mockDriver, hooks);
|
|
159
|
+
|
|
160
|
+
// Products should be hooked
|
|
161
|
+
mockDriver.fetchEntity.mockResolvedValueOnce(
|
|
162
|
+
{ id: "p1", path: "products", values: { name: "Widget" } } as any
|
|
163
|
+
);
|
|
164
|
+
const prodRes = await app.request("/api/products/p1");
|
|
165
|
+
const prodBody = await prodRes.json() as any;
|
|
166
|
+
expect(prodBody.hooked).toBe(true);
|
|
167
|
+
|
|
168
|
+
// Orders should NOT be hooked
|
|
169
|
+
mockDriver.fetchEntity.mockResolvedValueOnce(
|
|
170
|
+
{ id: "o1", path: "orders", values: { total: 42 } } as any
|
|
171
|
+
);
|
|
172
|
+
const orderRes = await app.request("/api/orders/o1");
|
|
173
|
+
const orderBody = await orderRes.json() as any;
|
|
174
|
+
expect(orderBody.hooked).toBeUndefined();
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
// ── beforeSave ────────────────────────────────────────────────────
|
|
179
|
+
describe("data.beforeSave", () => {
|
|
180
|
+
it("transforms values before POST (create)", async () => {
|
|
181
|
+
const hooks: DataHooks = {
|
|
182
|
+
beforeSave(slug, values, entityId) {
|
|
183
|
+
return { ...values, slug: values.name?.toString().toLowerCase().replace(/\s+/g, "-") };
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
const app = createApp(mockDriver, hooks);
|
|
187
|
+
|
|
188
|
+
mockDriver.saveEntity.mockResolvedValue(
|
|
189
|
+
{ id: "new-1", path: "products", values: { name: "Cool Widget", slug: "cool-widget" } } as any
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
const res = await app.request("/api/products", {
|
|
193
|
+
method: "POST",
|
|
194
|
+
headers: { "Content-Type": "application/json" },
|
|
195
|
+
body: JSON.stringify({ name: "Cool Widget" })
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
expect(res.status).toBe(201);
|
|
199
|
+
expect(mockDriver.saveEntity).toHaveBeenCalledWith(
|
|
200
|
+
expect.objectContaining({
|
|
201
|
+
values: expect.objectContaining({ slug: "cool-widget" })
|
|
202
|
+
})
|
|
203
|
+
);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("transforms values before PUT (update)", async () => {
|
|
207
|
+
const hooks: DataHooks = {
|
|
208
|
+
beforeSave(slug, values, entityId) {
|
|
209
|
+
// Add an updatedBy field
|
|
210
|
+
return { ...values, updatedBy: "hook" };
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
const app = createApp(mockDriver, hooks);
|
|
214
|
+
|
|
215
|
+
mockDriver.fetchEntity.mockResolvedValue({ id: "p1", path: "products", values: {} } as any);
|
|
216
|
+
mockDriver.saveEntity.mockResolvedValue(
|
|
217
|
+
{ id: "p1", path: "products", values: { name: "Updated", updatedBy: "hook" } } as any
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
const res = await app.request("/api/products/p1", {
|
|
221
|
+
method: "PUT",
|
|
222
|
+
headers: { "Content-Type": "application/json" },
|
|
223
|
+
body: JSON.stringify({ name: "Updated" })
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
expect(res.status).toBe(200);
|
|
227
|
+
expect(mockDriver.saveEntity).toHaveBeenCalledWith(
|
|
228
|
+
expect.objectContaining({
|
|
229
|
+
values: expect.objectContaining({ updatedBy: "hook" })
|
|
230
|
+
})
|
|
231
|
+
);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it("receives entityId=undefined on POST, actual id on PUT", async () => {
|
|
235
|
+
const beforeSaveSpy = jest.fn((slug, values, entityId) => values);
|
|
236
|
+
const hooks: DataHooks = { beforeSave: beforeSaveSpy };
|
|
237
|
+
const app = createApp(mockDriver, hooks);
|
|
238
|
+
|
|
239
|
+
// POST
|
|
240
|
+
mockDriver.saveEntity.mockResolvedValueOnce(
|
|
241
|
+
{ id: "new-1", path: "products", values: { name: "A" } } as any
|
|
242
|
+
);
|
|
243
|
+
await app.request("/api/products", {
|
|
244
|
+
method: "POST",
|
|
245
|
+
headers: { "Content-Type": "application/json" },
|
|
246
|
+
body: JSON.stringify({ name: "A" })
|
|
247
|
+
});
|
|
248
|
+
expect(beforeSaveSpy.mock.calls[0][2]).toBeUndefined(); // entityId
|
|
249
|
+
|
|
250
|
+
// PUT
|
|
251
|
+
mockDriver.fetchEntity.mockResolvedValueOnce({ id: "p1", path: "products", values: {} } as any);
|
|
252
|
+
mockDriver.saveEntity.mockResolvedValueOnce(
|
|
253
|
+
{ id: "p1", path: "products", values: { name: "B" } } as any
|
|
254
|
+
);
|
|
255
|
+
await app.request("/api/products/p1", {
|
|
256
|
+
method: "PUT",
|
|
257
|
+
headers: { "Content-Type": "application/json" },
|
|
258
|
+
body: JSON.stringify({ name: "B" })
|
|
259
|
+
});
|
|
260
|
+
expect(beforeSaveSpy.mock.calls[1][2]).toBe("p1"); // entityId
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it("aborts save when beforeSave throws", async () => {
|
|
264
|
+
const hooks: DataHooks = {
|
|
265
|
+
beforeSave(slug, values) {
|
|
266
|
+
if (!values.name) throw new Error("Name is required");
|
|
267
|
+
return values;
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
const app = createApp(mockDriver, hooks);
|
|
271
|
+
|
|
272
|
+
const res = await app.request("/api/products", {
|
|
273
|
+
method: "POST",
|
|
274
|
+
headers: { "Content-Type": "application/json" },
|
|
275
|
+
body: JSON.stringify({ price: 99 })
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
// Should get an error status
|
|
279
|
+
expect(res.status).toBeGreaterThanOrEqual(400);
|
|
280
|
+
expect(mockDriver.saveEntity).not.toHaveBeenCalled();
|
|
281
|
+
});
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
// ── afterSave ─────────────────────────────────────────────────────
|
|
285
|
+
describe("data.afterSave", () => {
|
|
286
|
+
it("fires afterSave after POST", async () => {
|
|
287
|
+
const afterSaveSpy = jest.fn();
|
|
288
|
+
const hooks: DataHooks = { afterSave: afterSaveSpy };
|
|
289
|
+
const app = createApp(mockDriver, hooks);
|
|
290
|
+
|
|
291
|
+
mockDriver.saveEntity.mockResolvedValue(
|
|
292
|
+
{ id: "new-1", path: "products", values: { name: "Widget" } } as any
|
|
293
|
+
);
|
|
294
|
+
|
|
295
|
+
const res = await app.request("/api/products", {
|
|
296
|
+
method: "POST",
|
|
297
|
+
headers: { "Content-Type": "application/json" },
|
|
298
|
+
body: JSON.stringify({ name: "Widget" })
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
expect(res.status).toBe(201);
|
|
302
|
+
await new Promise(r => setTimeout(r, 50));
|
|
303
|
+
expect(afterSaveSpy).toHaveBeenCalledTimes(1);
|
|
304
|
+
expect(afterSaveSpy).toHaveBeenCalledWith(
|
|
305
|
+
"products",
|
|
306
|
+
expect.objectContaining({ id: "new-1", name: "Widget" }),
|
|
307
|
+
expect.objectContaining({ method: "POST" })
|
|
308
|
+
);
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it("fires afterSave after PUT", async () => {
|
|
312
|
+
const afterSaveSpy = jest.fn();
|
|
313
|
+
const hooks: DataHooks = { afterSave: afterSaveSpy };
|
|
314
|
+
const app = createApp(mockDriver, hooks);
|
|
315
|
+
|
|
316
|
+
mockDriver.fetchEntity.mockResolvedValue({ id: "p1", path: "products", values: {} } as any);
|
|
317
|
+
mockDriver.saveEntity.mockResolvedValue(
|
|
318
|
+
{ id: "p1", path: "products", values: { name: "Updated" } } as any
|
|
319
|
+
);
|
|
320
|
+
|
|
321
|
+
await app.request("/api/products/p1", {
|
|
322
|
+
method: "PUT",
|
|
323
|
+
headers: { "Content-Type": "application/json" },
|
|
324
|
+
body: JSON.stringify({ name: "Updated" })
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
await new Promise(r => setTimeout(r, 50));
|
|
328
|
+
expect(afterSaveSpy).toHaveBeenCalledWith(
|
|
329
|
+
"products",
|
|
330
|
+
expect.objectContaining({ id: "p1" }),
|
|
331
|
+
expect.objectContaining({ method: "PUT" })
|
|
332
|
+
);
|
|
333
|
+
});
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
// ── beforeDelete / afterDelete ───────────────────────────────────
|
|
337
|
+
describe("data.beforeDelete / afterDelete", () => {
|
|
338
|
+
it("aborts deletion when beforeDelete throws", async () => {
|
|
339
|
+
const hooks: DataHooks = {
|
|
340
|
+
beforeDelete(slug, entityId) {
|
|
341
|
+
if (entityId === "protected") {
|
|
342
|
+
throw new Error("Cannot delete protected entity");
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
const app = createApp(mockDriver, hooks);
|
|
347
|
+
|
|
348
|
+
mockDriver.fetchEntity.mockResolvedValue(
|
|
349
|
+
{ id: "protected", path: "products", values: {} } as any
|
|
350
|
+
);
|
|
351
|
+
|
|
352
|
+
const res = await app.request("/api/products/protected", { method: "DELETE" });
|
|
353
|
+
expect(res.status).toBe(500);
|
|
354
|
+
expect(mockDriver.deleteEntity).not.toHaveBeenCalled();
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
it("allows deletion when beforeDelete does not throw", async () => {
|
|
358
|
+
const beforeDeleteSpy = jest.fn();
|
|
359
|
+
const hooks: DataHooks = { beforeDelete: beforeDeleteSpy };
|
|
360
|
+
const app = createApp(mockDriver, hooks);
|
|
361
|
+
|
|
362
|
+
const existingEntity = { id: "p1", path: "products", values: {} } as any;
|
|
363
|
+
mockDriver.fetchEntity.mockResolvedValue(existingEntity);
|
|
364
|
+
mockDriver.deleteEntity.mockResolvedValue();
|
|
365
|
+
|
|
366
|
+
const res = await app.request("/api/products/p1", { method: "DELETE" });
|
|
367
|
+
expect(res.status).toBe(204);
|
|
368
|
+
expect(beforeDeleteSpy).toHaveBeenCalledWith(
|
|
369
|
+
"products", "p1", expect.objectContaining({ method: "DELETE" })
|
|
370
|
+
);
|
|
371
|
+
expect(mockDriver.deleteEntity).toHaveBeenCalled();
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
it("fires afterDelete after successful deletion", async () => {
|
|
375
|
+
const afterDeleteSpy = jest.fn();
|
|
376
|
+
const hooks: DataHooks = { afterDelete: afterDeleteSpy };
|
|
377
|
+
const app = createApp(mockDriver, hooks);
|
|
378
|
+
|
|
379
|
+
mockDriver.fetchEntity.mockResolvedValue({ id: "p1", path: "products", values: {} } as any);
|
|
380
|
+
mockDriver.deleteEntity.mockResolvedValue();
|
|
381
|
+
|
|
382
|
+
await app.request("/api/products/p1", { method: "DELETE" });
|
|
383
|
+
await new Promise(r => setTimeout(r, 50));
|
|
384
|
+
|
|
385
|
+
expect(afterDeleteSpy).toHaveBeenCalledWith(
|
|
386
|
+
"products", "p1", expect.objectContaining({ method: "DELETE" })
|
|
387
|
+
);
|
|
388
|
+
});
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
// ── no hooks (passthrough) ──────────────────────────────────────
|
|
392
|
+
describe("no hooks configured", () => {
|
|
393
|
+
it("returns data unchanged when no hooks are provided", async () => {
|
|
394
|
+
const app = createApp(mockDriver); // no hooks
|
|
395
|
+
mockDriver.fetchCollection.mockResolvedValue([
|
|
396
|
+
{ id: "p1", path: "products", values: { name: "Widget" } } as any
|
|
397
|
+
]);
|
|
398
|
+
mockDriver.countEntities!.mockResolvedValue(1);
|
|
399
|
+
|
|
400
|
+
const res = await app.request("/api/products");
|
|
401
|
+
expect(res.status).toBe(200);
|
|
402
|
+
|
|
403
|
+
const body = await res.json() as any;
|
|
404
|
+
expect(body.data).toHaveLength(1);
|
|
405
|
+
expect(body.data[0].name).toBe("Widget");
|
|
406
|
+
});
|
|
407
|
+
});
|
|
408
|
+
});
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DriverRegistry,
|
|
3
|
+
DefaultDriverRegistry,
|
|
4
|
+
DEFAULT_DRIVER_ID
|
|
5
|
+
} from "../src/services/driver-registry";
|
|
6
|
+
import { DataDriver } from "@rebasepro/types";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Mock DataDriver for testing
|
|
10
|
+
*/
|
|
11
|
+
function createMockDataDriverDelegate(key: string): DataDriver {
|
|
12
|
+
return {
|
|
13
|
+
key,
|
|
14
|
+
initialised: true,
|
|
15
|
+
fetchCollection: jest.fn().mockResolvedValue([]),
|
|
16
|
+
fetchEntity: jest.fn().mockResolvedValue(undefined),
|
|
17
|
+
saveEntity: jest.fn().mockResolvedValue({ id: "test-id",
|
|
18
|
+
path: "test",
|
|
19
|
+
values: {} }),
|
|
20
|
+
deleteEntity: jest.fn().mockResolvedValue(undefined),
|
|
21
|
+
checkUniqueField: jest.fn().mockResolvedValue(true)
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
describe("DriverRegistry", () => {
|
|
26
|
+
describe("DEFAULT_DRIVER_ID", () => {
|
|
27
|
+
it("should be '(default)'", () => {
|
|
28
|
+
expect(DEFAULT_DRIVER_ID).toBe("(default)");
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe("DefaultDriverRegistry", () => {
|
|
33
|
+
describe("constructor and basic operations", () => {
|
|
34
|
+
it("should create an empty registry", () => {
|
|
35
|
+
const registry = new DefaultDriverRegistry();
|
|
36
|
+
expect(registry.size()).toBe(0);
|
|
37
|
+
expect(registry.list()).toEqual([]);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("should register a driver", () => {
|
|
41
|
+
const registry = new DefaultDriverRegistry();
|
|
42
|
+
const mockDelegate = createMockDataDriverDelegate("postgres");
|
|
43
|
+
|
|
44
|
+
registry.register("test-db", mockDelegate);
|
|
45
|
+
|
|
46
|
+
expect(registry.has("test-db")).toBe(true);
|
|
47
|
+
expect(registry.size()).toBe(1);
|
|
48
|
+
expect(registry.list()).toContain("test-db");
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("should get a registered driver", () => {
|
|
52
|
+
const registry = new DefaultDriverRegistry();
|
|
53
|
+
const mockDelegate = createMockDataDriverDelegate("postgres");
|
|
54
|
+
|
|
55
|
+
registry.register("my-db", mockDelegate);
|
|
56
|
+
|
|
57
|
+
const retrieved = registry.get("my-db");
|
|
58
|
+
expect(retrieved).toBe(mockDelegate);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("should return undefined for non-existent driver", () => {
|
|
62
|
+
const registry = new DefaultDriverRegistry();
|
|
63
|
+
expect(registry.get("non-existent")).toBeUndefined();
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
describe("default driver handling", () => {
|
|
68
|
+
it("should get default driver with get(undefined)", () => {
|
|
69
|
+
const registry = new DefaultDriverRegistry();
|
|
70
|
+
const mockDelegate = createMockDataDriverDelegate("postgres");
|
|
71
|
+
|
|
72
|
+
registry.register(DEFAULT_DRIVER_ID, mockDelegate);
|
|
73
|
+
|
|
74
|
+
expect(registry.get(undefined)).toBe(mockDelegate);
|
|
75
|
+
expect(registry.get(null)).toBe(mockDelegate);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("should get default driver with getDefault()", () => {
|
|
79
|
+
const registry = new DefaultDriverRegistry();
|
|
80
|
+
const mockDelegate = createMockDataDriverDelegate("postgres");
|
|
81
|
+
|
|
82
|
+
registry.register(DEFAULT_DRIVER_ID, mockDelegate);
|
|
83
|
+
|
|
84
|
+
expect(registry.getDefault()).toBe(mockDelegate);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("should throw error when no default driver exists", () => {
|
|
88
|
+
const registry = new DefaultDriverRegistry();
|
|
89
|
+
|
|
90
|
+
expect(() => registry.getDefault()).toThrow(
|
|
91
|
+
"[DriverRegistry] No default driver registered."
|
|
92
|
+
);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
describe("getOrDefault", () => {
|
|
97
|
+
let registry: DefaultDriverRegistry;
|
|
98
|
+
let defaultDelegate: DataDriver;
|
|
99
|
+
let analyticsDelegate: DataDriver;
|
|
100
|
+
|
|
101
|
+
beforeEach(() => {
|
|
102
|
+
registry = new DefaultDriverRegistry();
|
|
103
|
+
defaultDelegate = createMockDataDriverDelegate("default-postgres");
|
|
104
|
+
analyticsDelegate = createMockDataDriverDelegate("analytics-postgres");
|
|
105
|
+
|
|
106
|
+
registry.register(DEFAULT_DRIVER_ID, defaultDelegate);
|
|
107
|
+
registry.register("analytics", analyticsDelegate);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("should return specific driver when found", () => {
|
|
111
|
+
expect(registry.getOrDefault("analytics")).toBe(analyticsDelegate);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("should return default when id is undefined", () => {
|
|
115
|
+
expect(registry.getOrDefault(undefined)).toBe(defaultDelegate);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("should return default when id is null", () => {
|
|
119
|
+
expect(registry.getOrDefault(null)).toBe(defaultDelegate);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("should fallback to default when id not found", () => {
|
|
123
|
+
// This should log a warning and return the default
|
|
124
|
+
const consoleSpy = jest.spyOn(console, "warn").mockImplementation();
|
|
125
|
+
|
|
126
|
+
expect(registry.getOrDefault("non-existent")).toBe(defaultDelegate);
|
|
127
|
+
expect(consoleSpy).toHaveBeenCalledWith(
|
|
128
|
+
expect.stringContaining('Driver "non-existent" not found')
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
consoleSpy.mockRestore();
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("should throw when fallback fails (no default)", () => {
|
|
135
|
+
const emptyRegistry = new DefaultDriverRegistry();
|
|
136
|
+
|
|
137
|
+
expect(() => emptyRegistry.getOrDefault("anything")).toThrow();
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
describe("overwriting drivers", () => {
|
|
142
|
+
it("should overwrite existing driver with same id", () => {
|
|
143
|
+
const registry = new DefaultDriverRegistry();
|
|
144
|
+
const original = createMockDataDriverDelegate("original");
|
|
145
|
+
const replacement = createMockDataDriverDelegate("replacement");
|
|
146
|
+
|
|
147
|
+
const consoleSpy = jest.spyOn(console, "warn").mockImplementation();
|
|
148
|
+
|
|
149
|
+
registry.register("my-db", original);
|
|
150
|
+
registry.register("my-db", replacement);
|
|
151
|
+
|
|
152
|
+
expect(registry.get("my-db")).toBe(replacement);
|
|
153
|
+
expect(consoleSpy).toHaveBeenCalledWith(
|
|
154
|
+
expect.stringContaining('Overwriting driver with id "my-db"')
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
consoleSpy.mockRestore();
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
describe("list and size", () => {
|
|
162
|
+
it("should list all registered drivers", () => {
|
|
163
|
+
const registry = new DefaultDriverRegistry();
|
|
164
|
+
|
|
165
|
+
registry.register("db-1", createMockDataDriverDelegate("pg1"));
|
|
166
|
+
registry.register("db-2", createMockDataDriverDelegate("pg2"));
|
|
167
|
+
registry.register(DEFAULT_DRIVER_ID, createMockDataDriverDelegate("default"));
|
|
168
|
+
|
|
169
|
+
const list = registry.list();
|
|
170
|
+
expect(list).toHaveLength(3);
|
|
171
|
+
expect(list).toContain("db-1");
|
|
172
|
+
expect(list).toContain("db-2");
|
|
173
|
+
expect(list).toContain(DEFAULT_DRIVER_ID);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("should return correct size", () => {
|
|
177
|
+
const registry = new DefaultDriverRegistry();
|
|
178
|
+
|
|
179
|
+
expect(registry.size()).toBe(0);
|
|
180
|
+
|
|
181
|
+
registry.register("db-1", createMockDataDriverDelegate("pg1"));
|
|
182
|
+
expect(registry.size()).toBe(1);
|
|
183
|
+
|
|
184
|
+
registry.register("db-2", createMockDataDriverDelegate("pg2"));
|
|
185
|
+
expect(registry.size()).toBe(2);
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
describe("DefaultDriverRegistry.create() factory", () => {
|
|
191
|
+
describe("with single DataDriver", () => {
|
|
192
|
+
it('should register single delegate as "(default)"', () => {
|
|
193
|
+
const mockDelegate = createMockDataDriverDelegate("postgres");
|
|
194
|
+
|
|
195
|
+
const registry = DefaultDriverRegistry.create(mockDelegate);
|
|
196
|
+
|
|
197
|
+
expect(registry.has(DEFAULT_DRIVER_ID)).toBe(true);
|
|
198
|
+
expect(registry.getDefault()).toBe(mockDelegate);
|
|
199
|
+
expect(registry.size()).toBe(1);
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
describe("with map of DataDriverDelegates", () => {
|
|
204
|
+
it("should register all delegates from map", () => {
|
|
205
|
+
const defaultDelegate = createMockDataDriverDelegate("default-pg");
|
|
206
|
+
const analyticsDelegate = createMockDataDriverDelegate("analytics-pg");
|
|
207
|
+
|
|
208
|
+
const registry = DefaultDriverRegistry.create({
|
|
209
|
+
[DEFAULT_DRIVER_ID]: defaultDelegate,
|
|
210
|
+
"analytics": analyticsDelegate
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
expect(registry.size()).toBe(2);
|
|
214
|
+
expect(registry.getDefault()).toBe(defaultDelegate);
|
|
215
|
+
expect(registry.get("analytics")).toBe(analyticsDelegate);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it("should use first entry as default if no explicit default provided", () => {
|
|
219
|
+
const db1 = createMockDataDriverDelegate("db1");
|
|
220
|
+
const db2 = createMockDataDriverDelegate("db2");
|
|
221
|
+
|
|
222
|
+
const consoleSpy = jest.spyOn(console, "warn").mockImplementation();
|
|
223
|
+
|
|
224
|
+
const registry = DefaultDriverRegistry.create({
|
|
225
|
+
"primary": db1,
|
|
226
|
+
"secondary": db2
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
// Should have registered both + created default pointing to first
|
|
230
|
+
expect(registry.size()).toBe(3); // primary, secondary, (default)
|
|
231
|
+
expect(registry.has(DEFAULT_DRIVER_ID)).toBe(true);
|
|
232
|
+
expect(consoleSpy).toHaveBeenCalledWith(
|
|
233
|
+
expect.stringContaining('No "(default)" driver provided')
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
consoleSpy.mockRestore();
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it("should handle empty map gracefully", () => {
|
|
240
|
+
const registry = DefaultDriverRegistry.create({});
|
|
241
|
+
|
|
242
|
+
expect(registry.size()).toBe(0);
|
|
243
|
+
expect(() => registry.getDefault()).toThrow();
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
describe("type detection (isDataDriverDelegate)", () => {
|
|
249
|
+
it("should correctly identify a DataDriver", () => {
|
|
250
|
+
const mockDelegate = createMockDataDriverDelegate("postgres");
|
|
251
|
+
|
|
252
|
+
// The factory should recognize it as a single delegate
|
|
253
|
+
const registry = DefaultDriverRegistry.create(mockDelegate);
|
|
254
|
+
expect(registry.size()).toBe(1);
|
|
255
|
+
expect(registry.has(DEFAULT_DRIVER_ID)).toBe(true);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it("should correctly identify a map of DataDriverDelegates", () => {
|
|
259
|
+
const delegates = {
|
|
260
|
+
[DEFAULT_DRIVER_ID]: createMockDataDriverDelegate("pg1"),
|
|
261
|
+
"other": createMockDataDriverDelegate("pg2")
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
// The factory should recognize it as a map
|
|
265
|
+
const registry = DefaultDriverRegistry.create(delegates);
|
|
266
|
+
expect(registry.size()).toBe(2);
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
it("should not mistakenly identify a map as a single delegate", () => {
|
|
270
|
+
// A map doesn't have the required DataDriver methods
|
|
271
|
+
const map = {
|
|
272
|
+
key: "not-a-delegate", // This looks like the key property but...
|
|
273
|
+
db1: createMockDataDriverDelegate("pg1")
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
// This should be treated as a map (and show a warning because 'key' isn't a valid delegate)
|
|
277
|
+
const registry = DefaultDriverRegistry.create(map as any);
|
|
278
|
+
// 'key' entry will be ignored since it's not a valid delegate
|
|
279
|
+
expect(registry.has("db1")).toBe(true);
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
});
|