|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#36409 — a view created Shared from the view dropdown is saved |
| 5 | + * and published in one act (`saveAndPublishRuntimeMetadata`), and its first |
| 6 | + * write lands in the app it was made in, exactly as `createRuntimeMetadata` |
| 7 | + * saves a new view (objectstack#35624, ruling C): the app's own package when |
| 8 | + * the server lists it writable, else the pair the ensure door names, else no |
| 9 | + * package at all. Only the SAVE names a package: the publish, and the discard |
| 10 | + * a refused publish runs, address the name's one holder (ADR-0146 D8). A type |
| 11 | + * other than `view` ignores the app. |
| 12 | + * |
| 13 | + * A REAL `MetadataClient` writes, so each pin reads the request the server |
| 14 | + * would receive; the two package doors answer through the page's `fetch`, |
| 15 | + * with the bodies `createIntoApp-35624` measured on examples/app-crm. |
| 16 | + */ |
| 17 | + |
| 18 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 19 | +import { MetadataClient } from '@object-ui/data-objectstack'; |
| 20 | +import { saveAndPublishRuntimeMetadata, viewEnvelope } from './runtime-metadata-persistence'; |
| 21 | + |
| 22 | +const json = (status: number, body: unknown) => |
| 23 | + new Response(JSON.stringify(body), { status, headers: { 'content-type': 'application/json' } }); |
| 24 | + |
| 25 | +function packageList(rows: Array<{ id: string; writable: boolean }>) { |
| 26 | + return { |
| 27 | + success: true, |
| 28 | + data: { |
| 29 | + packages: rows.map(({ id, writable }) => ({ |
| 30 | + manifest: { id, name: id, version: '4.0.0', type: 'app', scope: 'project' }, |
| 31 | + writable, |
| 32 | + })), |
| 33 | + total: rows.length, |
| 34 | + hasMore: false, |
| 35 | + }, |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +const ENSURED = { |
| 40 | + success: true, |
| 41 | + data: { |
| 42 | + basePackageId: 'com.example.crm', |
| 43 | + packageId: 'com.example.crm.custom', |
| 44 | + namespace: 'crm_custom', |
| 45 | + dependencyRange: '^4', |
| 46 | + created: true, |
| 47 | + writable: true, |
| 48 | + }, |
| 49 | +}; |
| 50 | + |
| 51 | +let pageCalls: string[] = []; |
| 52 | +let writes: Array<{ method: string; url: URL }> = []; |
| 53 | + |
| 54 | +function stubPageFetch(rows: Array<{ id: string; writable: boolean }>) { |
| 55 | + vi.stubGlobal( |
| 56 | + 'fetch', |
| 57 | + vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => { |
| 58 | + const url = String(input); |
| 59 | + pageCalls.push(`${(init?.method ?? 'GET').toUpperCase()} ${url}`); |
| 60 | + if (url === '/api/v1/packages') return json(200, packageList(rows)); |
| 61 | + if (url.endsWith('/ensure-customization')) return json(201, ENSURED); |
| 62 | + return json(404, { success: false, error: { code: 'RESOURCE_NOT_FOUND', message: url, httpStatus: 404 } }); |
| 63 | + }), |
| 64 | + ); |
| 65 | +} |
| 66 | + |
| 67 | +/** A real client; `refusePublish` answers the publish door `400 VALIDATION_ERROR`. */ |
| 68 | +function realClient(refusePublish = false): MetadataClient { |
| 69 | + return new MetadataClient({ |
| 70 | + baseUrl: 'http://localhost:3000', |
| 71 | + fetch: (async (input: RequestInfo | URL, init?: RequestInit) => { |
| 72 | + const url = new URL(String(input)); |
| 73 | + writes.push({ method: (init?.method ?? 'GET').toUpperCase(), url }); |
| 74 | + if (refusePublish && url.pathname.endsWith('/publish')) { |
| 75 | + return json(400, { success: false, error: { code: 'VALIDATION_ERROR', message: 'refused', httpStatus: 400 } }); |
| 76 | + } |
| 77 | + return json(200, { success: true, state: 'draft' }); |
| 78 | + }) as unknown as typeof fetch, |
| 79 | + }); |
| 80 | +} |
| 81 | + |
| 82 | +const env = viewEnvelope('crm_account', { type: 'grid', columns: ['name'] }, { name: 'key_accounts', label: 'Key accounts' }); |
| 83 | +const VIEW_PATH = '/api/v1/meta/view/crm_account.key_accounts'; |
| 84 | + |
| 85 | +/** Every write after the save: none of them may name a package. */ |
| 86 | +const packagesNamedAfterSave = () => writes.slice(1).map((w) => w.url.searchParams.get('package')); |
| 87 | + |
| 88 | +beforeEach(() => { |
| 89 | + pageCalls = []; |
| 90 | + writes = []; |
| 91 | +}); |
| 92 | + |
| 93 | +afterEach(() => { |
| 94 | + vi.unstubAllGlobals(); |
| 95 | + vi.restoreAllMocks(); |
| 96 | +}); |
| 97 | + |
| 98 | +describe('saveAndPublishRuntimeMetadata saves a new shared view into the app it was made in (objectstack#36409)', () => { |
| 99 | + it('a writable app package: the save names it; the publish names none, and no pair is asked for', async () => { |
| 100 | + stubPageFetch([{ id: 'com.acme.helpdesk', writable: true }]); |
| 101 | + |
| 102 | + await expect( |
| 103 | + saveAndPublishRuntimeMetadata('view', env.name, env, { |
| 104 | + metadataClient: realClient(), |
| 105 | + objectName: 'crm_account', |
| 106 | + app: { packageId: 'com.acme.helpdesk' }, |
| 107 | + }), |
| 108 | + ).resolves.toBe('published'); |
| 109 | + |
| 110 | + expect(pageCalls).toEqual(['GET /api/v1/packages']); |
| 111 | + expect(writes).toHaveLength(2); |
| 112 | + expect(writes[0]!.url.pathname).toBe(VIEW_PATH); |
| 113 | + expect(writes[0]!.url.searchParams.get('mode')).toBe('draft'); |
| 114 | + expect(writes[0]!.url.searchParams.get('package')).toBe('com.acme.helpdesk'); |
| 115 | + expect(writes[1]!.url.pathname).toBe(`${VIEW_PATH}/publish`); |
| 116 | + expect(packagesNamedAfterSave()).toEqual([null]); |
| 117 | + }); |
| 118 | + |
| 119 | + it('an installed (read-only) app package: the save names the pair the ensure door returns; the publish names none', async () => { |
| 120 | + stubPageFetch([{ id: 'com.example.crm', writable: false }]); |
| 121 | + |
| 122 | + await saveAndPublishRuntimeMetadata('view', env.name, env, { |
| 123 | + metadataClient: realClient(), |
| 124 | + objectName: 'crm_account', |
| 125 | + app: { packageId: 'com.example.crm' }, |
| 126 | + }); |
| 127 | + |
| 128 | + expect(pageCalls).toEqual([ |
| 129 | + 'GET /api/v1/packages', |
| 130 | + 'POST /api/v1/packages/com.example.crm/ensure-customization', |
| 131 | + ]); |
| 132 | + expect(writes[0]!.url.searchParams.get('package')).toBe('com.example.crm.custom'); |
| 133 | + expect(writes[1]!.url.pathname).toBe(`${VIEW_PATH}/publish`); |
| 134 | + expect(packagesNamedAfterSave()).toEqual([null]); |
| 135 | + }); |
| 136 | + |
| 137 | + it('an app whose package cannot be resolved (not listed), or no app: the save names none', async () => { |
| 138 | + stubPageFetch([{ id: 'com.other.app', writable: true }]); |
| 139 | + |
| 140 | + await saveAndPublishRuntimeMetadata('view', env.name, env, { metadataClient: realClient(), app: { packageId: 'com.example.crm' } }); |
| 141 | + await saveAndPublishRuntimeMetadata('view', env.name, env, { metadataClient: realClient(), app: { packageId: null } }); |
| 142 | + await saveAndPublishRuntimeMetadata('view', env.name, env, { metadataClient: realClient() }); |
| 143 | + |
| 144 | + expect(pageCalls).toEqual(['GET /api/v1/packages']); |
| 145 | + expect(writes.map((w) => w.url.searchParams.get('package'))).toEqual([null, null, null, null, null, null]); |
| 146 | + }); |
| 147 | + |
| 148 | + it('a refused publish: the discard of the draft this call wrote names no package either', async () => { |
| 149 | + stubPageFetch([{ id: 'com.example.crm', writable: false }]); |
| 150 | + |
| 151 | + await expect( |
| 152 | + saveAndPublishRuntimeMetadata('view', env.name, env, { |
| 153 | + metadataClient: realClient(true), |
| 154 | + app: { packageId: 'com.example.crm' }, |
| 155 | + }), |
| 156 | + ).rejects.toBeTruthy(); |
| 157 | + |
| 158 | + expect(writes[0]!.url.searchParams.get('package')).toBe('com.example.crm.custom'); |
| 159 | + expect(writes[1]!.url.pathname).toBe(`${VIEW_PATH}/publish`); |
| 160 | + expect(writes.length).toBeGreaterThanOrEqual(3); // the discard |
| 161 | + expect(packagesNamedAfterSave().every((p) => p === null)).toBe(true); |
| 162 | + }); |
| 163 | + |
| 164 | + it('a type other than view ignores the app: no package door is asked and the save names none', async () => { |
| 165 | + stubPageFetch([{ id: 'com.acme.helpdesk', writable: true }]); |
| 166 | + |
| 167 | + await saveAndPublishRuntimeMetadata('dashboard', 'ops_board', { name: 'ops_board' }, { |
| 168 | + metadataClient: realClient(), |
| 169 | + app: { packageId: 'com.acme.helpdesk' }, |
| 170 | + }); |
| 171 | + |
| 172 | + expect(pageCalls).toEqual([]); |
| 173 | + expect(writes.map((w) => w.url.searchParams.get('package'))).toEqual([null, null]); |
| 174 | + }); |
| 175 | +}); |
0 commit comments