|
11 | 11 | * which this kernel does not have, and the catch-all's `dispatch()` had no |
12 | 12 | * domain for them. The first pin here asserts no door answers a route miss |
13 | 13 | * any more, door by door. (The card's hosted reading spelled the miss |
14 | | - * `ENDPOINT_NOT_FOUND`, the transport's own; this tree does not explain that |
15 | | - * spelling, so the pin refuses both.) |
| 14 | + * `ENDPOINT_NOT_FOUND`, the transport's own: item 4 below explains it.) |
16 | 15 | * |
17 | 16 | * What this pins: |
18 | 17 | * 1. on the hosted shape, a member removes their own reaction, resolves and |
|
22 | 21 | * anonymous refusal and a refused body — because the member runs the very |
23 | 22 | * registration the self-hosted mount runs; |
24 | 23 | * 3. a hosted kernel without plugin-audit answers a typed |
25 | | - * `501 NOT_IMPLEMENTED`, never a route miss. |
| 24 | + * `501 NOT_IMPLEMENTED`, never a route miss; |
| 25 | + * 4. on the `objectstack serve` / `objectstack dev` host shape (HonoServerPlugin |
| 26 | + * + DispatcherPlugin on the host, a kernel-resolver, NO catch-all) the same |
| 27 | + * member answers, with the same bytes, through DispatcherPlugin's fallback. |
| 28 | + * Without that fallback arm this shape answered the transport's own |
| 29 | + * `{"success":false,"error":{"code":"ENDPOINT_NOT_FOUND","message":"Not found"}}`, |
| 30 | + * byte for byte the card's hosted reading (cloud's local rig runs |
| 31 | + * `apps/objectos` under `objectstack dev`). |
26 | 32 | * |
27 | 33 | * The domain's own claim, gates and faults are pinned on the real dispatcher |
28 | 34 | * in `packages/runtime/src/domains/comments.test.ts`. |
29 | 35 | */ |
30 | 36 |
|
31 | 37 | import { describe, it, expect, afterEach } from 'vitest'; |
32 | | -import { ObjectKernel } from '@objectstack/core'; |
| 38 | +import { ObjectKernel, LiteKernel } from '@objectstack/core'; |
33 | 39 | import type { Plugin, PluginContext } from '@objectstack/core'; |
34 | 40 | import { ObjectQLPlugin, type ObjectQL } from '@objectstack/objectql'; |
35 | 41 | import { HonoServerPlugin } from '@objectstack/plugin-hono-server'; |
36 | 42 | import { SqliteWasmDriver } from '@objectstack/driver-sqlite-wasm'; |
37 | 43 | import { AuditPlugin } from '@objectstack/plugin-audit'; |
38 | 44 | import { createHonoApp } from '@objectstack/hono'; |
| 45 | +import { createDispatcherPlugin } from '@objectstack/runtime'; |
39 | 46 | import type { IHttpServer } from '@objectstack/spec/contracts'; |
40 | 47 | import { installDefaultOrganization, DEFAULT_ORGANIZATION_ID } from './default-organization.testkit.js'; |
41 | 48 |
|
@@ -284,3 +291,97 @@ describe('[#36469] a hosted kernel without plugin-audit answers a typed 501, nev |
284 | 291 | } |
285 | 292 | }); |
286 | 293 | }); |
| 294 | + |
| 295 | +/** |
| 296 | + * [#36469] The `objectstack serve` / `objectstack dev` host shape: the HOST |
| 297 | + * kernel has `HonoServerPlugin` and `DispatcherPlugin` but NO `createHonoApp` |
| 298 | + * catch-all, and a `kernel-resolver` places each request on a per-environment |
| 299 | + * kernel that runs the real AuditPlugin with no `http.server` (cloud's local |
| 300 | + * rig runs this way, `apps/objectos` under `objectstack dev`). Before |
| 301 | + * `DispatcherPlugin`'s fallback handed the door shapes on, every request below |
| 302 | + * answered the transport's own `404 ENDPOINT_NOT_FOUND`: the card's literal |
| 303 | + * hosted reading. |
| 304 | + */ |
| 305 | +async function serveShapedHost() { |
| 306 | + const tenant = await hostedDoor(); |
| 307 | + // A host kernel with no data engine of its own, as the runtime's |
| 308 | + // multi-tenant fallback tests compose one: the doors' data is the tenant's. |
| 309 | + const host = new LiteKernel({ logger: { level: 'silent' } } as any); |
| 310 | + liveKernels.push(host as unknown as ObjectKernel); |
| 311 | + await host.use(new HonoServerPlugin({ port: 0, cors: false })); |
| 312 | + await host.use(new TestAuth()); |
| 313 | + await host.use({ |
| 314 | + name: 'test.comment-doors-kernel-resolver', |
| 315 | + version: '1.0.0', |
| 316 | + type: 'standard', |
| 317 | + init: async (ctx: PluginContext) => { |
| 318 | + ctx.registerService('kernel-resolver', { |
| 319 | + resolveKernel: async (context: any) => { |
| 320 | + const headers = context?.request?.headers; |
| 321 | + const envId = typeof headers?.get === 'function' ? headers.get(ENVIRONMENT) : headers?.[ENVIRONMENT]; |
| 322 | + if (envId !== TENANT_ENV) return undefined; |
| 323 | + context.environmentId = TENANT_ENV; |
| 324 | + return tenant.kernel; |
| 325 | + }, |
| 326 | + }); |
| 327 | + }, |
| 328 | + } as Plugin); |
| 329 | + await host.use(createDispatcherPlugin({ prefix: PREFIX, securityHeaders: false }) as any); |
| 330 | + await host.bootstrap(); |
| 331 | + const base = `http://127.0.0.1:${host.getService<IHttpServer>('http.server').getPort!()}`; |
| 332 | + const send = (path: string, init: RequestInit = {}) => |
| 333 | + fetch(`${base}${path}`, { ...init, headers: { ...(init.headers as Record<string, string>), [ENVIRONMENT]: TENANT_ENV } }); |
| 334 | + return { ...tenant, send }; |
| 335 | +} |
| 336 | + |
| 337 | +const ENVIRONMENT = 'x-environment-id'; |
| 338 | +const TENANT_ENV = 'env_tenant'; |
| 339 | + |
| 340 | +describe('[#36469] the comment doors answer on an `objectstack serve`-shaped multi-tenant host (no catch-all)', () => { |
| 341 | + it('a member removes their own reaction: 200, and the row is gone on the tenant', async () => { |
| 342 | + const door = await serveShapedHost(); |
| 343 | + const { reactionId } = await door.seed(); |
| 344 | + |
| 345 | + const res = await door.send(`${PREFIX}/comments/reactions/${reactionId}`, as(ALICE, { method: 'DELETE' })); |
| 346 | + |
| 347 | + expect(await res.clone().text()).not.toContain('ENDPOINT_NOT_FOUND'); |
| 348 | + expect(res.status, await res.clone().text()).toBe(200); |
| 349 | + expect(await res.json()).toEqual({ success: true, data: { id: reactionId, deleted: true } }); |
| 350 | + expect(await door.stored(REACTION, reactionId)).toBeNull(); |
| 351 | + }); |
| 352 | + |
| 353 | + it('a member resolves and reopens a thread, then removes their own comment', async () => { |
| 354 | + const door = await serveShapedHost(); |
| 355 | + const { commentId, reactionId } = await door.seed(); |
| 356 | + const resolution = `${PREFIX}/comments/${commentId}/resolution`; |
| 357 | + |
| 358 | + const resolved = await door.send(resolution, as(ALICE, { method: 'PUT', body: JSON.stringify({ resolved: true }) })); |
| 359 | + expect(resolved.status, await resolved.clone().text()).toBe(200); |
| 360 | + expect((await door.stored(COMMENT, commentId) as any)?.is_resolved).toBeTruthy(); |
| 361 | + |
| 362 | + const reopened = await door.send(resolution, as(ALICE, { method: 'PUT', body: JSON.stringify({ resolved: false }) })); |
| 363 | + expect(reopened.status, await reopened.clone().text()).toBe(200); |
| 364 | + expect((await door.stored(COMMENT, commentId) as any)?.is_resolved).toBeFalsy(); |
| 365 | + |
| 366 | + await door.send(`${PREFIX}/comments/reactions/${reactionId}`, as(ALICE, { method: 'DELETE' })); |
| 367 | + const removed = await door.send(`${PREFIX}/comments/${commentId}`, as(ALICE, { method: 'DELETE' })); |
| 368 | + expect(removed.status, await removed.clone().text()).toBe(200); |
| 369 | + expect(await door.stored(COMMENT, commentId)).toBeNull(); |
| 370 | + }); |
| 371 | + |
| 372 | + it('the anonymous refusal and the refused body are byte-identical to the self-hosted mount', async () => { |
| 373 | + const host = await serveShapedHost(); |
| 374 | + const selfHosted = await selfHostedDoor(); |
| 375 | + const cases: Array<[label: string, path: string, init: RequestInit]> = [ |
| 376 | + ['anonymous unreact', `${PREFIX}/comments/reactions/rx_missing`, as(undefined, { method: 'DELETE' })], |
| 377 | + ['a body that is not JSON', `${PREFIX}/comments/c_missing/resolution`, as(ALICE, { method: 'PUT', body: '{resolved:' })], |
| 378 | + ['a body that is not exactly { resolved }', `${PREFIX}/comments/c_missing/resolution`, as(ALICE, { method: 'PUT', body: '{"resolved":"yes"}' })], |
| 379 | + ]; |
| 380 | + for (const [label, path, init] of cases) { |
| 381 | + const viaHost = await snapshot(await host.send(path, init)); |
| 382 | + const viaMount = await snapshot(await selfHosted.send(path, init)); |
| 383 | + expect(viaHost.status, label).not.toBe(404); |
| 384 | + expect(viaHost, label).toEqual(viaMount); |
| 385 | + } |
| 386 | + }); |
| 387 | +}); |
0 commit comments