Open History to see the last commit
1import { test, expect } from './fixtures'
2
3interface Link {
4 transferId: string
5 harness: string
6 threadId: string
7}
8
9interface Entry {
10 harness: string
11 members: Array<{ userId: string; role: string }>
12 parentThreadId?: string
13 metadata?: {
14 'harness:transferredFrom'?: Array<Link>
15 'harness:transferredTo'?: Array<Link>
16 }
17}
18
19interface Result {
20 error?: string
21 otherTenant: string
22 first: { transferId: string; threadId: string }
23 second: { transferId: string; threadId: string }
24 sourceTranscript: Array<{ role: string; text: string }>
25 targetTranscript: Array<{ role: string; text: string }>
26 targetEntry: Entry
27 sourceEntry: Entry
28 threads: number
29 globexThreads: number
30 transfers: {
31 afterFirst: { messages: number; records: number }
32 target: Array<{ transferId: string; from?: unknown }>
33 source: Array<{ transferId: string; to?: unknown }>
34 }
35}
36
37const SUPPORT = 'e2e/harness-transfer-support'
38const BILLING = 'e2e/harness-transfer-billing'
39// The id of the source thread, which the route makes.
40const SOURCE_ID = expect.stringMatching(/^e2e-harness-transfer-/)
41const NOTE = 'Refund for order 42. The user waits.'
42
43test.describe('harness: a transfer to another harness', () => {
44 test("moves the messages and the note with messages: 'all', links both threads, and a retry adds nothing", async ({
45 request,
46 }) => {
47 const res = await request.post('/api/harness-transfer')
48 expect(res.ok()).toBe(true)
49 const result: Result = await res.json()
50 expect(result.error ?? null).toBeNull()
51
52 // A user of another tenant is refused, and gets no thread.
53 expect(result.otherTenant).toBe('denied')
54 expect(result.globexThreads).toBe(0)
55
56 // The target thread has the note first, then the messages of the source.
57 const { first } = result
58 expect(result.sourceTranscript).toEqual([
59 { role: 'user', text: 'Hi.' },
60 { role: 'assistant', text: 'Support here.' },
61 { role: 'user', text: 'I want a refund.' },
62 { role: 'assistant', text: 'I pass you to billing.' },
63 ])
64 expect(result.targetTranscript).toEqual([
65 { role: 'user', text: NOTE },
66 ...result.sourceTranscript,
67 ])
68
69 // The target belongs to the billing harness, and olga owns it alone.
70 expect(result.targetEntry).toMatchObject({
71 harness: BILLING,
72 members: [{ userId: 'olga', role: 'owner' }],
73 })
74 expect(result.targetEntry.parentThreadId).toBeUndefined()
75
76 // Both threads show the link, in the index and in the log.
77 const source = result.sourceEntry
78 expect(result.targetEntry.metadata?.['harness:transferredFrom']).toEqual([
79 { transferId: first.transferId, harness: SUPPORT, threadId: SOURCE_ID },
80 ])
81 expect(source.metadata?.['harness:transferredTo']).toEqual([
82 {
83 transferId: first.transferId,
84 harness: BILLING,
85 threadId: first.threadId,
86 },
87 ])
88 expect(result.transfers.target).toEqual([
89 expect.objectContaining({
90 transferId: first.transferId,
91 from: { harness: SUPPORT, threadId: SOURCE_ID },
92 }),
93 ])
94 expect(result.transfers.source).toEqual([
95 expect.objectContaining({
96 transferId: first.transferId,
97 to: { harness: BILLING, threadId: first.threadId },
98 }),
99 ])
100
101 // The retry with the same transferId returns the same target, and adds
102 // no thread, no message, and no record.
103 expect(result.second).toEqual(first)
104 expect(result.threads).toBe(2)
105 expect(result.transfers.afterFirst).toEqual({
106 messages: result.targetTranscript.length,
107 records: result.transfers.target.length,
108 })
109 })
110})