@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,339 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import type { AuthDb } from '../lib/auth-db-adapter.js';
|
|
3
|
+
import {
|
|
4
|
+
deletePublicUserProjection,
|
|
5
|
+
getPublicProfileWithCache,
|
|
6
|
+
invalidatePublicUserCache,
|
|
7
|
+
queuePublicUserProjectionDelete,
|
|
8
|
+
queuePublicUserProjectionSync,
|
|
9
|
+
syncPublicUserProjection,
|
|
10
|
+
} from '../lib/public-user-profile.js';
|
|
11
|
+
|
|
12
|
+
type PublicProfile = Record<string, unknown>;
|
|
13
|
+
|
|
14
|
+
function createMockAuthDb(
|
|
15
|
+
initialProfiles: Array<[string, PublicProfile]> = [],
|
|
16
|
+
): AuthDb & {
|
|
17
|
+
_profiles: Map<string, PublicProfile>;
|
|
18
|
+
_firstCalls: number;
|
|
19
|
+
} {
|
|
20
|
+
const profiles = new Map(initialProfiles);
|
|
21
|
+
let firstCalls = 0;
|
|
22
|
+
|
|
23
|
+
const db = {
|
|
24
|
+
dialect: 'sqlite' as const,
|
|
25
|
+
async query<T = Record<string, unknown>>(): Promise<T[]> {
|
|
26
|
+
return [];
|
|
27
|
+
},
|
|
28
|
+
async first<T = Record<string, unknown>>(sql: string, params?: unknown[]): Promise<T | null> {
|
|
29
|
+
firstCalls++;
|
|
30
|
+
if (sql.includes('FROM _users_public WHERE id = ?')) {
|
|
31
|
+
return (profiles.get(String(params?.[0] ?? '')) ?? null) as T | null;
|
|
32
|
+
}
|
|
33
|
+
return null;
|
|
34
|
+
},
|
|
35
|
+
async run(sql: string, params?: unknown[]): Promise<void> {
|
|
36
|
+
if (sql.includes('INSERT OR REPLACE INTO _users_public')) {
|
|
37
|
+
const [userId, email, displayName, avatarUrl, role, isAnonymous, createdAt, updatedAt] = params ?? [];
|
|
38
|
+
profiles.set(String(userId), {
|
|
39
|
+
id: userId,
|
|
40
|
+
email,
|
|
41
|
+
displayName,
|
|
42
|
+
avatarUrl,
|
|
43
|
+
role,
|
|
44
|
+
isAnonymous,
|
|
45
|
+
createdAt,
|
|
46
|
+
updatedAt,
|
|
47
|
+
});
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (sql.includes('DELETE FROM _users_public WHERE id = ?')) {
|
|
51
|
+
profiles.delete(String(params?.[0] ?? ''));
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
async batch(statements: { sql: string; params?: unknown[] }[]): Promise<void> {
|
|
55
|
+
for (const statement of statements) {
|
|
56
|
+
if (statement.sql.includes('DELETE FROM _users_public WHERE id = ?')) {
|
|
57
|
+
profiles.delete(String(statement.params?.[0] ?? ''));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
_profiles: profiles,
|
|
62
|
+
get _firstCalls() {
|
|
63
|
+
return firstCalls;
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
return db;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function createMockKv(options: {
|
|
71
|
+
putError?: Error;
|
|
72
|
+
deleteError?: Error;
|
|
73
|
+
initial?: Record<string, string>;
|
|
74
|
+
} = {}): KVNamespace & { _store: Record<string, string> } {
|
|
75
|
+
const store = { ...(options.initial ?? {}) };
|
|
76
|
+
return {
|
|
77
|
+
get: async (key: string) => store[key] ?? null,
|
|
78
|
+
put: async (key: string, value: string) => {
|
|
79
|
+
if (options.putError) throw options.putError;
|
|
80
|
+
store[key] = value;
|
|
81
|
+
},
|
|
82
|
+
delete: async (key: string) => {
|
|
83
|
+
if (options.deleteError) throw options.deleteError;
|
|
84
|
+
delete store[key];
|
|
85
|
+
},
|
|
86
|
+
list: async () => ({ keys: [], list_complete: true, cacheStatus: null }),
|
|
87
|
+
getWithMetadata: async () => ({ value: null, metadata: null, cacheStatus: null }),
|
|
88
|
+
_store: store,
|
|
89
|
+
} as unknown as KVNamespace & { _store: Record<string, string> };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function createExecutionContext(): {
|
|
93
|
+
ctx: ExecutionContext;
|
|
94
|
+
pending: Promise<unknown>[];
|
|
95
|
+
} {
|
|
96
|
+
const pending: Promise<unknown>[] = [];
|
|
97
|
+
return {
|
|
98
|
+
pending,
|
|
99
|
+
ctx: {
|
|
100
|
+
waitUntil(promise: Promise<unknown>) {
|
|
101
|
+
pending.push(Promise.resolve(promise));
|
|
102
|
+
},
|
|
103
|
+
} as ExecutionContext,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
let userIdCounter = 0;
|
|
108
|
+
|
|
109
|
+
function nextUserId(): string {
|
|
110
|
+
userIdCounter += 1;
|
|
111
|
+
return `public-user-${userIdCounter}`;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function flushMicrotasks(): Promise<void> {
|
|
115
|
+
await Promise.resolve();
|
|
116
|
+
await Promise.resolve();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
afterEach(() => {
|
|
120
|
+
vi.restoreAllMocks();
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
describe('getPublicProfileWithCache', () => {
|
|
124
|
+
it('reads from auth DB once and then serves the L1 cache', async () => {
|
|
125
|
+
const userId = nextUserId();
|
|
126
|
+
const profile = {
|
|
127
|
+
id: userId,
|
|
128
|
+
email: 'public@example.com',
|
|
129
|
+
displayName: 'Public User',
|
|
130
|
+
avatarUrl: null,
|
|
131
|
+
role: 'user',
|
|
132
|
+
isAnonymous: 0,
|
|
133
|
+
createdAt: '2026-03-10T00:00:00.000Z',
|
|
134
|
+
updatedAt: '2026-03-10T00:00:00.000Z',
|
|
135
|
+
};
|
|
136
|
+
const authDb = createMockAuthDb([[userId, profile]]);
|
|
137
|
+
|
|
138
|
+
const first = await getPublicProfileWithCache(authDb, userId, {});
|
|
139
|
+
authDb._profiles.delete(userId);
|
|
140
|
+
const second = await getPublicProfileWithCache(authDb, userId, {});
|
|
141
|
+
|
|
142
|
+
expect(first).toEqual(profile);
|
|
143
|
+
expect(second).toEqual(profile);
|
|
144
|
+
expect(authDb._firstCalls).toBe(1);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('treats KV population failures as best-effort when no execution context is available', async () => {
|
|
148
|
+
const userId = nextUserId();
|
|
149
|
+
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
150
|
+
const authDb = createMockAuthDb([[
|
|
151
|
+
userId,
|
|
152
|
+
{
|
|
153
|
+
id: userId,
|
|
154
|
+
email: null,
|
|
155
|
+
displayName: 'KV Fallback',
|
|
156
|
+
avatarUrl: null,
|
|
157
|
+
role: 'user',
|
|
158
|
+
isAnonymous: 0,
|
|
159
|
+
createdAt: '2026-03-10T00:00:00.000Z',
|
|
160
|
+
updatedAt: '2026-03-10T00:00:00.000Z',
|
|
161
|
+
},
|
|
162
|
+
]]);
|
|
163
|
+
const kv = createMockKv({ putError: new Error('kv-put-failed') });
|
|
164
|
+
|
|
165
|
+
await expect(getPublicProfileWithCache(authDb, userId, { kv })).resolves.toMatchObject({
|
|
166
|
+
id: userId,
|
|
167
|
+
displayName: 'KV Fallback',
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
await flushMicrotasks();
|
|
171
|
+
expect(errorSpy).toHaveBeenCalled();
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
describe('syncPublicUserProjection', () => {
|
|
176
|
+
it('writes the auth projection and mirrors KV via waitUntil', async () => {
|
|
177
|
+
const userId = nextUserId();
|
|
178
|
+
const authDb = createMockAuthDb();
|
|
179
|
+
const kv = createMockKv();
|
|
180
|
+
const { ctx, pending } = createExecutionContext();
|
|
181
|
+
const profile = {
|
|
182
|
+
email: 'public@example.com',
|
|
183
|
+
displayName: 'Mirror User',
|
|
184
|
+
avatarUrl: null,
|
|
185
|
+
role: 'admin',
|
|
186
|
+
isAnonymous: 0,
|
|
187
|
+
createdAt: '2026-03-10T00:00:00.000Z',
|
|
188
|
+
updatedAt: '2026-03-10T00:00:00.000Z',
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
await syncPublicUserProjection(authDb, userId, profile, {
|
|
192
|
+
kv,
|
|
193
|
+
executionCtx: ctx,
|
|
194
|
+
awaitCacheWrites: false,
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
expect(authDb._profiles.get(userId)).toEqual({ id: userId, ...profile });
|
|
198
|
+
expect(pending).toHaveLength(1);
|
|
199
|
+
await Promise.all(pending);
|
|
200
|
+
expect(JSON.parse(kv._store[`kv:users_public:${userId}`])).toEqual({ id: userId, ...profile });
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('does not fail the caller when KV mirroring rejects without an execution context', async () => {
|
|
204
|
+
const userId = nextUserId();
|
|
205
|
+
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
206
|
+
const authDb = createMockAuthDb();
|
|
207
|
+
const kv = createMockKv({ putError: new Error('kv-put-failed') });
|
|
208
|
+
|
|
209
|
+
await expect(syncPublicUserProjection(authDb, userId, {
|
|
210
|
+
displayName: 'Best Effort Sync',
|
|
211
|
+
createdAt: '2026-03-10T00:00:00.000Z',
|
|
212
|
+
updatedAt: '2026-03-10T00:00:00.000Z',
|
|
213
|
+
}, { kv })).resolves.toBeUndefined();
|
|
214
|
+
|
|
215
|
+
expect(authDb._profiles.get(userId)).toMatchObject({ id: userId, displayName: 'Best Effort Sync' });
|
|
216
|
+
await flushMicrotasks();
|
|
217
|
+
expect(errorSpy).toHaveBeenCalled();
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
describe('queuePublicUserProjectionSync', () => {
|
|
222
|
+
it('queues projection syncing without surfacing async failures to the caller', async () => {
|
|
223
|
+
const userId = nextUserId();
|
|
224
|
+
const authDb = createMockAuthDb();
|
|
225
|
+
const kv = createMockKv();
|
|
226
|
+
const { ctx, pending } = createExecutionContext();
|
|
227
|
+
|
|
228
|
+
queuePublicUserProjectionSync(authDb, userId, {
|
|
229
|
+
displayName: 'Queued Sync',
|
|
230
|
+
createdAt: '2026-03-10T00:00:00.000Z',
|
|
231
|
+
updatedAt: '2026-03-10T00:00:00.000Z',
|
|
232
|
+
}, {
|
|
233
|
+
kv,
|
|
234
|
+
executionCtx: ctx,
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
await flushMicrotasks();
|
|
238
|
+
await Promise.all(pending);
|
|
239
|
+
expect(authDb._profiles.get(userId)).toMatchObject({ id: userId, displayName: 'Queued Sync' });
|
|
240
|
+
expect(JSON.parse(kv._store[`kv:users_public:${userId}`])).toMatchObject({ id: userId, displayName: 'Queued Sync' });
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
describe('deletePublicUserProjection', () => {
|
|
245
|
+
it('removes the auth projection and treats cache invalidation as best-effort', async () => {
|
|
246
|
+
const userId = nextUserId();
|
|
247
|
+
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
248
|
+
const authDb = createMockAuthDb([[
|
|
249
|
+
userId,
|
|
250
|
+
{
|
|
251
|
+
id: userId,
|
|
252
|
+
email: null,
|
|
253
|
+
displayName: 'To Delete',
|
|
254
|
+
avatarUrl: null,
|
|
255
|
+
role: 'user',
|
|
256
|
+
isAnonymous: 0,
|
|
257
|
+
createdAt: '2026-03-10T00:00:00.000Z',
|
|
258
|
+
updatedAt: '2026-03-10T00:00:00.000Z',
|
|
259
|
+
},
|
|
260
|
+
]]);
|
|
261
|
+
const kv = createMockKv({
|
|
262
|
+
deleteError: new Error('kv-delete-failed'),
|
|
263
|
+
initial: {
|
|
264
|
+
[`kv:users_public:${userId}`]: JSON.stringify({ id: userId, displayName: 'To Delete' }),
|
|
265
|
+
},
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
await expect(deletePublicUserProjection(authDb, userId, { kv })).resolves.toBeUndefined();
|
|
269
|
+
|
|
270
|
+
expect(authDb._profiles.has(userId)).toBe(false);
|
|
271
|
+
await flushMicrotasks();
|
|
272
|
+
expect(errorSpy).toHaveBeenCalled();
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
describe('invalidatePublicUserCache', () => {
|
|
277
|
+
it('clears the in-memory cache and removes the KV mirror', async () => {
|
|
278
|
+
const userId = nextUserId();
|
|
279
|
+
const authDb = createMockAuthDb([[
|
|
280
|
+
userId,
|
|
281
|
+
{
|
|
282
|
+
id: userId,
|
|
283
|
+
email: null,
|
|
284
|
+
displayName: 'Invalidate Cache',
|
|
285
|
+
avatarUrl: null,
|
|
286
|
+
role: 'user',
|
|
287
|
+
isAnonymous: 0,
|
|
288
|
+
createdAt: '2026-03-10T00:00:00.000Z',
|
|
289
|
+
updatedAt: '2026-03-10T00:00:00.000Z',
|
|
290
|
+
},
|
|
291
|
+
]]);
|
|
292
|
+
const kv = createMockKv();
|
|
293
|
+
|
|
294
|
+
await getPublicProfileWithCache(authDb, userId, { kv });
|
|
295
|
+
await flushMicrotasks();
|
|
296
|
+
authDb._profiles.delete(userId);
|
|
297
|
+
|
|
298
|
+
await invalidatePublicUserCache(userId, { kv });
|
|
299
|
+
|
|
300
|
+
const profileAfterInvalidation = await getPublicProfileWithCache(authDb, userId, { kv });
|
|
301
|
+
expect(profileAfterInvalidation).toBeNull();
|
|
302
|
+
expect(kv._store[`kv:users_public:${userId}`]).toBeUndefined();
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
describe('queuePublicUserProjectionDelete', () => {
|
|
307
|
+
it('queues cache invalidation and projection deletion in the background', async () => {
|
|
308
|
+
const userId = nextUserId();
|
|
309
|
+
const authDb = createMockAuthDb([[
|
|
310
|
+
userId,
|
|
311
|
+
{
|
|
312
|
+
id: userId,
|
|
313
|
+
email: null,
|
|
314
|
+
displayName: 'Queued Delete',
|
|
315
|
+
avatarUrl: null,
|
|
316
|
+
role: 'user',
|
|
317
|
+
isAnonymous: 0,
|
|
318
|
+
createdAt: '2026-03-10T00:00:00.000Z',
|
|
319
|
+
updatedAt: '2026-03-10T00:00:00.000Z',
|
|
320
|
+
},
|
|
321
|
+
]]);
|
|
322
|
+
const kv = createMockKv({
|
|
323
|
+
initial: {
|
|
324
|
+
[`kv:users_public:${userId}`]: JSON.stringify({ id: userId, displayName: 'Queued Delete' }),
|
|
325
|
+
},
|
|
326
|
+
});
|
|
327
|
+
const { ctx, pending } = createExecutionContext();
|
|
328
|
+
|
|
329
|
+
queuePublicUserProjectionDelete(authDb, userId, {
|
|
330
|
+
kv,
|
|
331
|
+
executionCtx: ctx,
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
await flushMicrotasks();
|
|
335
|
+
await Promise.all(pending);
|
|
336
|
+
expect(authDb._profiles.has(userId)).toBe(false);
|
|
337
|
+
expect(kv._store[`kv:users_public:${userId}`]).toBeUndefined();
|
|
338
|
+
});
|
|
339
|
+
});
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { defineConfig } from '@edge-base/shared';
|
|
3
|
+
import { setConfig } from '../lib/do-router.js';
|
|
4
|
+
import { OpenAPIHono, type HonoEnv } from '../lib/hono.js';
|
|
5
|
+
import { pushRoute } from '../routes/push.js';
|
|
6
|
+
import type { Env } from '../types.js';
|
|
7
|
+
|
|
8
|
+
const sendToTopicMock = vi.fn();
|
|
9
|
+
const broadcastMock = vi.fn();
|
|
10
|
+
const sendMock = vi.fn();
|
|
11
|
+
const subscribeTokenToTopicMock = vi.fn();
|
|
12
|
+
const unsubscribeTokenFromTopicMock = vi.fn();
|
|
13
|
+
|
|
14
|
+
vi.mock('../lib/push-provider.js', () => ({
|
|
15
|
+
createPushProvider: vi.fn(() => ({
|
|
16
|
+
send: sendMock,
|
|
17
|
+
sendToTopic: sendToTopicMock,
|
|
18
|
+
broadcast: broadcastMock,
|
|
19
|
+
subscribeTokenToTopic: subscribeTokenToTopicMock,
|
|
20
|
+
unsubscribeTokenFromTopic: unsubscribeTokenFromTopicMock,
|
|
21
|
+
})),
|
|
22
|
+
}));
|
|
23
|
+
|
|
24
|
+
function createApp() {
|
|
25
|
+
const app = new OpenAPIHono<HonoEnv>();
|
|
26
|
+
app.route('/api/push', pushRoute);
|
|
27
|
+
return app;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function createEnv(): Env {
|
|
31
|
+
return {
|
|
32
|
+
KV: {
|
|
33
|
+
put: vi.fn().mockResolvedValue(undefined),
|
|
34
|
+
},
|
|
35
|
+
} as unknown as Env;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
describe('Push handlers route integration', () => {
|
|
39
|
+
const afterSendCalls: Array<{ input: Record<string, unknown>; output: Record<string, unknown> }> = [];
|
|
40
|
+
|
|
41
|
+
beforeEach(() => {
|
|
42
|
+
afterSendCalls.length = 0;
|
|
43
|
+
sendMock.mockReset();
|
|
44
|
+
sendToTopicMock.mockReset().mockResolvedValue({ success: true });
|
|
45
|
+
broadcastMock.mockReset().mockResolvedValue({ success: true });
|
|
46
|
+
subscribeTokenToTopicMock.mockReset().mockResolvedValue({ success: true });
|
|
47
|
+
unsubscribeTokenFromTopicMock.mockReset().mockResolvedValue({ success: true });
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
afterEach(() => {
|
|
51
|
+
setConfig({});
|
|
52
|
+
vi.clearAllMocks();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('applies beforeSend transforms and fires afterSend for topic sends', async () => {
|
|
56
|
+
setConfig(defineConfig({
|
|
57
|
+
serviceKeys: {
|
|
58
|
+
keys: [
|
|
59
|
+
{
|
|
60
|
+
kid: 'push-root',
|
|
61
|
+
tier: 'root',
|
|
62
|
+
scopes: ['*'],
|
|
63
|
+
secretSource: 'inline',
|
|
64
|
+
inlineSecret: 'sk-test',
|
|
65
|
+
},
|
|
66
|
+
],
|
|
67
|
+
},
|
|
68
|
+
push: {
|
|
69
|
+
fcm: {
|
|
70
|
+
projectId: 'demo-project',
|
|
71
|
+
serviceAccount: '{}',
|
|
72
|
+
},
|
|
73
|
+
handlers: {
|
|
74
|
+
hooks: {
|
|
75
|
+
beforeSend: (_auth, input) => ({
|
|
76
|
+
...input,
|
|
77
|
+
topic: `prefixed-${input.topic}`,
|
|
78
|
+
payload: {
|
|
79
|
+
...(input.payload as Record<string, unknown>),
|
|
80
|
+
body: 'transformed-body',
|
|
81
|
+
},
|
|
82
|
+
}),
|
|
83
|
+
afterSend: (_auth, input, output) => {
|
|
84
|
+
afterSendCalls.push({
|
|
85
|
+
input: input as unknown as Record<string, unknown>,
|
|
86
|
+
output: output as unknown as Record<string, unknown>,
|
|
87
|
+
});
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
}));
|
|
93
|
+
|
|
94
|
+
const app = createApp();
|
|
95
|
+
const response = await app.request('/api/push/send-to-topic', {
|
|
96
|
+
method: 'POST',
|
|
97
|
+
headers: {
|
|
98
|
+
'Content-Type': 'application/json',
|
|
99
|
+
'X-EdgeBase-Service-Key': 'sk-test',
|
|
100
|
+
},
|
|
101
|
+
body: JSON.stringify({
|
|
102
|
+
topic: 'news',
|
|
103
|
+
payload: { title: 'hello' },
|
|
104
|
+
}),
|
|
105
|
+
}, createEnv());
|
|
106
|
+
|
|
107
|
+
expect(response.status).toBe(200);
|
|
108
|
+
expect(sendToTopicMock).toHaveBeenCalledWith(
|
|
109
|
+
'prefixed-news',
|
|
110
|
+
expect.objectContaining({
|
|
111
|
+
title: 'hello',
|
|
112
|
+
body: 'transformed-body',
|
|
113
|
+
}),
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
await Promise.resolve();
|
|
117
|
+
await Promise.resolve();
|
|
118
|
+
|
|
119
|
+
expect(afterSendCalls).toHaveLength(1);
|
|
120
|
+
expect(afterSendCalls[0].input).toMatchObject({
|
|
121
|
+
kind: 'topic',
|
|
122
|
+
topic: 'prefixed-news',
|
|
123
|
+
});
|
|
124
|
+
expect(afterSendCalls[0].output).toMatchObject({
|
|
125
|
+
raw: {
|
|
126
|
+
success: true,
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('rejects invalid beforeSend topic output', async () => {
|
|
132
|
+
setConfig(defineConfig({
|
|
133
|
+
serviceKeys: {
|
|
134
|
+
keys: [
|
|
135
|
+
{
|
|
136
|
+
kid: 'push-root',
|
|
137
|
+
tier: 'root',
|
|
138
|
+
scopes: ['*'],
|
|
139
|
+
secretSource: 'inline',
|
|
140
|
+
inlineSecret: 'sk-test',
|
|
141
|
+
},
|
|
142
|
+
],
|
|
143
|
+
},
|
|
144
|
+
push: {
|
|
145
|
+
fcm: {
|
|
146
|
+
projectId: 'demo-project',
|
|
147
|
+
serviceAccount: '{}',
|
|
148
|
+
},
|
|
149
|
+
handlers: {
|
|
150
|
+
hooks: {
|
|
151
|
+
beforeSend: () => ({
|
|
152
|
+
kind: 'topic',
|
|
153
|
+
topic: '',
|
|
154
|
+
payload: null as never,
|
|
155
|
+
}),
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
}));
|
|
160
|
+
|
|
161
|
+
const app = createApp();
|
|
162
|
+
const response = await app.request('/api/push/send-to-topic', {
|
|
163
|
+
method: 'POST',
|
|
164
|
+
headers: {
|
|
165
|
+
'Content-Type': 'application/json',
|
|
166
|
+
'X-EdgeBase-Service-Key': 'sk-test',
|
|
167
|
+
},
|
|
168
|
+
body: JSON.stringify({
|
|
169
|
+
topic: 'news',
|
|
170
|
+
payload: { title: 'hello' },
|
|
171
|
+
}),
|
|
172
|
+
}, createEnv());
|
|
173
|
+
|
|
174
|
+
expect(response.status).toBe(400);
|
|
175
|
+
await expect(response.json()).resolves.toMatchObject({
|
|
176
|
+
message: 'beforeSend must return topic and payload',
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { createPushProvider, resolveFcmEndpoints } from '../lib/push-provider.js';
|
|
3
|
+
import type { Env } from '../types.js';
|
|
4
|
+
|
|
5
|
+
describe('resolveFcmEndpoints', () => {
|
|
6
|
+
it('uses MOCK_FCM_BASE_URL from env when present', () => {
|
|
7
|
+
const endpoints = resolveFcmEndpoints(
|
|
8
|
+
'test-project',
|
|
9
|
+
{
|
|
10
|
+
oauth2TokenUrl: 'http://localhost:9099/token',
|
|
11
|
+
fcmSendUrl: 'http://localhost:9099/v1/projects/test-project/messages:send',
|
|
12
|
+
iidBaseUrl: 'http://localhost:9099',
|
|
13
|
+
},
|
|
14
|
+
{ MOCK_FCM_BASE_URL: 'http://host.docker.internal:9099/' } as Partial<Env> as Env,
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
expect(endpoints).toEqual({
|
|
18
|
+
oauth2TokenUrl: 'http://host.docker.internal:9099/token',
|
|
19
|
+
fcmSendUrl: 'http://host.docker.internal:9099/v1/projects/test-project/messages:send',
|
|
20
|
+
iidBaseUrl: 'http://host.docker.internal:9099',
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('falls back to configured endpoints when mock override is absent', () => {
|
|
25
|
+
const endpoints = resolveFcmEndpoints(
|
|
26
|
+
'test-project',
|
|
27
|
+
{
|
|
28
|
+
oauth2TokenUrl: 'https://oauth.example.com/token',
|
|
29
|
+
fcmSendUrl: 'https://push.example.com/send',
|
|
30
|
+
iidBaseUrl: 'https://iid.example.com',
|
|
31
|
+
},
|
|
32
|
+
{} as Partial<Env> as Env,
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
expect(endpoints).toEqual({
|
|
36
|
+
oauth2TokenUrl: 'https://oauth.example.com/token',
|
|
37
|
+
fcmSendUrl: 'https://push.example.com/send',
|
|
38
|
+
iidBaseUrl: 'https://iid.example.com',
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
afterEach(() => {
|
|
43
|
+
vi.restoreAllMocks();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('bypasses OAuth token exchange when MOCK_FCM_BASE_URL is configured', async () => {
|
|
47
|
+
const fetchMock = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
48
|
+
const url = String(input);
|
|
49
|
+
expect(url).toBe('https://mock.example/v1/projects/test-project/messages:send');
|
|
50
|
+
expect((init?.headers as Record<string, string>)?.Authorization).toBe('Bearer mock-access-token');
|
|
51
|
+
return new Response(JSON.stringify({ name: 'projects/test-project/messages/mock-1' }), {
|
|
52
|
+
status: 200,
|
|
53
|
+
headers: { 'content-type': 'application/json' },
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
57
|
+
|
|
58
|
+
const provider = createPushProvider(
|
|
59
|
+
{
|
|
60
|
+
fcm: {
|
|
61
|
+
projectId: 'test-project',
|
|
62
|
+
serviceAccount: JSON.stringify({
|
|
63
|
+
client_email: 'push-suite@test-project.iam.gserviceaccount.com',
|
|
64
|
+
private_key: '-----BEGIN PRIVATE KEY-----\\ninvalid-for-mock\\n-----END PRIVATE KEY-----\\n',
|
|
65
|
+
}),
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
{ MOCK_FCM_BASE_URL: 'https://mock.example/' } as Partial<Env> as Env,
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
const result = await provider?.send({
|
|
72
|
+
token: 'device-token',
|
|
73
|
+
platform: 'web',
|
|
74
|
+
payload: { title: 'Trace', body: 'Bypass mock OAuth' },
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
expect(result).toEqual({ success: true });
|
|
78
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
79
|
+
});
|
|
80
|
+
});
|