@edge-base/server 0.1.1
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/admin-build/.gitkeep +0 -0
- package/admin-build/_app/env.js +1 -0
- package/admin-build/_app/immutable/assets/0.Bm6cF078.css +1 -0
- package/admin-build/_app/immutable/assets/1.BfW3pUNa.css +1 -0
- package/admin-build/_app/immutable/assets/11.CVmQOewb.css +1 -0
- package/admin-build/_app/immutable/assets/12.B1EhbRZT.css +1 -0
- package/admin-build/_app/immutable/assets/13.BvwYeuwE.css +1 -0
- package/admin-build/_app/immutable/assets/14.CdVfcO0R.css +1 -0
- package/admin-build/_app/immutable/assets/15.2yeZ66b-.css +1 -0
- package/admin-build/_app/immutable/assets/17.BVg0JEVu.css +1 -0
- package/admin-build/_app/immutable/assets/18.Rwnl3x_i.css +1 -0
- package/admin-build/_app/immutable/assets/20.DsPWA9AV.css +1 -0
- package/admin-build/_app/immutable/assets/21.Dz2RJ56c.css +1 -0
- package/admin-build/_app/immutable/assets/22.DwNLk5Ai.css +1 -0
- package/admin-build/_app/immutable/assets/23.CFpu0gOO.css +1 -0
- package/admin-build/_app/immutable/assets/24.Cy5LBeoJ.css +1 -0
- package/admin-build/_app/immutable/assets/25.pUyLVf-h.css +1 -0
- package/admin-build/_app/immutable/assets/26.DBcGrlXa.css +1 -0
- package/admin-build/_app/immutable/assets/27.BswYyAJD.css +1 -0
- package/admin-build/_app/immutable/assets/28.B4ueB1Kf.css +1 -0
- package/admin-build/_app/immutable/assets/29.B-qU6PdF.css +1 -0
- package/admin-build/_app/immutable/assets/3.Dg81Pgmd.css +1 -0
- package/admin-build/_app/immutable/assets/30.CsdWum94.css +1 -0
- package/admin-build/_app/immutable/assets/31.U6OwIp50.css +1 -0
- package/admin-build/_app/immutable/assets/4.CyawCCux.css +1 -0
- package/admin-build/_app/immutable/assets/5.C0YO2HTk.css +1 -0
- package/admin-build/_app/immutable/assets/8.Br5jd6kD.css +1 -0
- package/admin-build/_app/immutable/assets/Badge.EMYLHBxE.css +1 -0
- package/admin-build/_app/immutable/assets/Button.DpzMRTjK.css +1 -0
- package/admin-build/_app/immutable/assets/ConfirmDialog.DAnaWRRk.css +1 -0
- package/admin-build/_app/immutable/assets/EmptyState.CwKsu57Y.css +1 -0
- package/admin-build/_app/immutable/assets/Input.BDUSenmU.css +1 -0
- package/admin-build/_app/immutable/assets/Modal.Dm5B0Xie.css +1 -0
- package/admin-build/_app/immutable/assets/PageShell.CmU-Xh-b.css +1 -0
- package/admin-build/_app/immutable/assets/SchemaFieldEditor.g4NsCdno.css +1 -0
- package/admin-build/_app/immutable/assets/Select.BW4Keufm.css +1 -0
- package/admin-build/_app/immutable/assets/Skeleton.KWUulTKJ.css +1 -0
- package/admin-build/_app/immutable/assets/Tabs.CniGYb67.css +1 -0
- package/admin-build/_app/immutable/assets/TimeChart.BTCDAvmT.css +1 -0
- package/admin-build/_app/immutable/assets/Toggle.Cy_K12OM.css +1 -0
- package/admin-build/_app/immutable/assets/TopList.ClFzmPlA.css +1 -0
- package/admin-build/_app/immutable/chunks/7B47DvSx.js +1 -0
- package/admin-build/_app/immutable/chunks/7f08Id8e.js +1 -0
- package/admin-build/_app/immutable/chunks/8wJeQ7LN.js +1 -0
- package/admin-build/_app/immutable/chunks/B-h2afW5.js +1 -0
- package/admin-build/_app/immutable/chunks/B8vJP3wz.js +1 -0
- package/admin-build/_app/immutable/chunks/BR_fL5Yv.js +1 -0
- package/admin-build/_app/immutable/chunks/BY92tFS2.js +1 -0
- package/admin-build/_app/immutable/chunks/BcR-Rdj9.js +1 -0
- package/admin-build/_app/immutable/chunks/BdrwyZv8.js +1 -0
- package/admin-build/_app/immutable/chunks/Bh56EfQ_.js +1 -0
- package/admin-build/_app/immutable/chunks/BkrCkgYp.js +1 -0
- package/admin-build/_app/immutable/chunks/BmRjiP5k.js +1 -0
- package/admin-build/_app/immutable/chunks/BsokvhWC.js +1 -0
- package/admin-build/_app/immutable/chunks/C4D51vTW.js +1 -0
- package/admin-build/_app/immutable/chunks/C6puvcoR.js +2 -0
- package/admin-build/_app/immutable/chunks/CCKNu7m7.js +1 -0
- package/admin-build/_app/immutable/chunks/CWj6FrbW.js +1 -0
- package/admin-build/_app/immutable/chunks/Ce-ngf4p.js +5 -0
- package/admin-build/_app/immutable/chunks/Cs0GwzJA.js +1 -0
- package/admin-build/_app/immutable/chunks/CwROoZK0.js +1 -0
- package/admin-build/_app/immutable/chunks/CxCPv_Ut.js +1 -0
- package/admin-build/_app/immutable/chunks/CxbRue-5.js +1 -0
- package/admin-build/_app/immutable/chunks/CyqB6g-D.js +1 -0
- package/admin-build/_app/immutable/chunks/D5h5A1cc.js +2 -0
- package/admin-build/_app/immutable/chunks/DnyL7Zq-.js +1 -0
- package/admin-build/_app/immutable/chunks/DoPXzH7F.js +1 -0
- package/admin-build/_app/immutable/chunks/DrQSgw-f.js +1 -0
- package/admin-build/_app/immutable/chunks/DttM2zNO.js +1 -0
- package/admin-build/_app/immutable/chunks/DuXuUBWN.js +1 -0
- package/admin-build/_app/immutable/chunks/MdeqaOQx.js +10 -0
- package/admin-build/_app/immutable/chunks/NuUjtcO2.js +1 -0
- package/admin-build/_app/immutable/chunks/Q2nPFxS6.js +1 -0
- package/admin-build/_app/immutable/chunks/R6arueIl.js +1 -0
- package/admin-build/_app/immutable/chunks/UUazaC_N.js +1 -0
- package/admin-build/_app/immutable/chunks/cOYbrQxx.js +1 -0
- package/admin-build/_app/immutable/chunks/eFQHTGwA.js +1 -0
- package/admin-build/_app/immutable/chunks/ehbppgYb.js +1 -0
- package/admin-build/_app/immutable/chunks/glwixJlP.js +1 -0
- package/admin-build/_app/immutable/chunks/vApWTCBs.js +1 -0
- package/admin-build/_app/immutable/chunks/w89G9Xpi.js +1 -0
- package/admin-build/_app/immutable/chunks/wJsUhbfZ.js +1 -0
- package/admin-build/_app/immutable/chunks/zfauFM8P.js +1 -0
- package/admin-build/_app/immutable/entry/app.CcO-Uos3.js +2 -0
- package/admin-build/_app/immutable/entry/start.COebYq3I.js +1 -0
- package/admin-build/_app/immutable/nodes/0.CjtHKU-6.js +1 -0
- package/admin-build/_app/immutable/nodes/1.DEisjlM0.js +1 -0
- package/admin-build/_app/immutable/nodes/10.CvhdyWVB.js +1 -0
- package/admin-build/_app/immutable/nodes/11.DjHqcOvy.js +1 -0
- package/admin-build/_app/immutable/nodes/12.mQLz4Mj_.js +1 -0
- package/admin-build/_app/immutable/nodes/13.CBonZZyP.js +110 -0
- package/admin-build/_app/immutable/nodes/14.d-oiZL0j.js +3 -0
- package/admin-build/_app/immutable/nodes/15.CKPQsUYF.js +1 -0
- package/admin-build/_app/immutable/nodes/16.wPzAPQGx.js +1 -0
- package/admin-build/_app/immutable/nodes/17.DayhKyEZ.js +1 -0
- package/admin-build/_app/immutable/nodes/18.DKwS0Ir0.js +1 -0
- package/admin-build/_app/immutable/nodes/19.wPzAPQGx.js +1 -0
- package/admin-build/_app/immutable/nodes/2.BKoKrw1i.js +1 -0
- package/admin-build/_app/immutable/nodes/20.BvIkkkrW.js +1 -0
- package/admin-build/_app/immutable/nodes/21.DMaFhdHk.js +128 -0
- package/admin-build/_app/immutable/nodes/22.3xdgwuK1.js +1 -0
- package/admin-build/_app/immutable/nodes/23.8Bvgjbsl.js +112 -0
- package/admin-build/_app/immutable/nodes/24.DzSSzRhG.js +2 -0
- package/admin-build/_app/immutable/nodes/25.9KKYBnAE.js +2 -0
- package/admin-build/_app/immutable/nodes/26.Bhn9dfhY.js +1 -0
- package/admin-build/_app/immutable/nodes/27.kRLiC24G.js +1 -0
- package/admin-build/_app/immutable/nodes/28.BVIN1-7N.js +1 -0
- package/admin-build/_app/immutable/nodes/29.3yabZWj4.js +1 -0
- package/admin-build/_app/immutable/nodes/3.BFtSOkX7.js +2 -0
- package/admin-build/_app/immutable/nodes/30.CyCQlwaP.js +1 -0
- package/admin-build/_app/immutable/nodes/31.C4LDXjES.js +1 -0
- package/admin-build/_app/immutable/nodes/4.CvbiMlCa.js +1 -0
- package/admin-build/_app/immutable/nodes/5.C6BLv2eM.js +1 -0
- package/admin-build/_app/immutable/nodes/6.BcXvfl2P.js +1 -0
- package/admin-build/_app/immutable/nodes/7.CIuqhPiK.js +1 -0
- package/admin-build/_app/immutable/nodes/8.BQOR_JfO.js +1 -0
- package/admin-build/_app/immutable/nodes/9.NZqXQxPy.js +1 -0
- package/admin-build/_app/version.json +1 -0
- package/admin-build/favicon.svg +26 -0
- package/admin-build/index.html +45 -0
- package/openapi.json +19543 -0
- package/package.json +66 -0
- package/src/__tests__/admin-assets.test.ts +55 -0
- package/src/__tests__/admin-data-routes.test.ts +488 -0
- package/src/__tests__/admin-db-target.test.ts +103 -0
- package/src/__tests__/admin-routing.test.ts +31 -0
- package/src/__tests__/admin-user-management.test.ts +311 -0
- package/src/__tests__/analytics-query.test.ts +75 -0
- package/src/__tests__/auth-d1.test.ts +749 -0
- package/src/__tests__/auth-db-adapter.test.ts +73 -0
- package/src/__tests__/auth-jwt.test.ts +440 -0
- package/src/__tests__/auth-oauth.test.ts +389 -0
- package/src/__tests__/auth-password.test.ts +367 -0
- package/src/__tests__/auth-redirect.test.ts +87 -0
- package/src/__tests__/backup-restore.test.ts +711 -0
- package/src/__tests__/broadcast.test.ts +128 -0
- package/src/__tests__/cli.test.ts +178 -0
- package/src/__tests__/cloudflare-realtime.test.ts +113 -0
- package/src/__tests__/config.test.ts +469 -0
- package/src/__tests__/cors.test.ts +154 -0
- package/src/__tests__/cron.test.ts +302 -0
- package/src/__tests__/d1-handler.test.ts +402 -0
- package/src/__tests__/d1-sql.test.ts +120 -0
- package/src/__tests__/database-live-config.test.ts +42 -0
- package/src/__tests__/database-live-emitter.test.ts +56 -0
- package/src/__tests__/database-live-filters.test.ts +63 -0
- package/src/__tests__/database-live-route.test.ts +113 -0
- package/src/__tests__/db-sql.test.ts +163 -0
- package/src/__tests__/do-lifecycle.test.ts +263 -0
- package/src/__tests__/do-router.test.ts +729 -0
- package/src/__tests__/email-provider.test.ts +128 -0
- package/src/__tests__/email-templates.test.ts +528 -0
- package/src/__tests__/error-format.test.ts +250 -0
- package/src/__tests__/field-ops.test.ts +242 -0
- package/src/__tests__/functions-context.test.ts +334 -0
- package/src/__tests__/functions-d1-proxy.test.ts +229 -0
- package/src/__tests__/functions-registry-runtime-config.test.ts +17 -0
- package/src/__tests__/functions-route.test.ts +139 -0
- package/src/__tests__/internal-request.test.ts +77 -0
- package/src/__tests__/log-writer.test.ts +44 -0
- package/src/__tests__/logger.test.ts +58 -0
- package/src/__tests__/meta-admin-proxy.test.ts +48 -0
- package/src/__tests__/meta-export-coverage.test.ts +191 -0
- package/src/__tests__/meta-route-registration.test.ts +47 -0
- package/src/__tests__/namespace-dump.test.ts +28 -0
- package/src/__tests__/oauth-providers.test.ts +337 -0
- package/src/__tests__/openapi-coverage.test.ts +144 -0
- package/src/__tests__/pagination.test.ts +59 -0
- package/src/__tests__/password-policy.test.ts +191 -0
- package/src/__tests__/plugin-migrations.test.ts +379 -0
- package/src/__tests__/postgres-batch-compat.test.ts +133 -0
- package/src/__tests__/postgres-dialect.test.ts +328 -0
- package/src/__tests__/postgres-executor.test.ts +79 -0
- package/src/__tests__/postgres-field-ops-compat.test.ts +222 -0
- package/src/__tests__/postgres-schema-init.test.ts +105 -0
- package/src/__tests__/postgres-table-utils.test.ts +107 -0
- package/src/__tests__/presence.test.ts +199 -0
- package/src/__tests__/provider.test.ts +550 -0
- package/src/__tests__/public-user-profile.test.ts +339 -0
- package/src/__tests__/push-handlers.test.ts +179 -0
- package/src/__tests__/push-provider.test.ts +80 -0
- package/src/__tests__/push-token.test.ts +418 -0
- package/src/__tests__/query.test.ts +771 -0
- package/src/__tests__/rate-limit.test.ts +260 -0
- package/src/__tests__/room-access-policy.test.ts +101 -0
- package/src/__tests__/room-handler-context.test.ts +130 -0
- package/src/__tests__/room-monitoring.test.ts +138 -0
- package/src/__tests__/room-runtime-routing.test.ts +222 -0
- package/src/__tests__/room.test.ts +254 -0
- package/src/__tests__/route-parser.test.ts +490 -0
- package/src/__tests__/rules.test.ts +234 -0
- package/src/__tests__/runtime-surface-accounting.test.ts +120 -0
- package/src/__tests__/scheduled.test.ts +80 -0
- package/src/__tests__/schema.test.ts +1273 -0
- package/src/__tests__/security-hardening.test.ts +312 -0
- package/src/__tests__/server.unit.test.ts +333 -0
- package/src/__tests__/service-key-db-proxy.test.ts +650 -0
- package/src/__tests__/service-key-provider-bypass.test.ts +138 -0
- package/src/__tests__/service-key.test.ts +757 -0
- package/src/__tests__/smoke-skip-report.test.ts +72 -0
- package/src/__tests__/sms-provider.test.ts +39 -0
- package/src/__tests__/sql-route.test.ts +218 -0
- package/src/__tests__/storage-hook-context.test.ts +115 -0
- package/src/__tests__/totp.test.ts +200 -0
- package/src/__tests__/uuid.test.ts +144 -0
- package/src/__tests__/validation.test.ts +773 -0
- package/src/__tests__/websocket-pending.test.ts +163 -0
- package/src/_functions-registry.ts +51 -0
- package/src/bench-entry.ts +9 -0
- package/src/cloudflare-test.d.ts +1 -0
- package/src/durable-objects/auth-do.ts +49 -0
- package/src/durable-objects/database-do.ts +2240 -0
- package/src/durable-objects/database-live-do.ts +949 -0
- package/src/durable-objects/logs-do.ts +1200 -0
- package/src/durable-objects/room-runtime-base.ts +1604 -0
- package/src/durable-objects/rooms-do.ts +2191 -0
- package/src/generated-config.ts +6 -0
- package/src/index.ts +382 -0
- package/src/lib/admin-assets.ts +54 -0
- package/src/lib/admin-db-target.ts +301 -0
- package/src/lib/admin-routing.ts +35 -0
- package/src/lib/admin-user-management.ts +464 -0
- package/src/lib/analytics-adapter.ts +103 -0
- package/src/lib/analytics-query.ts +579 -0
- package/src/lib/auth-d1-service.ts +1193 -0
- package/src/lib/auth-d1.ts +1056 -0
- package/src/lib/auth-db-adapter.ts +289 -0
- package/src/lib/auth-redirect.ts +116 -0
- package/src/lib/cidr.ts +115 -0
- package/src/lib/client-ip.ts +51 -0
- package/src/lib/cloudflare-realtime.ts +251 -0
- package/src/lib/control-db.ts +36 -0
- package/src/lib/cron.ts +163 -0
- package/src/lib/d1-handler.ts +1425 -0
- package/src/lib/d1-schema-init.ts +255 -0
- package/src/lib/d1-sql.ts +33 -0
- package/src/lib/database-live-config.ts +24 -0
- package/src/lib/database-live-emitter.ts +111 -0
- package/src/lib/db-sql.ts +66 -0
- package/src/lib/do-retry.ts +36 -0
- package/src/lib/do-router.ts +270 -0
- package/src/lib/do-sql.ts +73 -0
- package/src/lib/email-provider.ts +379 -0
- package/src/lib/email-templates.ts +285 -0
- package/src/lib/email-translations.ts +422 -0
- package/src/lib/errors.ts +151 -0
- package/src/lib/functions.ts +2091 -0
- package/src/lib/hono.ts +56 -0
- package/src/lib/internal-request.ts +56 -0
- package/src/lib/jwt.ts +354 -0
- package/src/lib/log-writer.ts +272 -0
- package/src/lib/namespace-dump.ts +125 -0
- package/src/lib/oauth-providers.ts +1225 -0
- package/src/lib/op-parser.ts +99 -0
- package/src/lib/openapi.ts +146 -0
- package/src/lib/pagination.ts +19 -0
- package/src/lib/password-policy.ts +102 -0
- package/src/lib/password.ts +145 -0
- package/src/lib/plugin-migrations.ts +612 -0
- package/src/lib/postgres-executor.ts +203 -0
- package/src/lib/postgres-handler.ts +1102 -0
- package/src/lib/postgres-schema-init.ts +341 -0
- package/src/lib/postgres-table-utils.ts +87 -0
- package/src/lib/public-user-profile.ts +187 -0
- package/src/lib/push-provider.ts +409 -0
- package/src/lib/push-token.ts +294 -0
- package/src/lib/query-engine.ts +768 -0
- package/src/lib/room-monitoring.ts +97 -0
- package/src/lib/room-runtime.ts +14 -0
- package/src/lib/route-parser.ts +434 -0
- package/src/lib/schema.ts +538 -0
- package/src/lib/schemas.ts +152 -0
- package/src/lib/service-key.ts +419 -0
- package/src/lib/sms-provider.ts +230 -0
- package/src/lib/startup-config.ts +99 -0
- package/src/lib/totp.ts +242 -0
- package/src/lib/uuid.ts +87 -0
- package/src/lib/validation.ts +205 -0
- package/src/lib/version.ts +2 -0
- package/src/lib/websocket-pending.ts +40 -0
- package/src/middleware/auth.ts +169 -0
- package/src/middleware/captcha-verify.ts +217 -0
- package/src/middleware/cors.ts +159 -0
- package/src/middleware/error-handler.ts +54 -0
- package/src/middleware/internal-guard.ts +26 -0
- package/src/middleware/logger.ts +126 -0
- package/src/middleware/rate-limit.ts +283 -0
- package/src/middleware/rules.ts +475 -0
- package/src/routes/admin-auth.ts +447 -0
- package/src/routes/admin.ts +3501 -0
- package/src/routes/analytics-api.ts +290 -0
- package/src/routes/auth.ts +4222 -0
- package/src/routes/backup.ts +1466 -0
- package/src/routes/config.ts +53 -0
- package/src/routes/d1.ts +109 -0
- package/src/routes/database-live.ts +281 -0
- package/src/routes/functions.ts +155 -0
- package/src/routes/health.ts +32 -0
- package/src/routes/kv.ts +167 -0
- package/src/routes/oauth.ts +1055 -0
- package/src/routes/push.ts +1465 -0
- package/src/routes/room.ts +639 -0
- package/src/routes/schema-endpoint.ts +76 -0
- package/src/routes/sql.ts +176 -0
- package/src/routes/storage.ts +1674 -0
- package/src/routes/tables.ts +699 -0
- package/src/routes/users.ts +21 -0
- package/src/routes/vectorize.ts +372 -0
- package/src/types.ts +99 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import type { TableConfig } from '@edge-base/shared';
|
|
3
|
+
import { resolvePgInitOrder } from '../lib/postgres-schema-init.js';
|
|
4
|
+
|
|
5
|
+
afterEach(async () => {
|
|
6
|
+
vi.resetModules();
|
|
7
|
+
vi.clearAllMocks();
|
|
8
|
+
vi.doUnmock('../lib/postgres-executor.js');
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
describe('resolvePgInitOrder', () => {
|
|
12
|
+
it('orders referenced tables before dependents', () => {
|
|
13
|
+
const tables: Record<string, TableConfig> = {
|
|
14
|
+
posts: {
|
|
15
|
+
schema: {
|
|
16
|
+
categoryId: { type: 'string', references: 'categories' },
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
categories: {
|
|
20
|
+
schema: {
|
|
21
|
+
name: { type: 'string', required: true },
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
expect(resolvePgInitOrder(tables).map(([tableName]) => tableName)).toEqual([
|
|
27
|
+
'categories',
|
|
28
|
+
'posts',
|
|
29
|
+
]);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('ignores auth-only logical references when ordering', () => {
|
|
33
|
+
const tables: Record<string, TableConfig> = {
|
|
34
|
+
posts: {
|
|
35
|
+
schema: {
|
|
36
|
+
authorId: { type: 'string', references: 'users' },
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
categories: {
|
|
40
|
+
schema: {
|
|
41
|
+
name: { type: 'string', required: true },
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
expect(resolvePgInitOrder(tables).map(([tableName]) => tableName)).toEqual([
|
|
47
|
+
'posts',
|
|
48
|
+
'categories',
|
|
49
|
+
]);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('caches schema initialization for the same namespace/config signature', async () => {
|
|
53
|
+
const withPostgresConnection = vi.fn(async (_connectionString: string, fn: (query: (sql: string, params?: unknown[]) => Promise<{ columns: string[]; rows: Record<string, unknown>[]; rowCount: number }>) => Promise<void>) => {
|
|
54
|
+
const query = vi.fn(async () => ({
|
|
55
|
+
columns: [],
|
|
56
|
+
rows: [],
|
|
57
|
+
rowCount: 0,
|
|
58
|
+
}));
|
|
59
|
+
await fn(query);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
vi.doMock('../lib/postgres-executor.js', () => ({
|
|
63
|
+
executePostgresQuery: vi.fn(),
|
|
64
|
+
withPostgresConnection,
|
|
65
|
+
}));
|
|
66
|
+
|
|
67
|
+
const { ensurePgSchema, _resetPgSchemaCache } = await import('../lib/postgres-schema-init.js');
|
|
68
|
+
_resetPgSchemaCache();
|
|
69
|
+
|
|
70
|
+
await ensurePgSchema('postgres://edgebase:test@localhost/db', 'shared', {});
|
|
71
|
+
await ensurePgSchema('postgres://edgebase:test@localhost/db', 'shared', {});
|
|
72
|
+
|
|
73
|
+
expect(withPostgresConnection).toHaveBeenCalledTimes(1);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('re-runs schema initialization when the config signature changes', async () => {
|
|
77
|
+
const withPostgresConnection = vi.fn(async (_connectionString: string, fn: (query: (sql: string, params?: unknown[]) => Promise<{ columns: string[]; rows: Record<string, unknown>[]; rowCount: number }>) => Promise<void>) => {
|
|
78
|
+
const query = vi.fn(async () => ({
|
|
79
|
+
columns: [],
|
|
80
|
+
rows: [],
|
|
81
|
+
rowCount: 0,
|
|
82
|
+
}));
|
|
83
|
+
await fn(query);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
vi.doMock('../lib/postgres-executor.js', () => ({
|
|
87
|
+
executePostgresQuery: vi.fn(),
|
|
88
|
+
withPostgresConnection,
|
|
89
|
+
}));
|
|
90
|
+
|
|
91
|
+
const { ensurePgSchema, _resetPgSchemaCache } = await import('../lib/postgres-schema-init.js');
|
|
92
|
+
_resetPgSchemaCache();
|
|
93
|
+
|
|
94
|
+
await ensurePgSchema('postgres://edgebase:test@localhost/db', 'shared', {});
|
|
95
|
+
await ensurePgSchema('postgres://edgebase:test@localhost/db', 'shared', {
|
|
96
|
+
posts: {
|
|
97
|
+
schema: {
|
|
98
|
+
title: { type: 'string' },
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
expect(withPostgresConnection).toHaveBeenCalledTimes(2);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import type { TableConfig } from '@edge-base/shared';
|
|
3
|
+
import {
|
|
4
|
+
escapePgIdentifier,
|
|
5
|
+
filterToPgSchemaColumns,
|
|
6
|
+
preparePgInsertData,
|
|
7
|
+
preparePgUpdateData,
|
|
8
|
+
serializePgJsonFields,
|
|
9
|
+
stripInternalPgFields,
|
|
10
|
+
} from '../lib/postgres-table-utils.js';
|
|
11
|
+
|
|
12
|
+
const tableConfig: TableConfig = {
|
|
13
|
+
schema: {
|
|
14
|
+
title: { type: 'string', required: true },
|
|
15
|
+
status: { type: 'string', default: 'draft' },
|
|
16
|
+
metadata: { type: 'json' },
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
describe('postgres table utils', () => {
|
|
21
|
+
it('escapePgIdentifier quotes identifiers and escapes embedded quotes', () => {
|
|
22
|
+
expect(escapePgIdentifier('posts')).toBe('"posts"');
|
|
23
|
+
expect(escapePgIdentifier('weird"name')).toBe('"weird""name"');
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('serializePgJsonFields stringifies json schema fields without double-stringifying existing strings', () => {
|
|
27
|
+
const data = {
|
|
28
|
+
title: 'hello',
|
|
29
|
+
metadata: { tags: ['a'] },
|
|
30
|
+
rawJson: '{"ready":true}',
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
serializePgJsonFields(data, {
|
|
34
|
+
title: { type: 'string' },
|
|
35
|
+
metadata: { type: 'json' },
|
|
36
|
+
rawJson: { type: 'json' },
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
expect(data.title).toBe('hello');
|
|
40
|
+
expect(data.metadata).toBe(JSON.stringify({ tags: ['a'] }));
|
|
41
|
+
expect(data.rawJson).toBe('{"ready":true}');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('filterToPgSchemaColumns keeps only declared schema fields', () => {
|
|
45
|
+
expect(
|
|
46
|
+
filterToPgSchemaColumns(
|
|
47
|
+
{
|
|
48
|
+
title: 'hello',
|
|
49
|
+
metadata: { ok: true },
|
|
50
|
+
ignored: 'nope',
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
title: { type: 'string' },
|
|
54
|
+
metadata: { type: 'json' },
|
|
55
|
+
},
|
|
56
|
+
),
|
|
57
|
+
).toEqual({
|
|
58
|
+
title: 'hello',
|
|
59
|
+
metadata: { ok: true },
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('preparePgInsertData applies auto fields, defaults, schema filtering, and JSON serialization', () => {
|
|
64
|
+
const { data } = preparePgInsertData(
|
|
65
|
+
{
|
|
66
|
+
title: 'hello',
|
|
67
|
+
metadata: { tags: ['a'] },
|
|
68
|
+
ignored: 'nope',
|
|
69
|
+
},
|
|
70
|
+
tableConfig,
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
expect(typeof data.id).toBe('string');
|
|
74
|
+
expect(typeof data.createdAt).toBe('string');
|
|
75
|
+
expect(typeof data.updatedAt).toBe('string');
|
|
76
|
+
expect(data.status).toBe('draft');
|
|
77
|
+
expect(data.metadata).toBe(JSON.stringify({ tags: ['a'] }));
|
|
78
|
+
expect(data.ignored).toBeUndefined();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('preparePgUpdateData strips immutable fields and serializes JSON', () => {
|
|
82
|
+
const { data } = preparePgUpdateData(
|
|
83
|
+
{
|
|
84
|
+
id: 'row-1',
|
|
85
|
+
createdAt: 'yesterday',
|
|
86
|
+
title: 'updated',
|
|
87
|
+
metadata: { ok: true },
|
|
88
|
+
ignored: 'nope',
|
|
89
|
+
},
|
|
90
|
+
tableConfig,
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
expect(data.id).toBeUndefined();
|
|
94
|
+
expect(data.createdAt).toBeUndefined();
|
|
95
|
+
expect(data.title).toBe('updated');
|
|
96
|
+
expect(data.metadata).toBe(JSON.stringify({ ok: true }));
|
|
97
|
+
expect(typeof data.updatedAt).toBe('string');
|
|
98
|
+
expect(data.ignored).toBeUndefined();
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('stripInternalPgFields removes internal postgres helper columns', () => {
|
|
102
|
+
expect(stripInternalPgFields({ id: 'row-1', _fts: 'vector', title: 'hello' })).toEqual({
|
|
103
|
+
id: 'row-1',
|
|
104
|
+
title: 'hello',
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
});
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 서버 단위 테스트 — Presence + Broadcast 로직
|
|
3
|
+
* 1-18 presence.test.ts — 30개
|
|
4
|
+
* (RoomsDO 내 Presence 로직 — 순수 로직 파트)
|
|
5
|
+
*
|
|
6
|
+
* 실행: cd packages/server && npx vitest run src/__tests__/presence.test.ts
|
|
7
|
+
*
|
|
8
|
+
* NOTE: WebSocket 연결 없이 Presence 데이터 구조만 검증
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { describe, it, expect } from 'vitest';
|
|
12
|
+
|
|
13
|
+
// ── Presence 상태 관리 로직 (RoomsDO 동일 패턴) ──────────────────────────────
|
|
14
|
+
|
|
15
|
+
interface PresenceEntry {
|
|
16
|
+
userId: string;
|
|
17
|
+
data: Record<string, unknown>;
|
|
18
|
+
updatedAt: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
class PresenceStore {
|
|
22
|
+
private store = new Map<string, PresenceEntry>();
|
|
23
|
+
private readonly MAX_PRESENCE_SIZE = 1024; // 1KB per
|
|
24
|
+
|
|
25
|
+
track(userId: string, data: Record<string, unknown>): boolean {
|
|
26
|
+
const json = JSON.stringify(data);
|
|
27
|
+
if (json.length > this.MAX_PRESENCE_SIZE) {
|
|
28
|
+
return false; // size exceeded
|
|
29
|
+
}
|
|
30
|
+
this.store.set(userId, { userId, data, updatedAt: Date.now() });
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
untrack(userId: string): boolean {
|
|
35
|
+
return this.store.delete(userId);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
getAll(): PresenceEntry[] {
|
|
39
|
+
return Array.from(this.store.values());
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
getUser(userId: string): PresenceEntry | undefined {
|
|
43
|
+
return this.store.get(userId);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
size(): number {
|
|
47
|
+
return this.store.size;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
clear(): void {
|
|
51
|
+
this.store.clear();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// ─── A. track ────────────────────────────────────────────────────────────────
|
|
56
|
+
|
|
57
|
+
describe('PresenceStore.track', () => {
|
|
58
|
+
it('tracks a user', () => {
|
|
59
|
+
const ps = new PresenceStore();
|
|
60
|
+
const result = ps.track('user-1', { status: 'online' });
|
|
61
|
+
expect(result).toBe(true);
|
|
62
|
+
expect(ps.size()).toBe(1);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('tracks multiple users', () => {
|
|
66
|
+
const ps = new PresenceStore();
|
|
67
|
+
ps.track('user-1', { status: 'online' });
|
|
68
|
+
ps.track('user-2', { status: 'away' });
|
|
69
|
+
expect(ps.size()).toBe(2);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('overwrites existing presence for same userId', () => {
|
|
73
|
+
const ps = new PresenceStore();
|
|
74
|
+
ps.track('user-1', { status: 'online' });
|
|
75
|
+
ps.track('user-1', { status: 'away' });
|
|
76
|
+
expect(ps.size()).toBe(1);
|
|
77
|
+
expect(ps.getUser('user-1')?.data['status']).toBe('away');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('rejects data exceeding 1KB → false', () => {
|
|
81
|
+
const ps = new PresenceStore();
|
|
82
|
+
const bigData = { payload: 'x'.repeat(1025) };
|
|
83
|
+
const result = ps.track('user-1', bigData);
|
|
84
|
+
expect(result).toBe(false);
|
|
85
|
+
expect(ps.size()).toBe(0);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('data exactly at 1024 bytes → accepted', () => {
|
|
89
|
+
const ps = new PresenceStore();
|
|
90
|
+
// Create exactly 1024B JSON
|
|
91
|
+
const data = { key: 'a'.repeat(1024 - '{"key":""}'. length) };
|
|
92
|
+
const json = JSON.stringify(data);
|
|
93
|
+
const result = ps.track('user-1', data);
|
|
94
|
+
expect(result).toBe(json.length <= 1024);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('track stores updatedAt timestamp', () => {
|
|
98
|
+
const ps = new PresenceStore();
|
|
99
|
+
const before = Date.now();
|
|
100
|
+
ps.track('user-1', {});
|
|
101
|
+
const after = Date.now();
|
|
102
|
+
const entry = ps.getUser('user-1');
|
|
103
|
+
expect(entry?.updatedAt).toBeGreaterThanOrEqual(before);
|
|
104
|
+
expect(entry?.updatedAt).toBeLessThanOrEqual(after);
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// ─── B. untrack ──────────────────────────────────────────────────────────────
|
|
109
|
+
|
|
110
|
+
describe('PresenceStore.untrack', () => {
|
|
111
|
+
it('removes tracked user', () => {
|
|
112
|
+
const ps = new PresenceStore();
|
|
113
|
+
ps.track('user-1', { status: 'online' });
|
|
114
|
+
const result = ps.untrack('user-1');
|
|
115
|
+
expect(result).toBe(true);
|
|
116
|
+
expect(ps.size()).toBe(0);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('returns false for non-existent user', () => {
|
|
120
|
+
const ps = new PresenceStore();
|
|
121
|
+
expect(ps.untrack('unknown')).toBe(false);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('does not affect other users', () => {
|
|
125
|
+
const ps = new PresenceStore();
|
|
126
|
+
ps.track('user-1', {});
|
|
127
|
+
ps.track('user-2', {});
|
|
128
|
+
ps.untrack('user-1');
|
|
129
|
+
expect(ps.size()).toBe(1);
|
|
130
|
+
expect(ps.getUser('user-2')).toBeDefined();
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
// ─── C. getAll (presence broadcast) ─────────────────────────────────────────
|
|
135
|
+
|
|
136
|
+
describe('PresenceStore.getAll', () => {
|
|
137
|
+
it('returns all entries', () => {
|
|
138
|
+
const ps = new PresenceStore();
|
|
139
|
+
ps.track('user-1', { status: 'online' });
|
|
140
|
+
ps.track('user-2', { status: 'away' });
|
|
141
|
+
const all = ps.getAll();
|
|
142
|
+
expect(all.length).toBe(2);
|
|
143
|
+
const ids = all.map((e) => e.userId);
|
|
144
|
+
expect(ids).toContain('user-1');
|
|
145
|
+
expect(ids).toContain('user-2');
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('returns empty array when no presence', () => {
|
|
149
|
+
const ps = new PresenceStore();
|
|
150
|
+
expect(ps.getAll()).toEqual([]);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('clear removes all presence', () => {
|
|
154
|
+
const ps = new PresenceStore();
|
|
155
|
+
ps.track('user-1', {});
|
|
156
|
+
ps.track('user-2', {});
|
|
157
|
+
ps.clear();
|
|
158
|
+
expect(ps.size()).toBe(0);
|
|
159
|
+
expect(ps.getAll()).toEqual([]);
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
// ─── D. Presence on disconnect (자동 untrack) ────────────────────────────────
|
|
164
|
+
|
|
165
|
+
describe('Presence auto-untrack on disconnect', () => {
|
|
166
|
+
it('simulated disconnect removes presence', () => {
|
|
167
|
+
const ps = new PresenceStore();
|
|
168
|
+
const connections = new Map<string, string>(); // connectionId → userId
|
|
169
|
+
|
|
170
|
+
// Connect
|
|
171
|
+
connections.set('conn-1', 'user-1');
|
|
172
|
+
ps.track('user-1', { status: 'online' });
|
|
173
|
+
|
|
174
|
+
// Disconnect
|
|
175
|
+
const userId = connections.get('conn-1');
|
|
176
|
+
if (userId) ps.untrack(userId);
|
|
177
|
+
connections.delete('conn-1');
|
|
178
|
+
|
|
179
|
+
expect(ps.size()).toBe(0);
|
|
180
|
+
expect(ps.getUser('user-1')).toBeUndefined();
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('disconnect of non-connected user is safe', () => {
|
|
184
|
+
const ps = new PresenceStore();
|
|
185
|
+
expect(() => ps.untrack('non-existent')).not.toThrow();
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
// ─── E. Multiple clients same user ──────────────────────────────────────────
|
|
190
|
+
|
|
191
|
+
describe('PresenceStore — multi-client same user', () => {
|
|
192
|
+
it('last write wins for same userId', () => {
|
|
193
|
+
const ps = new PresenceStore();
|
|
194
|
+
ps.track('user-1', { device: 'mobile' });
|
|
195
|
+
ps.track('user-1', { device: 'desktop' });
|
|
196
|
+
expect(ps.getUser('user-1')?.data['device']).toBe('desktop');
|
|
197
|
+
expect(ps.size()).toBe(1);
|
|
198
|
+
});
|
|
199
|
+
});
|