|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#31399] `getMetaItems({ type: 'seed' })`, the read behind |
| 5 | + * `GET /api/v1/meta/seed`, lists every stored seed row, each carrying its |
| 6 | + * row's name: the name `getMetaItem('seed', name)` already serves it under. |
| 7 | + * |
| 8 | + * A seed body states no name (`SeedSchema` declares none; the name lives on |
| 9 | + * the `sys_metadata` row's `name` column). The list merge |
| 10 | + * (`mergePackageAwareOverlay`) keyed every stored body by the body's own name, |
| 11 | + * so it skipped each seed: N stored rows listed 0 items while the by-name read |
| 12 | + * served all N. It now slots a body that states no name by its row's name. |
| 13 | + * |
| 14 | + * 1. N stored seed rows of a package give N listed items, named, one each, |
| 15 | + * on the environment's list and on the list scoped to the package, and |
| 16 | + * each listed body is the body `getMetaItem` serves under that name. |
| 17 | + * 2. A seed the package ships (the registry's item) and a stored row of the |
| 18 | + * same package and name give ONE slot, the stored row's: no duplicate. |
| 19 | + * Another package's seed of the same name keeps its own slot. |
| 20 | + * 3. The draft preview lists a draft seed row by its row's name too. |
| 21 | + * 4. Control: a stored body that states a name keeps it, whatever the row |
| 22 | + * says, and the stored body itself is not written by the list. |
| 23 | + */ |
| 24 | +// Pays the lint root's first load at collection, never inside a clocked case. |
| 25 | +import './lint-root-preload.testkit.js'; |
| 26 | +import { describe, expect, it } from 'vitest'; |
| 27 | +import { assertEngineFindOnePredicate } from '@objectstack/metadata-core'; |
| 28 | +import { ObjectStackProtocolImplementation } from './protocol.js'; |
| 29 | + |
| 30 | +const PKG = 'app.seedlist31399'; |
| 31 | +const OTHER = 'app.other31399'; |
| 32 | +const SEED_NAMES = ['s_one', 's_two', 's_three'] as const; |
| 33 | + |
| 34 | +interface StoredRow { |
| 35 | + id: string; |
| 36 | + type: string; |
| 37 | + name: string; |
| 38 | + package_id: string | null; |
| 39 | + state: string; |
| 40 | + metadata: string; |
| 41 | +} |
| 42 | + |
| 43 | +const seedBody = (marker: string) => ({ |
| 44 | + object: 'seedlist_thing', |
| 45 | + mode: 'upsert', |
| 46 | + externalId: 'name', |
| 47 | + records: [{ name: marker }], |
| 48 | +}); |
| 49 | + |
| 50 | +function seedRow(name: string, opts: { packageId?: string | null; state?: 'active' | 'draft'; body?: Record<string, unknown> } = {}): StoredRow { |
| 51 | + const state = opts.state ?? 'active'; |
| 52 | + return { |
| 53 | + id: `r_${name}_${state}_${opts.packageId ?? 'none'}`, |
| 54 | + type: 'seed', |
| 55 | + name, |
| 56 | + package_id: opts.packageId === undefined ? PKG : opts.packageId, |
| 57 | + state, |
| 58 | + metadata: JSON.stringify(opts.body ?? seedBody(`stored ${name}${state === 'draft' ? ' (draft)' : ''}`)), |
| 59 | + }; |
| 60 | +} |
| 61 | + |
| 62 | +const whereValueMatches = (actual: unknown, want: unknown): boolean => |
| 63 | + want !== null && typeof want === 'object' && Array.isArray((want as { $in?: unknown }).$in) |
| 64 | + ? (want as { $in: unknown[] }).$in.includes(actual) |
| 65 | + : actual === want; |
| 66 | + |
| 67 | +/** |
| 68 | + * The engine double: `find` / `findOne` over `sys_metadata` rows, and a |
| 69 | + * registry holding the seeds packages ship (`shipped`), answering a |
| 70 | + * package-scoped lookup with that package's items only. |
| 71 | + */ |
| 72 | +function harness(rows: StoredRow[], shipped: Array<Record<string, unknown>> = []) { |
| 73 | + const ofPackage = (packageId?: string) => shipped.filter((i) => !packageId || i._packageId === packageId); |
| 74 | + const registry = { |
| 75 | + getArtifactItem: () => undefined, |
| 76 | + getItem(asked: string, name: string, packageId?: string) { |
| 77 | + return asked === 'seed' ? ofPackage(packageId).find((i) => i.name === name) : undefined; |
| 78 | + }, |
| 79 | + listItems(asked: string, packageId?: string) { |
| 80 | + return asked === 'seed' ? structuredClone(ofPackage(packageId)) : []; |
| 81 | + }, |
| 82 | + getObject: () => undefined, |
| 83 | + registerObject: () => undefined, |
| 84 | + getPackage: () => undefined, |
| 85 | + isPackageDisabled: () => false, |
| 86 | + applyNavContributions: (app: unknown) => app, |
| 87 | + }; |
| 88 | + const matching = (where: Record<string, unknown>) => { |
| 89 | + for (const k of Object.keys(where)) { |
| 90 | + if (k.startsWith('$')) throw new Error(`[test double] unsupported WHERE combinator '${k}'`); |
| 91 | + } |
| 92 | + return rows.filter((r) => |
| 93 | + Object.entries(where).every(([k, v]) => v === undefined || whereValueMatches((r as unknown as Record<string, unknown>)[k], v)), |
| 94 | + ); |
| 95 | + }; |
| 96 | + const engine: any = { |
| 97 | + async find(table: string, opts?: { where?: Record<string, unknown>; limit?: number }) { |
| 98 | + if (table !== 'sys_metadata') return []; |
| 99 | + const matched = matching(opts?.where ?? {}); |
| 100 | + // `check:objectql-double-limit` — the caller's bound, applied after the filter. |
| 101 | + return opts?.limit === undefined ? matched : matched.slice(0, opts.limit); |
| 102 | + }, |
| 103 | + async findOne(table: string, opts?: { where?: Record<string, unknown> }) { |
| 104 | + // `check:engine-double-contract` — refuses what the real engine refuses. |
| 105 | + assertEngineFindOnePredicate(table, opts); |
| 106 | + if (table !== 'sys_metadata') return null; |
| 107 | + return matching(opts?.where ?? {})[0] ?? null; |
| 108 | + }, |
| 109 | + async insert() { |
| 110 | + return {}; |
| 111 | + }, |
| 112 | + registry, |
| 113 | + }; |
| 114 | + return new ObjectStackProtocolImplementation(engine, () => new Map(), 'env_1'); |
| 115 | +} |
| 116 | + |
| 117 | +const listed = async (protocol: ObjectStackProtocolImplementation, request: Record<string, unknown> = {}): Promise<any[]> => |
| 118 | + ((await protocol.getMetaItems({ type: 'seed', ...request } as any)) as any).items as any[]; |
| 119 | + |
| 120 | +const markerOf = (item: any): unknown => item?.records?.[0]?.name; |
| 121 | + |
| 122 | +describe('[#31399] §1 N stored seed rows list as N named items', () => { |
| 123 | + const rows = SEED_NAMES.map((name) => seedRow(name)); |
| 124 | + |
| 125 | + it('the environment\'s list: one item per stored row, each carrying its row\'s name', async () => { |
| 126 | + const items = await listed(harness(rows)); |
| 127 | + expect(items).toHaveLength(SEED_NAMES.length); |
| 128 | + expect(items.map((i) => i.name).sort()).toEqual([...SEED_NAMES].sort()); |
| 129 | + for (const item of items) { |
| 130 | + expect(item._packageId).toBe(PKG); |
| 131 | + expect(markerOf(item)).toBe(`stored ${item.name}`); |
| 132 | + } |
| 133 | + }); |
| 134 | + |
| 135 | + it('the list scoped to the package lists the same N items', async () => { |
| 136 | + const items = await listed(harness(rows), { packageId: PKG }); |
| 137 | + expect(items.map((i) => i.name).sort()).toEqual([...SEED_NAMES].sort()); |
| 138 | + }); |
| 139 | + |
| 140 | + it('each listed body is the body the by-name read serves under that name', async () => { |
| 141 | + const protocol = harness(rows); |
| 142 | + const items = await listed(protocol); |
| 143 | + for (const name of SEED_NAMES) { |
| 144 | + const read: any = await protocol.getMetaItem({ type: 'seed', name }); |
| 145 | + const item = items.find((i) => i.name === name); |
| 146 | + expect(markerOf(item)).toBe(markerOf(read.item)); |
| 147 | + expect(item.object).toBe(read.item.object); |
| 148 | + } |
| 149 | + }); |
| 150 | + |
| 151 | + it('`names` narrows the list by the row\'s name', async () => { |
| 152 | + const items = await listed(harness(rows), { names: ['s_two'] }); |
| 153 | + expect(items.map((i) => i.name)).toEqual(['s_two']); |
| 154 | + }); |
| 155 | +}); |
| 156 | + |
| 157 | +describe('[#31399] §2 no duplicate against a shipped seed of the same name', () => { |
| 158 | + it('the package\'s shipped seed and its stored row of the same name are ONE slot, the stored row\'s', async () => { |
| 159 | + const shipped = [{ name: 's_one', ...seedBody('shipped s_one'), _packageId: PKG }]; |
| 160 | + const items = await listed(harness(SEED_NAMES.map((name) => seedRow(name)), shipped)); |
| 161 | + expect(items.filter((i) => i.name === 's_one')).toHaveLength(1); |
| 162 | + expect(markerOf(items.find((i) => i.name === 's_one'))).toBe('stored s_one'); |
| 163 | + expect(items).toHaveLength(SEED_NAMES.length); |
| 164 | + }); |
| 165 | + |
| 166 | + it('another package\'s shipped seed of the same name keeps its own slot', async () => { |
| 167 | + const shipped = [{ name: 's_one', ...seedBody('shipped by other'), _packageId: OTHER }]; |
| 168 | + const items = await listed(harness([seedRow('s_one')], shipped)); |
| 169 | + const slots = items.filter((i) => i.name === 's_one').map((i) => [i._packageId, markerOf(i)]); |
| 170 | + expect(slots.sort()).toEqual([[OTHER, 'shipped by other'], [PKG, 'stored s_one']]); |
| 171 | + }); |
| 172 | +}); |
| 173 | + |
| 174 | +describe('[#31399] §3 the draft preview', () => { |
| 175 | + it('a draft seed row is previewed under its row\'s name', async () => { |
| 176 | + const rows = [seedRow('s_one'), seedRow('s_one', { state: 'draft' }), seedRow('s_new', { state: 'draft' })]; |
| 177 | + const items = await listed(harness(rows), { previewDrafts: true }); |
| 178 | + expect(items.filter((i) => i.name === 's_one').map(markerOf)).toEqual(['stored s_one (draft)']); |
| 179 | + expect(items.filter((i) => i.name === 's_new').map(markerOf)).toEqual(['stored s_new (draft)']); |
| 180 | + }); |
| 181 | +}); |
| 182 | + |
| 183 | +describe('[#31399] §4 CONTROL — a body that states a name keeps it; the stored body is not written', () => { |
| 184 | + it('a dashboard body naming itself is listed under its own name, not the row\'s', async () => { |
| 185 | + const row: StoredRow = { |
| 186 | + id: 'r_dash', type: 'dashboard', name: 'row_spelling', package_id: PKG, state: 'active', |
| 187 | + metadata: JSON.stringify({ name: 'body_spelling', label: 'Body' }), |
| 188 | + }; |
| 189 | + const res: any = await harness([row]).getMetaItems({ type: 'dashboard' }); |
| 190 | + expect((res.items as any[]).map((i) => i.name)).toEqual(['body_spelling']); |
| 191 | + }); |
| 192 | + |
| 193 | + it('listing does not write the stored seed body', async () => { |
| 194 | + const rows = [seedRow('s_one')]; |
| 195 | + const before = rows[0]!.metadata; |
| 196 | + await listed(harness(rows)); |
| 197 | + expect(rows[0]!.metadata).toBe(before); |
| 198 | + expect(JSON.parse(rows[0]!.metadata)).not.toHaveProperty('name'); |
| 199 | + }); |
| 200 | +}); |
0 commit comments