|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `sys_queue`'s record page (`sys-queue.page.ts`): an organization's queue |
| 5 | + * lists its members, keyed by the queue's NAME, with "New" pre-filled. |
| 6 | + * |
| 7 | + * No published page keyed a related list on `relationshipValueField: 'name'` |
| 8 | + * before this one, so both halves of what the console does with the key are |
| 9 | + * pinned here, read off the page and never retyped: |
| 10 | + * |
| 11 | + * - the FILTER: the list reads `sys_queue_member` where |
| 12 | + * `{ [relationshipField]: queue[relationshipValueField] }`; |
| 13 | + * - the "New" PREFILL: the member form opens with `relationshipField` set to |
| 14 | + * that same value, and "New" is not withheld (no authored `actions: []`). |
| 15 | + * |
| 16 | + * Both are measured on a REAL engine (ObjectQL + SqlDriver over in-memory |
| 17 | + * SQLite) carrying the shipped `sys_queue` / `sys_queue_member`, and the row |
| 18 | + * the prefilled form writes is the row the ownership-queue service expands as |
| 19 | + * a member: the list and the service agree on who is in the queue. |
| 20 | + */ |
| 21 | + |
| 22 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 23 | +import { ObjectQL } from '@objectstack/objectql'; |
| 24 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 25 | +import { SysOrganization } from '@objectstack/platform-objects/identity'; |
| 26 | +import { PageSchema, ComponentPropsMap } from '@objectstack/spec/ui'; |
| 27 | +import { SysQueue, SysQueueMember } from './objects/index.js'; |
| 28 | +import { SysQueueDetailPage } from './sys-queue.page.js'; |
| 29 | +import { OwnershipQueueService } from './ownership-queue-service.js'; |
| 30 | +import { SharingTranslations } from './translations/index.js'; |
| 31 | +import { sharingPageTranslations } from './translations/pages.js'; |
| 32 | + |
| 33 | +type AnyRec = Record<string, any>; |
| 34 | + |
| 35 | +const SYS = { isSystem: true, userId: 'usr_admin' } as any; |
| 36 | +const ORG = 'org_qa'; |
| 37 | + |
| 38 | +const PAGE = SysQueueDetailPage as AnyRec; |
| 39 | +const COMPONENTS = (PAGE.regions?.[0]?.components ?? []) as AnyRec[]; |
| 40 | +const nodeOf = (type: string): AnyRec => COMPONENTS.find((c) => c.type === type) ?? {}; |
| 41 | +const MEMBERS = nodeOf('record:related_list').properties as AnyRec; |
| 42 | + |
| 43 | +describe('sys_queue record page — the page', () => { |
| 44 | + it('is the default full record page of sys_queue, and parses as a page', () => { |
| 45 | + expect(PAGE).toMatchObject({ name: 'sys_queue_detail', type: 'record', object: 'sys_queue', kind: 'full', isDefault: true }); |
| 46 | + const parsed = PageSchema.safeParse(PAGE); |
| 47 | + expect(parsed.success, JSON.stringify(parsed.error?.issues ?? [])).toBe(true); |
| 48 | + }); |
| 49 | + |
| 50 | + it('every node\'s props parse against its own component props row', () => { |
| 51 | + const map = ComponentPropsMap as Record<string, { safeParse: (v: unknown) => { success: boolean } }>; |
| 52 | + const bad = COMPONENTS.filter((c) => !map[c.type]?.safeParse(c.properties ?? {}).success).map((c) => c.type); |
| 53 | + expect(bad).toEqual([]); |
| 54 | + }); |
| 55 | + |
| 56 | + it('authors the header, the strip, the details, the members and the discussion, in that order', () => { |
| 57 | + expect(COMPONENTS.map((c) => c.type)).toEqual([ |
| 58 | + 'page:header', 'record:highlights', 'record:details', 'record:related_list', 'record:discussion', |
| 59 | + ]); |
| 60 | + // The strip is the object's highlightFields minus the title field. |
| 61 | + expect(nodeOf('record:highlights').properties.fields) |
| 62 | + .toEqual((SysQueue.highlightFields ?? []).filter((f: string) => f !== SysQueue.nameField)); |
| 63 | + // The details draw every field the queue declares. |
| 64 | + expect([...nodeOf('record:details').properties.fields].sort()).toEqual(Object.keys(SysQueue.fields).sort()); |
| 65 | + }); |
| 66 | + |
| 67 | + it('the members list is sys_queue_member keyed by the queue NAME, and "New" is not withheld', () => { |
| 68 | + expect(MEMBERS).toMatchObject({ objectName: 'sys_queue_member', relationshipField: 'queue', relationshipValueField: 'name' }); |
| 69 | + expect(MEMBERS.actions).toBeUndefined(); |
| 70 | + // The two sides of the key: the member's `queue` is text holding a name, |
| 71 | + // and the queue's `name` is the identity every reference uses. |
| 72 | + expect((SysQueueMember.fields as AnyRec).queue).toMatchObject({ type: 'text', required: true }); |
| 73 | + expect((SysQueue.indexes ?? []).some((i: AnyRec) => i.unique && i.fields?.join() === 'name')).toBe(true); |
| 74 | + const memberFields = Object.keys(SysQueueMember.fields); |
| 75 | + const columns = MEMBERS.columns as string[]; |
| 76 | + expect(columns.filter((c) => !memberFields.includes(c) && c !== 'updated_at')).toEqual([]); |
| 77 | + }); |
| 78 | + |
| 79 | + it('its label is translated in every shipped locale, `en` byte-equal to the page literal', () => { |
| 80 | + expect(sharingPageTranslations.en.sys_queue_detail?.label).toBe(PAGE.label); |
| 81 | + for (const [locale, bundle] of Object.entries(SharingTranslations)) { |
| 82 | + expect((bundle as AnyRec).pages?.sys_queue_detail?.label, locale) |
| 83 | + .toBe((sharingPageTranslations as AnyRec)[locale].sys_queue_detail.label); |
| 84 | + } |
| 85 | + }); |
| 86 | +}); |
| 87 | + |
| 88 | +describe('sys_queue record page — the members list on a real engine', () => { |
| 89 | + let engine: ObjectQL; |
| 90 | + let billing: AnyRec; |
| 91 | + |
| 92 | + /** The rows the page's list shows on `queue`'s page. */ |
| 93 | + const listedOn = async (queue: AnyRec): Promise<AnyRec[]> => |
| 94 | + (await engine.find('sys_queue_member', { |
| 95 | + where: { [MEMBERS.relationshipField]: queue[MEMBERS.relationshipValueField] }, |
| 96 | + context: SYS, |
| 97 | + })) as AnyRec[]; |
| 98 | + /** What the prefilled "New" form carries before the administrator fills in the member. */ |
| 99 | + const prefilledNew = (queue: AnyRec): AnyRec => ({ [MEMBERS.relationshipField]: queue[MEMBERS.relationshipValueField] }); |
| 100 | + |
| 101 | + beforeAll(async () => { |
| 102 | + const driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true }); |
| 103 | + await driver.connect(); |
| 104 | + engine = new ObjectQL(); |
| 105 | + engine.registerDriver(driver as never, true); |
| 106 | + await engine.init(); |
| 107 | + for (const def of [SysOrganization, SysQueue, SysQueueMember]) { |
| 108 | + engine.registry.registerObject(def as never, 'plugin-sharing', 'sys'); |
| 109 | + } |
| 110 | + await engine.syncSchemas(); |
| 111 | + await engine.insert('sys_organization', { id: ORG, name: 'QA Org', slug: 'qa-org' }, { context: SYS }); |
| 112 | + |
| 113 | + billing = (await engine.insert('sys_queue', { name: 'billing', label: 'Billing', organization_id: ORG }, { context: SYS })) as AnyRec; |
| 114 | + await engine.insert('sys_queue', { name: 'support', label: 'Support', organization_id: ORG }, { context: SYS }); |
| 115 | + for (const [queue, type, id] of [ |
| 116 | + ['billing', 'user', 'u_ada'], |
| 117 | + ['billing', 'position', 'billing_clerk'], |
| 118 | + ['support', 'user', 'u_bob'], |
| 119 | + ] as const) { |
| 120 | + await engine.insert('sys_queue_member', { queue, recipient_type: type, recipient_id: id, organization_id: ORG }, { context: SYS }); |
| 121 | + } |
| 122 | + }); |
| 123 | + |
| 124 | + afterAll(async () => { |
| 125 | + try { await engine?.destroy(); } catch { /* noop */ } |
| 126 | + }); |
| 127 | + |
| 128 | + it('the FILTER: a queue\'s page lists its own members, by name, and no other queue\'s', async () => { |
| 129 | + // Control: keyed on the id, the default, the list would find nothing — the |
| 130 | + // members store the name, which is why the page names `name`. |
| 131 | + expect(billing.id).not.toBe(billing.name); |
| 132 | + expect(await engine.find('sys_queue_member', { where: { queue: billing.id }, context: SYS })).toEqual([]); |
| 133 | + |
| 134 | + const listed = await listedOn(billing); |
| 135 | + expect(listed.map((m) => `${m.recipient_type}:${m.recipient_id}`).sort()).toEqual(['position:billing_clerk', 'user:u_ada']); |
| 136 | + }); |
| 137 | + |
| 138 | + it('the "New" PREFILL: the form carries the queue\'s name, and the member it saves is listed on the page', async () => { |
| 139 | + const draft = prefilledNew(billing); |
| 140 | + expect(draft).toEqual({ queue: 'billing' }); |
| 141 | + await engine.insert( |
| 142 | + 'sys_queue_member', |
| 143 | + { ...draft, recipient_type: 'user', recipient_id: 'u_cy', organization_id: ORG }, |
| 144 | + { context: SYS }, |
| 145 | + ); |
| 146 | + expect((await listedOn(billing)).map((m) => m.recipient_id)).toContain('u_cy'); |
| 147 | + }); |
| 148 | + |
| 149 | + it('the list and the ownership-queue service agree on who is in the queue', async () => { |
| 150 | + const service = new OwnershipQueueService({ engine: () => engine as never }); |
| 151 | + const resolved = await service.resolveQueue(billing.name, ORG); |
| 152 | + expect(resolved?.source).toBe('authored'); |
| 153 | + const listed = (await listedOn(billing)).map((m) => `${m.recipient_type}:${m.recipient_id}`).sort(); |
| 154 | + expect((resolved?.members ?? []).map((m) => `${m.type}:${m.value}`).sort()).toEqual(listed); |
| 155 | + }); |
| 156 | +}); |
0 commit comments