@teleporthq/teleport-plugin-next-workflows 0.43.48 → 0.43.50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/auth-session-cost.test.ts +101 -0
- package/__tests__/data-node-not-awaited.test.ts +12 -1
- package/__tests__/ecommerce-get-settings-client.test.ts +383 -0
- package/__tests__/resolve-user-segment-skip.test.ts +226 -0
- package/dist/cjs/executor-generator.d.ts.map +1 -1
- package/dist/cjs/executor-generator.js +12 -1
- package/dist/cjs/executor-generator.js.map +1 -1
- package/dist/cjs/nodes/account/account-get-current.d.ts.map +1 -1
- package/dist/cjs/nodes/account/account-get-current.js +51 -7
- package/dist/cjs/nodes/account/account-get-current.js.map +1 -1
- package/dist/cjs/nodes/ecommerce/ecommerce-get-settings.d.ts.map +1 -1
- package/dist/cjs/nodes/ecommerce/ecommerce-get-settings.js +59 -1
- package/dist/cjs/nodes/ecommerce/ecommerce-get-settings.js.map +1 -1
- package/dist/cjs/tsconfig.tsbuildinfo +1 -1
- package/dist/cjs/workflow-project-plugin.d.ts.map +1 -1
- package/dist/cjs/workflow-project-plugin.js +11 -1
- package/dist/cjs/workflow-project-plugin.js.map +1 -1
- package/dist/esm/executor-generator.d.ts.map +1 -1
- package/dist/esm/executor-generator.js +12 -1
- package/dist/esm/executor-generator.js.map +1 -1
- package/dist/esm/nodes/account/account-get-current.d.ts.map +1 -1
- package/dist/esm/nodes/account/account-get-current.js +51 -7
- package/dist/esm/nodes/account/account-get-current.js.map +1 -1
- package/dist/esm/nodes/ecommerce/ecommerce-get-settings.d.ts.map +1 -1
- package/dist/esm/nodes/ecommerce/ecommerce-get-settings.js +59 -1
- package/dist/esm/nodes/ecommerce/ecommerce-get-settings.js.map +1 -1
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/dist/esm/workflow-project-plugin.d.ts.map +1 -1
- package/dist/esm/workflow-project-plugin.js +11 -1
- package/dist/esm/workflow-project-plugin.js.map +1 -1
- package/package.json +2 -2
- package/src/executor-generator.ts +12 -1
- package/src/nodes/account/account-get-current.ts +52 -8
- package/src/nodes/ecommerce/ecommerce-get-settings.ts +62 -3
- package/src/workflow-project-plugin.ts +11 -1
|
@@ -591,6 +591,107 @@ describe('account-get-current reads the in-memory session before the network', (
|
|
|
591
591
|
)
|
|
592
592
|
})
|
|
593
593
|
|
|
594
|
+
it('confirms a sign-out ONCE per page load, then stops re-fetching', async () => {
|
|
595
|
+
// next-auth reports 'unauthenticated' both for "signed out" and "my fetch
|
|
596
|
+
// failed", so the FIRST guest resolution verifies against the endpoint.
|
|
597
|
+
// Once that verification succeeds with no user, repeating it on every
|
|
598
|
+
// click only burns a round trip — the memo short-circuits it.
|
|
599
|
+
let fetched = 0
|
|
600
|
+
await withWindow(
|
|
601
|
+
{ getSession: () => ({ status: 'unauthenticated', session: null as unknown }) },
|
|
602
|
+
async () => {
|
|
603
|
+
fetched += 1
|
|
604
|
+
return { ok: true, json: async () => ({}) }
|
|
605
|
+
},
|
|
606
|
+
async (handler) => {
|
|
607
|
+
const first = await handler({}, {})
|
|
608
|
+
expect(fetched).toBe(1)
|
|
609
|
+
expect(first.user).toBeNull()
|
|
610
|
+
|
|
611
|
+
const second = await handler({}, {})
|
|
612
|
+
expect(fetched).toBe(1)
|
|
613
|
+
expect(second.user).toBeNull()
|
|
614
|
+
}
|
|
615
|
+
)
|
|
616
|
+
})
|
|
617
|
+
|
|
618
|
+
it('never memoizes sign-out from a FAILED confirmation fetch', async () => {
|
|
619
|
+
let fetched = 0
|
|
620
|
+
const user = { id: 'u-7', email: 'joan@example.com' }
|
|
621
|
+
await withWindow(
|
|
622
|
+
{ getSession: () => ({ status: 'unauthenticated', session: null as unknown }) },
|
|
623
|
+
async () => {
|
|
624
|
+
fetched += 1
|
|
625
|
+
if (fetched === 1) {
|
|
626
|
+
throw new Error('network blip')
|
|
627
|
+
}
|
|
628
|
+
return { ok: true, json: async () => ({ user }) }
|
|
629
|
+
},
|
|
630
|
+
async (handler) => {
|
|
631
|
+
await handler({}, {})
|
|
632
|
+
// The blip must not count as confirmation — the next call re-verifies
|
|
633
|
+
// and finds the real signed-in session.
|
|
634
|
+
const second = await handler({}, {})
|
|
635
|
+
expect(fetched).toBe(2)
|
|
636
|
+
expect(second.id).toBe('u-7')
|
|
637
|
+
}
|
|
638
|
+
)
|
|
639
|
+
})
|
|
640
|
+
|
|
641
|
+
it('a confirmation fetch that finds a user does NOT memoize sign-out', async () => {
|
|
642
|
+
let fetched = 0
|
|
643
|
+
const user = { id: 'u-8', email: 'lin@example.com' }
|
|
644
|
+
await withWindow(
|
|
645
|
+
{ getSession: () => ({ status: 'unauthenticated', session: null as unknown }) },
|
|
646
|
+
async () => {
|
|
647
|
+
fetched += 1
|
|
648
|
+
return { ok: true, json: async () => ({ user }) }
|
|
649
|
+
},
|
|
650
|
+
async (handler) => {
|
|
651
|
+
const first = await handler({}, {})
|
|
652
|
+
expect(first.id).toBe('u-8')
|
|
653
|
+
const second = await handler({}, {})
|
|
654
|
+
// Still verifying every time — the memo only forms on a confirmed
|
|
655
|
+
// empty session, never while a user is being returned.
|
|
656
|
+
expect(fetched).toBe(2)
|
|
657
|
+
expect(second.id).toBe('u-8')
|
|
658
|
+
}
|
|
659
|
+
)
|
|
660
|
+
})
|
|
661
|
+
|
|
662
|
+
it('an authenticated bridge clears a previously memoized sign-out', async () => {
|
|
663
|
+
// Sequence: guest confirms sign-out (memo forms) → user signs in (bridge
|
|
664
|
+
// flips to authenticated) → signs out again (bridge back to
|
|
665
|
+
// unauthenticated). The sign-in must clear the memo... and it does, but
|
|
666
|
+
// even if it did not, correctness holds: the memo is only consulted while
|
|
667
|
+
// next-auth itself reports 'unauthenticated'.
|
|
668
|
+
let status = 'unauthenticated'
|
|
669
|
+
let fetched = 0
|
|
670
|
+
const user = { id: 'u-9', email: 'kay@example.com' }
|
|
671
|
+
await withWindow(
|
|
672
|
+
{ getSession: () => ({ status, session: status === 'authenticated' ? { user } : null }) },
|
|
673
|
+
async () => {
|
|
674
|
+
fetched += 1
|
|
675
|
+
return { ok: true, json: async () => ({}) }
|
|
676
|
+
},
|
|
677
|
+
async (handler) => {
|
|
678
|
+
await handler({}, {})
|
|
679
|
+
expect(fetched).toBe(1)
|
|
680
|
+
expect((global as any).window.__tqSessionConfirmedSignedOut).toBe(true)
|
|
681
|
+
|
|
682
|
+
status = 'authenticated'
|
|
683
|
+
const signedIn = await handler({}, {})
|
|
684
|
+
expect(signedIn.id).toBe('u-9')
|
|
685
|
+
expect((global as any).window.__tqSessionConfirmedSignedOut).toBe(false)
|
|
686
|
+
|
|
687
|
+
status = 'unauthenticated'
|
|
688
|
+
await handler({}, {})
|
|
689
|
+
// Memo was cleared by the sign-in, so sign-out is re-confirmed once.
|
|
690
|
+
expect(fetched).toBe(2)
|
|
691
|
+
}
|
|
692
|
+
)
|
|
693
|
+
})
|
|
694
|
+
|
|
594
695
|
it('ignores a malformed bridge rather than throwing', async () => {
|
|
595
696
|
const user = { id: 'u-6', email: 'ken@example.com' }
|
|
596
697
|
let fetched = 0
|
|
@@ -476,12 +476,23 @@ describe('generated client runtime', () => {
|
|
|
476
476
|
const client = generateClientRuntimeCode()
|
|
477
477
|
|
|
478
478
|
it('dispatches an all-fire-and-forget segment without awaiting the round trip', () => {
|
|
479
|
-
expect(client).toContain('if (
|
|
479
|
+
expect(client).toContain('if (segIsFireAndForget) {')
|
|
480
480
|
expect(client).toContain('callServerSegment(ffUrl, context).catch(')
|
|
481
481
|
// The nodes still get their null entries so downstream reads are defined.
|
|
482
482
|
expect(client).toContain('context[seg.nodes[ffi].id] = null;')
|
|
483
483
|
})
|
|
484
484
|
|
|
485
|
+
it('upgrades a mixed segment to fire-and-forget when the awaited nodes were branch-skipped', () => {
|
|
486
|
+
// Static flag first, then the dynamic check over the LIVE (non-skipped)
|
|
487
|
+
// nodes — a mixed segment whose awaited nodes sit on a branch that was
|
|
488
|
+
// not taken has only fire-and-forget work left to do.
|
|
489
|
+
expect(client).toContain(
|
|
490
|
+
'var segLiveNodes = seg.nodes.filter(function(n) { return !(context.__skippedNodes && context.__skippedNodes[n.id]); });'
|
|
491
|
+
)
|
|
492
|
+
expect(client).toContain('var segIsFireAndForget = seg.fireAndForget ||')
|
|
493
|
+
expect(client).toContain('segLiveNodes.every(utils.isFireAndForgetNode)')
|
|
494
|
+
})
|
|
495
|
+
|
|
485
496
|
it('keeps awaiting a normal server segment', () => {
|
|
486
497
|
expect(client).toContain('const serverResults = await callServerSegment(url, context);')
|
|
487
498
|
})
|
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
import { splitIntoSegments, generateSharedRuntimeUtilsCode } from '../src'
|
|
2
|
+
import { nodeRegistry } from '../src/nodes'
|
|
3
|
+
import { NextWorkflowProjectPlugin } from '../src/workflow-project-plugin'
|
|
4
|
+
import { UIDLWorkflow } from '@teleporthq/teleport-types'
|
|
5
|
+
|
|
6
|
+
// The /api/ecommerce/settings route returns a literal baked at generation time
|
|
7
|
+
// — within a deployment it can never say anything else. Classifying the
|
|
8
|
+
// `ecommerce-get-settings` node as a SERVER node therefore bought nothing and
|
|
9
|
+
// cost a full blocking round trip per click, PLUS it fragmented the
|
|
10
|
+
// surrounding workflow into extra server segments (one round trip each).
|
|
11
|
+
//
|
|
12
|
+
// The contract this file pins down:
|
|
13
|
+
// 1. the node is client-classified, so the segment splitter never mints a
|
|
14
|
+
// server segment for it;
|
|
15
|
+
// 2. the client handler resolves from the baked window global first, then a
|
|
16
|
+
// memoized fetch, then the route;
|
|
17
|
+
// 3. cron/webhook routes (which execute every node server-side, with no
|
|
18
|
+
// window) still get a real server implementation via
|
|
19
|
+
// generateServerHandler — never the client-only stub;
|
|
20
|
+
// 4. the add-to-cart / cart-increment workflow shapes collapse to exactly
|
|
21
|
+
// ONE server segment: the product data-select.
|
|
22
|
+
|
|
23
|
+
describe('ecommerce-get-settings node classification', () => {
|
|
24
|
+
const generator = nodeRegistry['ecommerce-get-settings']
|
|
25
|
+
|
|
26
|
+
it('is client-classified', () => {
|
|
27
|
+
expect(generator.executionEnv).toBe('client')
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('client handler reads the baked global, memoizes the fetch fallback', () => {
|
|
31
|
+
const source = generator.generateHandler()
|
|
32
|
+
expect(source).toContain('__teleportEcommerceSettings')
|
|
33
|
+
expect(source).toContain('__teleportEcommerceSettingsFetched')
|
|
34
|
+
expect(source).toContain('/api/ecommerce/settings')
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('keeps a real server implementation for cron/webhook contexts', () => {
|
|
38
|
+
expect(generator.generateServerHandler).toBeDefined()
|
|
39
|
+
const source = generator.generateServerHandler!()
|
|
40
|
+
expect(source).toContain('/api/ecommerce/settings')
|
|
41
|
+
expect(source).not.toContain('window')
|
|
42
|
+
})
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
describe('ecommerce-get-settings client handler behaviour', () => {
|
|
46
|
+
const buildHandler = () => {
|
|
47
|
+
const source = nodeRegistry['ecommerce-get-settings'].generateHandler()
|
|
48
|
+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
49
|
+
return new Function(`return (${source})`)() as (
|
|
50
|
+
config: unknown,
|
|
51
|
+
context: Record<string, unknown>
|
|
52
|
+
) => Promise<Record<string, unknown>>
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const globalAny = global as unknown as Record<string, unknown>
|
|
56
|
+
|
|
57
|
+
afterEach(() => {
|
|
58
|
+
delete globalAny.window
|
|
59
|
+
delete globalAny.fetch
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it('returns the baked global without fetching', async () => {
|
|
63
|
+
const baked = { guestCheckout: true, stockManagement: true, maxQuantityPerProduct: null }
|
|
64
|
+
globalAny.window = { __teleportEcommerceSettings: baked }
|
|
65
|
+
globalAny.fetch = jest.fn()
|
|
66
|
+
|
|
67
|
+
const result = await buildHandler()({}, {})
|
|
68
|
+
expect(result).toBe(baked)
|
|
69
|
+
expect(globalAny.fetch).not.toHaveBeenCalled()
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it('falls back to the route and memoizes a successful response', async () => {
|
|
73
|
+
const payload = { guestCheckout: false, stockManagement: true }
|
|
74
|
+
globalAny.window = {}
|
|
75
|
+
const fetchMock = jest.fn().mockResolvedValue({
|
|
76
|
+
ok: true,
|
|
77
|
+
json: async () => payload,
|
|
78
|
+
})
|
|
79
|
+
globalAny.fetch = fetchMock
|
|
80
|
+
|
|
81
|
+
const handler = buildHandler()
|
|
82
|
+
const first = await handler({}, {})
|
|
83
|
+
expect(first).toEqual(payload)
|
|
84
|
+
expect(fetchMock).toHaveBeenCalledTimes(1)
|
|
85
|
+
|
|
86
|
+
// Second call resolves from the memo — the route is immutable within a
|
|
87
|
+
// deployment, so this can never serve stale data.
|
|
88
|
+
const second = await handler({}, {})
|
|
89
|
+
expect(second).toEqual(payload)
|
|
90
|
+
expect(fetchMock).toHaveBeenCalledTimes(1)
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it('does not memoize a failed response', async () => {
|
|
94
|
+
globalAny.window = {}
|
|
95
|
+
const fetchMock = jest
|
|
96
|
+
.fn()
|
|
97
|
+
.mockResolvedValueOnce({ ok: false })
|
|
98
|
+
.mockResolvedValueOnce({ ok: true, json: async () => ({ guestCheckout: true }) })
|
|
99
|
+
globalAny.fetch = fetchMock
|
|
100
|
+
|
|
101
|
+
const handler = buildHandler()
|
|
102
|
+
const first = await handler({}, {})
|
|
103
|
+
expect(first).toEqual({ error: 'Failed to load e-commerce settings' })
|
|
104
|
+
|
|
105
|
+
const second = await handler({}, {})
|
|
106
|
+
expect(second).toEqual({ guestCheckout: true })
|
|
107
|
+
expect(fetchMock).toHaveBeenCalledTimes(2)
|
|
108
|
+
})
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
describe('segment shapes around a client settings node', () => {
|
|
112
|
+
const node = (id: string, type: string, config: Record<string, unknown> = {}) => ({
|
|
113
|
+
id,
|
|
114
|
+
type,
|
|
115
|
+
label: id,
|
|
116
|
+
config,
|
|
117
|
+
stepNumber: 0,
|
|
118
|
+
})
|
|
119
|
+
const edge = (source: string, target: string, sourceHandle?: string) => ({
|
|
120
|
+
id: `${source}->${target}`,
|
|
121
|
+
source,
|
|
122
|
+
target,
|
|
123
|
+
...(sourceHandle ? { sourceHandle } : {}),
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
it('cart-increment shape: ONE server segment holding only the data-select', () => {
|
|
127
|
+
// trigger → data-select (server) → get-settings → validate (client js) → if
|
|
128
|
+
const workflow = {
|
|
129
|
+
nodes: [
|
|
130
|
+
node('trigger', 'event-element-clicked', { isFirstNode: true }),
|
|
131
|
+
node('fetch-product', 'data-select', { tableName: 'teleport_products' }),
|
|
132
|
+
node('get-settings', 'ecommerce-get-settings'),
|
|
133
|
+
node('validate', 'general-custom-js', {
|
|
134
|
+
code: 'function customHandler(p){}',
|
|
135
|
+
context: 'client',
|
|
136
|
+
}),
|
|
137
|
+
node('gate', 'general-if-statement', {}),
|
|
138
|
+
],
|
|
139
|
+
edges: [
|
|
140
|
+
edge('trigger', 'fetch-product'),
|
|
141
|
+
edge('fetch-product', 'get-settings'),
|
|
142
|
+
edge('get-settings', 'validate'),
|
|
143
|
+
edge('validate', 'gate'),
|
|
144
|
+
],
|
|
145
|
+
} as unknown as UIDLWorkflow
|
|
146
|
+
|
|
147
|
+
const segments = splitIntoSegments(workflow)
|
|
148
|
+
const serverSegments = segments.filter((s) => s.env === 'server')
|
|
149
|
+
expect(serverSegments).toHaveLength(1)
|
|
150
|
+
expect(serverSegments[0].nodes.map((n) => n.id)).toEqual(['fetch-product'])
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
it('add-to-cart shape: settings between client nodes mints NO server segment', () => {
|
|
154
|
+
// extract (client js) → get-settings → guest-check (client js) → if →
|
|
155
|
+
// [redirect | data-select] — only the data-select is a server segment.
|
|
156
|
+
const workflow = {
|
|
157
|
+
nodes: [
|
|
158
|
+
node('extract', 'general-custom-js', {
|
|
159
|
+
code: 'function customHandler(p){}',
|
|
160
|
+
context: 'client',
|
|
161
|
+
}),
|
|
162
|
+
node('get-settings', 'ecommerce-get-settings'),
|
|
163
|
+
node('guest-check', 'general-custom-js', {
|
|
164
|
+
code: 'function customHandler(p){}',
|
|
165
|
+
context: 'client',
|
|
166
|
+
}),
|
|
167
|
+
node('gate', 'general-if-statement', {}),
|
|
168
|
+
node('redirect', 'navigation-navigate-to-url', { url: '/sign-in' }),
|
|
169
|
+
node('fetch-product', 'data-select', { tableName: 'teleport_products' }),
|
|
170
|
+
],
|
|
171
|
+
edges: [
|
|
172
|
+
edge('extract', 'get-settings'),
|
|
173
|
+
edge('get-settings', 'guest-check'),
|
|
174
|
+
edge('guest-check', 'gate'),
|
|
175
|
+
edge('gate', 'redirect', 'true'),
|
|
176
|
+
edge('gate', 'fetch-product', 'false'),
|
|
177
|
+
],
|
|
178
|
+
} as unknown as UIDLWorkflow
|
|
179
|
+
|
|
180
|
+
const segments = splitIntoSegments(workflow)
|
|
181
|
+
const serverSegments = segments.filter((s) => s.env === 'server')
|
|
182
|
+
expect(serverSegments).toHaveLength(1)
|
|
183
|
+
expect(serverSegments[0].nodes.map((n) => n.id)).toEqual(['fetch-product'])
|
|
184
|
+
})
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
// ─── Dynamic fire-and-forget upgrade, end to end ─────────────────────────────
|
|
188
|
+
//
|
|
189
|
+
// Mirrors the "Resolve Current User" custom node's anonymous half: an
|
|
190
|
+
// if-statement routes a RETURNING guest to a fire-and-forget users-row
|
|
191
|
+
// re-ensure and a NEW guest to an awaited create. Both data nodes land in one
|
|
192
|
+
// server segment, so the segment is statically mixed (fireAndForget: false).
|
|
193
|
+
// When the taken branch leaves only fire-and-forget work live, the runtime
|
|
194
|
+
// must dispatch the segment WITHOUT awaiting; when the awaited branch is
|
|
195
|
+
// taken, it must still block.
|
|
196
|
+
describe('generated custom node — mixed segment upgrades to fire-and-forget per branch', () => {
|
|
197
|
+
const customNodes = {
|
|
198
|
+
'cn-resolve': {
|
|
199
|
+
id: 'cn-resolve',
|
|
200
|
+
name: 'Resolve-ish',
|
|
201
|
+
parameters: [],
|
|
202
|
+
nodes: [
|
|
203
|
+
{
|
|
204
|
+
id: 'extract',
|
|
205
|
+
type: 'general-custom-js',
|
|
206
|
+
label: 'Extract',
|
|
207
|
+
config: {
|
|
208
|
+
code: 'function customHandler(p, q) { return { returning: true } }',
|
|
209
|
+
context: 'client',
|
|
210
|
+
},
|
|
211
|
+
executionEnv: 'client',
|
|
212
|
+
stepNumber: 0,
|
|
213
|
+
},
|
|
214
|
+
{
|
|
215
|
+
id: 'gate',
|
|
216
|
+
type: 'general-if-statement',
|
|
217
|
+
label: 'Returning Guest?',
|
|
218
|
+
config: {
|
|
219
|
+
conditionType: 'simple-comparison',
|
|
220
|
+
leftValue: {
|
|
221
|
+
type: 'workflowContext',
|
|
222
|
+
nodeId: 'extract',
|
|
223
|
+
path: ['extract', 'returning'],
|
|
224
|
+
},
|
|
225
|
+
operator: '===',
|
|
226
|
+
rightValue: true,
|
|
227
|
+
},
|
|
228
|
+
executionEnv: 'client',
|
|
229
|
+
stepNumber: 1,
|
|
230
|
+
},
|
|
231
|
+
{
|
|
232
|
+
id: 'ensure',
|
|
233
|
+
type: 'data-create-item',
|
|
234
|
+
label: 'Ensure Row',
|
|
235
|
+
config: { awaitResult: false },
|
|
236
|
+
executionEnv: 'server',
|
|
237
|
+
stepNumber: 2,
|
|
238
|
+
},
|
|
239
|
+
{
|
|
240
|
+
id: 'create',
|
|
241
|
+
type: 'data-create-item',
|
|
242
|
+
label: 'Create Row',
|
|
243
|
+
config: {},
|
|
244
|
+
executionEnv: 'server',
|
|
245
|
+
stepNumber: 2,
|
|
246
|
+
},
|
|
247
|
+
{
|
|
248
|
+
id: 'return-stored',
|
|
249
|
+
type: 'general-custom-js',
|
|
250
|
+
label: 'Return Stored',
|
|
251
|
+
config: {
|
|
252
|
+
code: 'function customHandler(p, q) { return { branch: "stored" } }',
|
|
253
|
+
context: 'client',
|
|
254
|
+
},
|
|
255
|
+
executionEnv: 'client',
|
|
256
|
+
stepNumber: 3,
|
|
257
|
+
},
|
|
258
|
+
{
|
|
259
|
+
id: 'return-new',
|
|
260
|
+
type: 'general-custom-js',
|
|
261
|
+
label: 'Return New',
|
|
262
|
+
config: {
|
|
263
|
+
code: 'function customHandler(p, q) { return { branch: "new" } }',
|
|
264
|
+
context: 'client',
|
|
265
|
+
},
|
|
266
|
+
executionEnv: 'client',
|
|
267
|
+
stepNumber: 3,
|
|
268
|
+
},
|
|
269
|
+
],
|
|
270
|
+
edges: [
|
|
271
|
+
{ id: 'e1', source: 'extract', target: 'gate' },
|
|
272
|
+
{ id: 'e2', source: 'gate', target: 'ensure', sourceHandle: 'true' },
|
|
273
|
+
{ id: 'e3', source: 'gate', target: 'create', sourceHandle: 'false' },
|
|
274
|
+
{ id: 'e4', source: 'ensure', target: 'return-stored' },
|
|
275
|
+
{ id: 'e5', source: 'create', target: 'return-new' },
|
|
276
|
+
],
|
|
277
|
+
},
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
interface BootedCustomNode {
|
|
281
|
+
run: (
|
|
282
|
+
outerContext: Record<string, unknown>,
|
|
283
|
+
parameters: Record<string, unknown>,
|
|
284
|
+
handlers: Record<string, unknown>
|
|
285
|
+
) => Promise<unknown>
|
|
286
|
+
segmentCalls: string[]
|
|
287
|
+
resolveSegment: () => void
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const bootCustomNode = (): BootedCustomNode => {
|
|
291
|
+
const plugin = new NextWorkflowProjectPlugin() as unknown as {
|
|
292
|
+
generateCustomNodesFile: (
|
|
293
|
+
nodes: Record<string, unknown>,
|
|
294
|
+
urls: Record<string, Record<string, string>>
|
|
295
|
+
) => string
|
|
296
|
+
}
|
|
297
|
+
const source = plugin.generateCustomNodesFile(customNodes, {
|
|
298
|
+
'cn-resolve': { 'server-1': '/api/workflows/resolve-seg-1' },
|
|
299
|
+
})
|
|
300
|
+
|
|
301
|
+
const utilsModule: { exports: Record<string, unknown> } = { exports: {} }
|
|
302
|
+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
303
|
+
new Function('module', 'exports', generateSharedRuntimeUtilsCode())(
|
|
304
|
+
utilsModule,
|
|
305
|
+
utilsModule.exports
|
|
306
|
+
)
|
|
307
|
+
|
|
308
|
+
const segmentCalls: string[] = []
|
|
309
|
+
let releaseSegment: () => void = () => undefined
|
|
310
|
+
const segmentGate = new Promise<void>((resolve) => {
|
|
311
|
+
releaseSegment = resolve
|
|
312
|
+
})
|
|
313
|
+
const runtimeStub = {
|
|
314
|
+
findStreamingAINodes: () => ({}),
|
|
315
|
+
mergeServerResults: () => undefined,
|
|
316
|
+
callStreamingServerSegment: async () => ({}),
|
|
317
|
+
callServerSegment: async (url: string) => {
|
|
318
|
+
segmentCalls.push(url)
|
|
319
|
+
await segmentGate
|
|
320
|
+
return {}
|
|
321
|
+
},
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const requireStub = (id: string) =>
|
|
325
|
+
id === './runtime-utils' ? utilsModule.exports : id === './runtime' ? runtimeStub : {}
|
|
326
|
+
|
|
327
|
+
const moduleStub: { exports: Record<string, unknown> } = { exports: {} }
|
|
328
|
+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
329
|
+
new Function('module', 'exports', 'require', source)(
|
|
330
|
+
moduleStub,
|
|
331
|
+
moduleStub.exports,
|
|
332
|
+
requireStub
|
|
333
|
+
)
|
|
334
|
+
|
|
335
|
+
const registry = moduleStub.exports as Record<string, BootedCustomNode['run']>
|
|
336
|
+
return { run: registry['cn-resolve'], segmentCalls, resolveSegment: releaseSegment }
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const handlersFor = (returning: boolean) => ({
|
|
340
|
+
'general-custom-js': async (config: { code?: string }) => {
|
|
341
|
+
const code = config && config.code ? config.code : ''
|
|
342
|
+
if (code.includes('returning')) {
|
|
343
|
+
return { returning }
|
|
344
|
+
}
|
|
345
|
+
if (code.includes('stored')) {
|
|
346
|
+
return { branch: 'stored' }
|
|
347
|
+
}
|
|
348
|
+
return { branch: 'new' }
|
|
349
|
+
},
|
|
350
|
+
})
|
|
351
|
+
|
|
352
|
+
it('returning-guest branch (only fire-and-forget live) does not await the segment', async () => {
|
|
353
|
+
const booted = bootCustomNode()
|
|
354
|
+
|
|
355
|
+
// The gate promise is never released — if the runtime awaited the segment
|
|
356
|
+
// this would hang and jest would time the test out.
|
|
357
|
+
const result = (await booted.run({}, {}, handlersFor(true))) as Record<string, unknown>
|
|
358
|
+
|
|
359
|
+
expect(booted.segmentCalls).toEqual(['/api/workflows/resolve-seg-1'])
|
|
360
|
+
expect(result).toEqual({ branch: 'stored' })
|
|
361
|
+
|
|
362
|
+
booted.resolveSegment()
|
|
363
|
+
})
|
|
364
|
+
|
|
365
|
+
it('new-guest branch (awaited create live) still blocks on the segment', async () => {
|
|
366
|
+
const booted = bootCustomNode()
|
|
367
|
+
|
|
368
|
+
let settled = false
|
|
369
|
+
const running = booted.run({}, {}, handlersFor(false)).then((value) => {
|
|
370
|
+
settled = true
|
|
371
|
+
return value
|
|
372
|
+
})
|
|
373
|
+
|
|
374
|
+
// Give the runtime ample turns to (incorrectly) run to completion.
|
|
375
|
+
await new Promise((resolve) => setTimeout(resolve, 50))
|
|
376
|
+
expect(booted.segmentCalls).toEqual(['/api/workflows/resolve-seg-1'])
|
|
377
|
+
expect(settled).toBe(false)
|
|
378
|
+
|
|
379
|
+
booted.resolveSegment()
|
|
380
|
+
const result = (await running) as Record<string, unknown>
|
|
381
|
+
expect(result).toEqual({ branch: 'new' })
|
|
382
|
+
})
|
|
383
|
+
})
|