|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// FLOW SCREEN RECORD LINK (#25872, contract half #26304) — a screen flow that |
| 4 | +// creates records ends on a result screen that links them. Driven end to end |
| 5 | +// through a REAL boot: the real trigger and resume doors, real writes, and the |
| 6 | +// record read back through the data door the console's record page reads. |
| 7 | +// |
| 8 | +// The chain, each link asserted where it lands rather than as "no error": |
| 9 | +// |
| 10 | +// • the run writes an account and a contact, then pauses on the result |
| 11 | +// screen. Each of its two fields arrives typed `record_link`, naming its |
| 12 | +// object (`reference`) and carrying the id of the record THIS run created |
| 13 | +// (`defaultValue`, evaluated before the screen went on the wire), with no |
| 14 | +// `required` to meet; |
| 15 | +// • each id is a record of that object, readable through the data door, and |
| 16 | +// its display name (the object's `nameField`, ADR-0079) is the name the |
| 17 | +// author typed — what the console draws as the link text, linking to that |
| 18 | +// object's record page; |
| 19 | +// • a resume carrying a value under a link field's name is refused 400 |
| 20 | +// `INVALID_SCREEN_INPUT` naming the field, and the pause survives it; |
| 21 | +// • the resume that leaves the links out completes the run. |
| 22 | + |
| 23 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 24 | +import { bootStack, type VerifyStack } from '@objectstack/verify'; |
| 25 | +import { screenRecordLinkStack } from './fixtures/flow-screen-record-link-fixture.js'; |
| 26 | + |
| 27 | +const SYS = { isSystem: true } as const; |
| 28 | +const FLOW = 'srl_convert'; |
| 29 | + |
| 30 | +describe('objectstack verify FLOW SCREEN RECORD LINK (#25872): a result screen links the records the run created', () => { |
| 31 | + let stack: VerifyStack; |
| 32 | + let ql: any; |
| 33 | + let token: string; |
| 34 | + |
| 35 | + /** Trigger the flow; returns the paused result. */ |
| 36 | + async function start(): Promise<any> { |
| 37 | + const triggered = await stack.apiAs(token, 'POST', `/automation/${FLOW}/trigger`, { params: {} }); |
| 38 | + expect(triggered.status, await triggered.clone().text()).toBeLessThan(300); |
| 39 | + const tj = (await triggered.json()) as any; |
| 40 | + const result = tj.result ?? tj.data ?? tj; |
| 41 | + expect(result.status).toBe('paused'); |
| 42 | + expect(result.runId, 'no runId on the paused result').toBeTruthy(); |
| 43 | + return result; |
| 44 | + } |
| 45 | + |
| 46 | + const resume = (runId: string, inputs: Record<string, unknown>) => |
| 47 | + stack.apiAs(token, 'POST', `/automation/${FLOW}/runs/${runId}/resume`, { inputs }); |
| 48 | + |
| 49 | + /** Answer the first screen and return the paused result screen. */ |
| 50 | + async function convert(company: string, contactName: string): Promise<{ runId: string; screen: any }> { |
| 51 | + const { runId } = await start(); |
| 52 | + const answered = await resume(runId, { company, contact_name: contactName }); |
| 53 | + expect(answered.status, await answered.clone().text()).toBeLessThan(300); |
| 54 | + const aj = (await answered.json()) as any; |
| 55 | + const result = aj.result ?? aj.data ?? aj; |
| 56 | + expect(result.status, `the run did not pause on the result screen: ${JSON.stringify(result)}`).toBe('paused'); |
| 57 | + expect(result.screen?.nodeId).toBe('result'); |
| 58 | + return { runId: result.runId ?? runId, screen: result.screen }; |
| 59 | + } |
| 60 | + |
| 61 | + /** The record behind an id, read as the console's record page reads it. */ |
| 62 | + async function readRecord(objectName: string, id: string): Promise<Record<string, unknown>> { |
| 63 | + const read = await stack.apiAs(token, 'GET', `/data/${objectName}/${id}`); |
| 64 | + expect(read.status, await read.clone().text()).toBe(200); |
| 65 | + const body = (await read.json()) as any; |
| 66 | + return body.record ?? body; |
| 67 | + } |
| 68 | + |
| 69 | + beforeAll(async () => { |
| 70 | + stack = await bootStack(screenRecordLinkStack as never, { automation: true }); |
| 71 | + token = await stack.signIn(); |
| 72 | + ql = await stack.kernel.getServiceAsync('objectql'); |
| 73 | + }, 120_000); |
| 74 | + |
| 75 | + afterAll(async () => { |
| 76 | + await stack?.stop(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('pauses on a result screen whose two fields carry the created records, each named and linkable', async () => { |
| 80 | + const { screen } = await convert('Acme Corp', 'Ada Lovelace'); |
| 81 | + const fields: any[] = screen.fields ?? []; |
| 82 | + expect(fields.map((f) => [f.name, f.type, f.reference])).toEqual([ |
| 83 | + ['account_link', 'record_link', 'srl_account'], |
| 84 | + ['contact_link', 'record_link', 'srl_contact'], |
| 85 | + ]); |
| 86 | + expect(fields.every((f) => f.required !== true), 'a record link asks nothing of the user').toBe(true); |
| 87 | + |
| 88 | + // The ids are the records this run wrote — no other row could carry these names. |
| 89 | + const [accounts, contacts] = await Promise.all([ |
| 90 | + ql.find('srl_account', { where: { company_name: 'Acme Corp' }, context: SYS }), |
| 91 | + ql.find('srl_contact', { where: { full_name: 'Ada Lovelace' }, context: SYS }), |
| 92 | + ]); |
| 93 | + expect(accounts).toHaveLength(1); |
| 94 | + expect(contacts).toHaveLength(1); |
| 95 | + const [accountLink, contactLink] = fields; |
| 96 | + expect(String(accountLink.defaultValue)).toBe(String(accounts[0].id)); |
| 97 | + expect(String(contactLink.defaultValue)).toBe(String(contacts[0].id)); |
| 98 | + |
| 99 | + // What the link shows: each record's `nameField` value, read through the |
| 100 | + // data door the record page it links to reads. |
| 101 | + const account = await readRecord('srl_account', String(accountLink.defaultValue)); |
| 102 | + expect(account.company_name).toBe('Acme Corp'); |
| 103 | + const contact = await readRecord('srl_contact', String(contactLink.defaultValue)); |
| 104 | + expect(contact.full_name).toBe('Ada Lovelace'); |
| 105 | + }, 60_000); |
| 106 | + |
| 107 | + it('refuses a resume that carries a link field\'s value, keeps the pause, and completes on the clean resume', async () => { |
| 108 | + const { runId, screen } = await convert('Globex', 'Grace Hopper'); |
| 109 | + const createdAccountId = String(screen.fields[0].defaultValue); |
| 110 | + |
| 111 | + for (const inputs of [{ account_link: createdAccountId }, { contact_link: null }]) { |
| 112 | + const refused = await resume(runId, inputs); |
| 113 | + expect(refused.status, JSON.stringify(inputs)).toBe(400); |
| 114 | + const text = await refused.text(); |
| 115 | + expect(text).toContain('INVALID_SCREEN_INPUT'); |
| 116 | + expect(text).toContain(`Screen field "${Object.keys(inputs)[0]}" shows a record and takes no value`); |
| 117 | + } |
| 118 | + |
| 119 | + // Refused, not consumed: the resume that leaves the links out finishes the run. |
| 120 | + const done = await resume(runId, {}); |
| 121 | + expect(done.status, await done.clone().text()).toBeLessThan(300); |
| 122 | + const dj = (await done.json()) as any; |
| 123 | + const result = dj.result ?? dj.data ?? dj; |
| 124 | + expect(result.status ?? 'completed').toBe('completed'); |
| 125 | + expect(await ql.find('srl_account', { where: { company_name: 'Globex' }, context: SYS })).toHaveLength(1); |
| 126 | + }, 60_000); |
| 127 | +}); |
0 commit comments