|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `client.packages` lifecycle methods — #3563 PR-4 gap closures. The eleven |
| 5 | + * routes beyond install/enable (ADR-0033 drafts, ADR-0067 commits, ADR-0070 |
| 6 | + * portability) existed server-side with no SDK expression; Studio reached |
| 7 | + * them via raw fetch. |
| 8 | + */ |
| 9 | + |
| 10 | +import { describe, it, expect, vi } from 'vitest'; |
| 11 | +import { ObjectStackClient } from './index'; |
| 12 | + |
| 13 | +function createMockClient(body: any, status = 200) { |
| 14 | + const fetchMock = vi.fn().mockResolvedValue({ |
| 15 | + ok: status >= 200 && status < 300, |
| 16 | + status, |
| 17 | + statusText: status === 200 ? 'OK' : 'Error', |
| 18 | + json: async () => body, |
| 19 | + headers: new Headers() |
| 20 | + }); |
| 21 | + const client = new ObjectStackClient({ |
| 22 | + baseUrl: 'http://localhost:3000', |
| 23 | + fetch: fetchMock |
| 24 | + }); |
| 25 | + return { client, fetchMock }; |
| 26 | +} |
| 27 | + |
| 28 | +const BASE = 'http://localhost:3000/api/v1/packages'; |
| 29 | + |
| 30 | +describe('client.packages lifecycle (#3563 PR-4)', () => { |
| 31 | + it('update PATCHes the manifest fields to /packages/:id', async () => { |
| 32 | + const { client, fetchMock } = createMockClient({ success: true, data: { id: 'com.acme.crm' } }); |
| 33 | + await client.packages.update('com.acme.crm', { name: 'Acme CRM', version: '2.0.0' }); |
| 34 | + const [url, init] = fetchMock.mock.calls[0]; |
| 35 | + expect(String(url)).toBe(`${BASE}/com.acme.crm`); |
| 36 | + expect(init.method).toBe('PATCH'); |
| 37 | + expect(JSON.parse(init.body)).toEqual({ name: 'Acme CRM', version: '2.0.0' }); |
| 38 | + }); |
| 39 | + |
| 40 | + it.each([ |
| 41 | + ['publish', (c: ObjectStackClient) => c.packages.publish('p1'), 'POST', `${BASE}/p1/publish`, {}], |
| 42 | + ['publishDrafts', (c: ObjectStackClient) => c.packages.publishDrafts('p1'), 'POST', `${BASE}/p1/publish-drafts`, {}], |
| 43 | + ['discardDrafts', (c: ObjectStackClient) => c.packages.discardDrafts('p1', { actor: 'admin' }), 'POST', `${BASE}/p1/discard-drafts`, { actor: 'admin' }], |
| 44 | + ['revert', (c: ObjectStackClient) => c.packages.revert('p1'), 'POST', `${BASE}/p1/revert`, undefined], |
| 45 | + ['adoptOrphans', (c: ObjectStackClient) => c.packages.adoptOrphans('p1'), 'POST', `${BASE}/p1/adopt-orphans`, {}], |
| 46 | + ] as const)('%s hits its lifecycle route', async (_name, call, method, url, expectedBody) => { |
| 47 | + const { client, fetchMock } = createMockClient({ success: true, data: { ok: true } }); |
| 48 | + await call(client); |
| 49 | + const [gotUrl, init] = fetchMock.mock.calls[0]; |
| 50 | + expect(String(gotUrl)).toBe(url); |
| 51 | + expect(init.method).toBe(method); |
| 52 | + if (expectedBody !== undefined) expect(JSON.parse(init.body)).toEqual(expectedBody); |
| 53 | + else expect(init.body).toBeUndefined(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('listCommits GETs the ADR-0067 timeline', async () => { |
| 57 | + const { client, fetchMock } = createMockClient({ success: true, data: { commits: [{ id: 'c1' }] } }); |
| 58 | + const out = await client.packages.listCommits('p1'); |
| 59 | + expect(String(fetchMock.mock.calls[0][0])).toBe(`${BASE}/p1/commits`); |
| 60 | + expect(out.commits).toHaveLength(1); |
| 61 | + }); |
| 62 | + |
| 63 | + it('revertCommit POSTs to the commit-scoped subroute, URL-encoding both ids', async () => { |
| 64 | + const { client, fetchMock } = createMockClient({ success: true, data: { ok: true } }); |
| 65 | + await client.packages.revertCommit('p one', 'c/1'); |
| 66 | + expect(String(fetchMock.mock.calls[0][0])).toBe(`${BASE}/p%20one/commits/c%2F1/revert`); |
| 67 | + expect(fetchMock.mock.calls[0][1].method).toBe('POST'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('rollback sends the required commitId in the body', async () => { |
| 71 | + const { client, fetchMock } = createMockClient({ success: true, data: { ok: true } }); |
| 72 | + await client.packages.rollback('p1', 'c9', { actor: 'admin' }); |
| 73 | + expect(String(fetchMock.mock.calls[0][0])).toBe(`${BASE}/p1/rollback`); |
| 74 | + expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({ commitId: 'c9', actor: 'admin' }); |
| 75 | + }); |
| 76 | + |
| 77 | + it('export GETs the portable manifest', async () => { |
| 78 | + const { client, fetchMock } = createMockClient({ success: true, data: { id: 'p1', objects: [] } }); |
| 79 | + const manifest = await client.packages.export('p1'); |
| 80 | + expect(String(fetchMock.mock.calls[0][0])).toBe(`${BASE}/p1/export`); |
| 81 | + expect(manifest.id).toBe('p1'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('duplicate requires targetPackageId and forwards the renaming options', async () => { |
| 85 | + const { client, fetchMock } = createMockClient({ success: true, data: { ok: true } }); |
| 86 | + await client.packages.duplicate('p1', 'p2', { targetName: 'Copy', targetNamespace: 'copy' }); |
| 87 | + expect(String(fetchMock.mock.calls[0][0])).toBe(`${BASE}/p1/duplicate`); |
| 88 | + expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({ |
| 89 | + targetPackageId: 'p2', |
| 90 | + targetName: 'Copy', |
| 91 | + targetNamespace: 'copy', |
| 92 | + }); |
| 93 | + }); |
| 94 | +}); |
0 commit comments