|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The final six #3563 gap closures (PR-5): meta published/drafts/FSM and the |
| 5 | + * automation descriptor/status routes. With these, every should-be-SDK |
| 6 | + * dispatcher route has an SDK expression and the ledger's gap ratchet is 0. |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, it, expect, vi } from 'vitest'; |
| 10 | +import { ObjectStackClient } from './index'; |
| 11 | + |
| 12 | +function createMockClient(body: any, status = 200) { |
| 13 | + const fetchMock = vi.fn().mockResolvedValue({ |
| 14 | + ok: status >= 200 && status < 300, |
| 15 | + status, |
| 16 | + statusText: status === 200 ? 'OK' : 'Error', |
| 17 | + json: async () => body, |
| 18 | + headers: new Headers() |
| 19 | + }); |
| 20 | + const client = new ObjectStackClient({ |
| 21 | + baseUrl: 'http://localhost:3000', |
| 22 | + fetch: fetchMock |
| 23 | + }); |
| 24 | + return { client, fetchMock }; |
| 25 | +} |
| 26 | + |
| 27 | +describe('client.meta (#3563 PR-5)', () => { |
| 28 | + it('getPublished passes compound names through unencoded', async () => { |
| 29 | + const { client, fetchMock } = createMockClient({ success: true, data: { name: 'all_leads' } }); |
| 30 | + await client.meta.getPublished('lead', 'views/all_leads'); |
| 31 | + expect(String(fetchMock.mock.calls[0][0])).toBe( |
| 32 | + 'http://localhost:3000/api/v1/meta/lead/views/all_leads/published', |
| 33 | + ); |
| 34 | + }); |
| 35 | + |
| 36 | + it('listDrafts builds the packageId/type query and omits empties', async () => { |
| 37 | + const { client, fetchMock } = createMockClient({ success: true, data: { drafts: [] } }); |
| 38 | + await client.meta.listDrafts({ packageId: 'com.acme.crm', type: 'object' }); |
| 39 | + expect(String(fetchMock.mock.calls[0][0])).toBe( |
| 40 | + 'http://localhost:3000/api/v1/meta/_drafts?packageId=com.acme.crm&type=object', |
| 41 | + ); |
| 42 | + await client.meta.listDrafts(); |
| 43 | + expect(String(fetchMock.mock.calls[1][0])).toBe('http://localhost:3000/api/v1/meta/_drafts'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('getLegalNextStates hits the FSM route and forwards from=', async () => { |
| 47 | + const { client, fetchMock } = createMockClient({ |
| 48 | + success: true, |
| 49 | + data: { object: 'crm_lead', field: 'status', from: 'new', next: ['contacted'] }, |
| 50 | + }); |
| 51 | + const out = await client.meta.getLegalNextStates('crm_lead', 'status', 'new'); |
| 52 | + expect(String(fetchMock.mock.calls[0][0])).toBe( |
| 53 | + 'http://localhost:3000/api/v1/meta/objects/crm_lead/state/status?from=new', |
| 54 | + ); |
| 55 | + expect(out.next).toEqual(['contacted']); |
| 56 | + |
| 57 | + // Omitted `from` → no query; the server answers next: null. |
| 58 | + await client.meta.getLegalNextStates('crm_lead', 'status'); |
| 59 | + expect(String(fetchMock.mock.calls[1][0])).toBe( |
| 60 | + 'http://localhost:3000/api/v1/meta/objects/crm_lead/state/status', |
| 61 | + ); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +describe('client.automation descriptors (#3563 PR-5)', () => { |
| 66 | + it('listActions forwards the paradigm/source/category filters', async () => { |
| 67 | + const { client, fetchMock } = createMockClient({ success: true, data: { actions: [], total: 0 } }); |
| 68 | + await client.automation.listActions({ paradigm: 'flow', category: 'crud' }); |
| 69 | + expect(String(fetchMock.mock.calls[0][0])).toBe( |
| 70 | + 'http://localhost:3000/api/v1/automation/actions?paradigm=flow&category=crud', |
| 71 | + ); |
| 72 | + await client.automation.listActions(); |
| 73 | + expect(String(fetchMock.mock.calls[1][0])).toBe('http://localhost:3000/api/v1/automation/actions'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('listConnectors forwards the type filter', async () => { |
| 77 | + const { client, fetchMock } = createMockClient({ success: true, data: { connectors: [], total: 0 } }); |
| 78 | + await client.automation.listConnectors({ type: 'rest' }); |
| 79 | + expect(String(fetchMock.mock.calls[0][0])).toBe( |
| 80 | + 'http://localhost:3000/api/v1/automation/connectors?type=rest', |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it('getRuntimeStatus GETs the underscore-guarded _status route', async () => { |
| 85 | + const { client, fetchMock } = createMockClient({ |
| 86 | + success: true, |
| 87 | + data: { flows: [{ name: 'f1', enabled: true, bound: true }], total: 1 }, |
| 88 | + }); |
| 89 | + const out = await client.automation.getRuntimeStatus(); |
| 90 | + expect(String(fetchMock.mock.calls[0][0])).toBe('http://localhost:3000/api/v1/automation/_status'); |
| 91 | + expect(out.flows[0].bound).toBe(true); |
| 92 | + }); |
| 93 | +}); |
0 commit comments