|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#38573] `listedPackageBoundItems`: the merged list read's binding read keeps |
| 5 | + * the service's `list` footing. Before, the merge asked `listPackageBindings` |
| 6 | + * on every call, uncached, and on the dogfood stack a package move's flow |
| 7 | + * re-sync waited up to 281 ms for it inside the move's transaction, so the |
| 8 | + * moved flow was armed after the move answered (`package-move-in.dogfood`, |
| 9 | + * A3: 404 at the package's address). |
| 10 | + */ |
| 11 | +import { describe, expect, it, vi, type Mock } from 'vitest'; |
| 12 | +import type { MetadataPackageBinding } from '@objectstack/spec/contracts'; |
| 13 | +import { DEGRADED_LIST_BOUND_MS, LIST_BOUND_MS, listedPackageBoundItems } from './listed-package-bindings.js'; |
| 14 | + |
| 15 | +const ROW_PKG = 'com.example.policy'; |
| 16 | +const BODY_PKG = 'com.example.crm'; |
| 17 | + |
| 18 | +function service(opts: { revision?: number; degraded?: boolean; bindings?: boolean } = {}) { |
| 19 | + const items: MetadataPackageBinding[] = [ |
| 20 | + { name: 'differs', data: { name: 'differs', _packageId: BODY_PKG }, packageId: ROW_PKG, source: 'row' }, |
| 21 | + { name: 'legacy', data: { name: 'legacy', _packageId: BODY_PKG }, packageId: null, source: 'row' }, |
| 22 | + { name: 'shipped', data: { name: 'shipped', _packageId: BODY_PKG }, packageId: BODY_PKG, source: 'registration' }, |
| 23 | + ]; |
| 24 | + const svc: Record<string, unknown> = { |
| 25 | + list: vi.fn(async () => items.map((b) => b.data)), |
| 26 | + ...(opts.revision !== undefined ? { revision: opts.revision } : {}), |
| 27 | + }; |
| 28 | + if (opts.bindings !== false) { |
| 29 | + svc.listPackageBindings = vi.fn(async () => ({ items, degraded: opts.degraded ?? false, errors: [] })); |
| 30 | + } |
| 31 | + return svc as unknown as { |
| 32 | + list: Mock<(type: string) => Promise<unknown[]>>; |
| 33 | + listPackageBindings: Mock<(type: string) => Promise<{ items: MetadataPackageBinding[]; degraded: boolean; errors: string[] }>>; |
| 34 | + revision?: number; |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +const filed = (items: unknown[]) => (items as Array<{ name: string; _packageId?: string }>).map((i) => `${i.name}|${i._packageId ?? '(none)'}`); |
| 39 | + |
| 40 | +describe('[#38573] listedPackageBoundItems', () => { |
| 41 | + it("files a row's copy under its column, a legacy row under none, and a registration as registered", async () => { |
| 42 | + expect(filed(await listedPackageBoundItems(service({ revision: 1 }), 'skill'))).toEqual([ |
| 43 | + `differs|${ROW_PKG}`, 'legacy|(none)', `shipped|${BODY_PKG}`, |
| 44 | + ]); |
| 45 | + }); |
| 46 | + |
| 47 | + it("reads the bindings once while the service's revision stands and the list bound has not passed (BEFORE: once per call)", async () => { |
| 48 | + const svc = service({ revision: 7 }); |
| 49 | + let now = 1_000; |
| 50 | + for (let i = 0; i < 3; i++) await listedPackageBoundItems(svc, 'skill', () => now); |
| 51 | + now += LIST_BOUND_MS - 1; |
| 52 | + expect(filed(await listedPackageBoundItems(svc, 'skill', () => now))).toEqual([ |
| 53 | + `differs|${ROW_PKG}`, 'legacy|(none)', `shipped|${BODY_PKG}`, |
| 54 | + ]); |
| 55 | + expect(svc.listPackageBindings).toHaveBeenCalledTimes(1); |
| 56 | + expect(svc.list).not.toHaveBeenCalled(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('reads again once the revision moves, per type', async () => { |
| 60 | + const svc = service({ revision: 1 }); |
| 61 | + await listedPackageBoundItems(svc, 'skill'); |
| 62 | + await listedPackageBoundItems(svc, 'agent'); |
| 63 | + svc.revision = 2; |
| 64 | + await listedPackageBoundItems(svc, 'skill'); |
| 65 | + expect(svc.listPackageBindings.mock.calls.map((c) => c[0])).toEqual(['skill', 'agent', 'skill']); |
| 66 | + }); |
| 67 | + |
| 68 | + it("reads again once the service's list bound passes: 30 s, 2 s for a degraded answer", async () => { |
| 69 | + let now = 0; |
| 70 | + const healthy = service({ revision: 1 }); |
| 71 | + await listedPackageBoundItems(healthy, 'skill', () => now); |
| 72 | + now = LIST_BOUND_MS; |
| 73 | + await listedPackageBoundItems(healthy, 'skill', () => now); |
| 74 | + expect(healthy.listPackageBindings).toHaveBeenCalledTimes(2); |
| 75 | + |
| 76 | + now = 0; |
| 77 | + const degraded = service({ revision: 1, degraded: true }); |
| 78 | + await listedPackageBoundItems(degraded, 'skill', () => now); |
| 79 | + now = DEGRADED_LIST_BOUND_MS - 1; |
| 80 | + await listedPackageBoundItems(degraded, 'skill', () => now); |
| 81 | + now = DEGRADED_LIST_BOUND_MS; |
| 82 | + await listedPackageBoundItems(degraded, 'skill', () => now); |
| 83 | + expect(degraded.listPackageBindings).toHaveBeenCalledTimes(2); |
| 84 | + }); |
| 85 | + |
| 86 | + it('keeps no answer the revision moved under while it was read', async () => { |
| 87 | + const svc = service({ revision: 1 }); |
| 88 | + const read = svc.listPackageBindings.getMockImplementation()!; |
| 89 | + svc.listPackageBindings.mockImplementationOnce(async (type: string) => { |
| 90 | + svc.revision = 2; |
| 91 | + return read(type); |
| 92 | + }); |
| 93 | + await listedPackageBoundItems(svc, 'skill'); |
| 94 | + await listedPackageBoundItems(svc, 'skill'); |
| 95 | + expect(svc.listPackageBindings).toHaveBeenCalledTimes(2); |
| 96 | + }); |
| 97 | + |
| 98 | + it('keeps no failed read: the next call reads again', async () => { |
| 99 | + const svc = service({ revision: 1 }); |
| 100 | + svc.listPackageBindings.mockRejectedValueOnce(new Error('sys_metadata unreachable')); |
| 101 | + await expect(listedPackageBoundItems(svc, 'skill')).rejects.toThrow('sys_metadata unreachable'); |
| 102 | + expect(filed(await listedPackageBoundItems(svc, 'skill'))).toContain(`differs|${ROW_PKG}`); |
| 103 | + expect(svc.listPackageBindings).toHaveBeenCalledTimes(2); |
| 104 | + }); |
| 105 | + |
| 106 | + it('a service with no revision is read every time: no counter is guessed', async () => { |
| 107 | + const svc = service(); |
| 108 | + await listedPackageBoundItems(svc, 'skill'); |
| 109 | + await listedPackageBoundItems(svc, 'skill'); |
| 110 | + expect(svc.listPackageBindings).toHaveBeenCalledTimes(2); |
| 111 | + }); |
| 112 | + |
| 113 | + it("a service with no listPackageBindings answers its list bodies as they are", async () => { |
| 114 | + const svc = service({ revision: 1, bindings: false }); |
| 115 | + expect(filed(await listedPackageBoundItems(svc, 'skill'))).toEqual([ |
| 116 | + `differs|${BODY_PKG}`, `legacy|${BODY_PKG}`, `shipped|${BODY_PKG}`, |
| 117 | + ]); |
| 118 | + expect(svc.list).toHaveBeenCalledTimes(1); |
| 119 | + }); |
| 120 | +}); |
0 commit comments