@asteby/metacore-runtime-react 53.4.1 → 53.5.0

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.
Files changed (59) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +73 -0
  3. package/dist/index.d.ts +2 -0
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +4 -0
  6. package/dist/setup/addon-config-surface.d.ts +110 -0
  7. package/dist/setup/addon-config-surface.d.ts.map +1 -0
  8. package/dist/setup/addon-config-surface.js +553 -0
  9. package/dist/setup/connector-form-model.d.ts +40 -0
  10. package/dist/setup/connector-form-model.d.ts.map +1 -0
  11. package/dist/setup/connector-form-model.js +76 -0
  12. package/dist/setup/flow-model.d.ts +49 -0
  13. package/dist/setup/flow-model.d.ts.map +1 -0
  14. package/dist/setup/flow-model.js +124 -0
  15. package/dist/setup/health-card.d.ts +25 -0
  16. package/dist/setup/health-card.d.ts.map +1 -0
  17. package/dist/setup/health-card.js +21 -0
  18. package/dist/setup/index.d.ts +11 -0
  19. package/dist/setup/index.d.ts.map +1 -0
  20. package/dist/setup/index.js +13 -0
  21. package/dist/setup/messages.d.ts +15 -0
  22. package/dist/setup/messages.d.ts.map +1 -0
  23. package/dist/setup/messages.js +249 -0
  24. package/dist/setup/records-table.d.ts +24 -0
  25. package/dist/setup/records-table.d.ts.map +1 -0
  26. package/dist/setup/records-table.js +80 -0
  27. package/dist/setup/setup-flow.d.ts +30 -0
  28. package/dist/setup/setup-flow.d.ts.map +1 -0
  29. package/dist/setup/setup-flow.js +277 -0
  30. package/dist/setup/setup-form-step.d.ts +20 -0
  31. package/dist/setup/setup-form-step.d.ts.map +1 -0
  32. package/dist/setup/setup-form-step.js +173 -0
  33. package/dist/setup/step-status-badge.d.ts +12 -0
  34. package/dist/setup/step-status-badge.d.ts.map +1 -0
  35. package/dist/setup/step-status-badge.js +21 -0
  36. package/dist/setup/tone.d.ts +8 -0
  37. package/dist/setup/tone.d.ts.map +1 -0
  38. package/dist/setup/tone.js +33 -0
  39. package/dist/setup/types.d.ts +130 -0
  40. package/dist/setup/types.d.ts.map +1 -0
  41. package/dist/setup/types.js +1 -0
  42. package/package.json +5 -1
  43. package/src/__tests__/addon-config-surface.test.tsx +248 -0
  44. package/src/__tests__/setup-components.test.tsx +147 -0
  45. package/src/__tests__/setup-flow-model.test.ts +117 -0
  46. package/src/__tests__/setup-flow.test.tsx +424 -0
  47. package/src/index.ts +25 -0
  48. package/src/setup/addon-config-surface.tsx +1262 -0
  49. package/src/setup/connector-form-model.ts +115 -0
  50. package/src/setup/flow-model.ts +156 -0
  51. package/src/setup/health-card.tsx +131 -0
  52. package/src/setup/index.ts +23 -0
  53. package/src/setup/messages.ts +263 -0
  54. package/src/setup/records-table.tsx +186 -0
  55. package/src/setup/setup-flow.tsx +562 -0
  56. package/src/setup/setup-form-step.tsx +342 -0
  57. package/src/setup/step-status-badge.tsx +47 -0
  58. package/src/setup/tone.ts +44 -0
  59. package/src/setup/types.ts +147 -0
@@ -0,0 +1,117 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import {
3
+ effectiveStatus,
4
+ flowProgress,
5
+ mergeAnswer,
6
+ nextPollDelayMs,
7
+ normalizeState,
8
+ pickActiveStep,
9
+ safeUrl,
10
+ unmetDependencies,
11
+ } from '../setup/flow-model'
12
+ import type { SetupStepDef, SetupStepState } from '../setup/types'
13
+
14
+ const defs: SetupStepDef[] = [
15
+ { key: 'domain', kind: 'form', title: 'Domain' },
16
+ { key: 'dns', kind: 'records', title: 'DNS', depends_on: ['domain'] },
17
+ { key: 'test', kind: 'action', title: 'Test', depends_on: ['dns'] },
18
+ { key: 'extras', kind: 'action', title: 'Extras', optional: true },
19
+ ]
20
+ const st = (key: string, status: SetupStepState['status'], extra: Partial<SetupStepState> = {}): SetupStepState => ({ key, status, ...extra })
21
+ const map = (...s: SetupStepState[]) => Object.fromEntries(s.map((x) => [x.key, x]))
22
+
23
+ describe('normalizeState', () => {
24
+ it('accepts array and by-key map', () => {
25
+ expect(normalizeState({ steps: [st('a', 'ok')] }).a.status).toBe('ok')
26
+ expect(normalizeState({ steps: { a: { status: 'warn' } as SetupStepState } }).a).toMatchObject({ key: 'a', status: 'warn' })
27
+ expect(normalizeState(undefined)).toEqual({})
28
+ })
29
+ })
30
+
31
+ describe('effectiveStatus / depends_on', () => {
32
+ it('blocks a step whose dependency is not satisfied', () => {
33
+ const s = map(st('domain', 'pending'))
34
+ expect(effectiveStatus(defs[1], s)).toBe('blocked')
35
+ expect(unmetDependencies(defs[1], s)).toEqual(['domain'])
36
+ })
37
+ it('unblocks when the dependency is ok, warn or skipped', () => {
38
+ expect(effectiveStatus(defs[1], map(st('domain', 'ok')))).toBe('pending')
39
+ expect(effectiveStatus(defs[1], map(st('domain', 'warn')))).toBe('pending')
40
+ expect(effectiveStatus(defs[1], map(st('domain', 'pending', { skipped: true })))).toBe('pending')
41
+ })
42
+ it('an error dependency keeps dependants blocked', () => {
43
+ expect(effectiveStatus(defs[1], map(st('domain', 'error')))).toBe('blocked')
44
+ })
45
+ it('keeps running/ok from the server and reads skipped as ok', () => {
46
+ expect(effectiveStatus(defs[1], map(st('dns', 'running')))).toBe('running')
47
+ expect(effectiveStatus(defs[3], map(st('extras', 'pending', { skipped: true })))).toBe('ok')
48
+ })
49
+ })
50
+
51
+ describe('pickActiveStep / flowProgress', () => {
52
+ it('resumes at the first step that is not satisfied and not blocked', () => {
53
+ expect(pickActiveStep(defs, map(st('domain', 'ok'), st('dns', 'error')))).toBe('dns')
54
+ expect(pickActiveStep(defs, map())).toBe('domain')
55
+ })
56
+ it('falls to the last step when everything is satisfied', () => {
57
+ expect(pickActiveStep(defs, map(st('domain', 'ok'), st('dns', 'ok'), st('test', 'ok'), st('extras', 'ok')))).toBe('extras')
58
+ })
59
+ it('optional steps do not hold completion back', () => {
60
+ const p = flowProgress(defs, map(st('domain', 'ok'), st('dns', 'ok'), st('test', 'ok')))
61
+ expect(p).toEqual({ done: 3, total: 3, percent: 100, complete: true })
62
+ })
63
+ it('an error step is not progress', () => {
64
+ const p = flowProgress(defs, map(st('domain', 'ok'), st('dns', 'error')))
65
+ expect(p.complete).toBe(false)
66
+ expect(p.done).toBe(1)
67
+ })
68
+ it('honours the server verdict', () => {
69
+ expect(flowProgress(defs, {}, true)).toMatchObject({ complete: true, percent: 100 })
70
+ })
71
+ })
72
+
73
+ describe('nextPollDelayMs', () => {
74
+ it('backs off exponentially up to the cap', () => {
75
+ expect(nextPollDelayMs(0)).toBe(2000)
76
+ expect(nextPollDelayMs(1)).toBe(3200)
77
+ expect(nextPollDelayMs(2)).toBe(5120)
78
+ expect(nextPollDelayMs(50)).toBe(30000)
79
+ })
80
+ it('retry_after_s wins, clamped to 1s..5min', () => {
81
+ expect(nextPollDelayMs(5, 45)).toBe(45000)
82
+ expect(nextPollDelayMs(0, 0.1)).toBe(1000) // floor 1s
83
+ expect(nextPollDelayMs(0, -3)).toBe(2000) // unusable hint → backoff
84
+ expect(nextPollDelayMs(0, 1)).toBe(1000)
85
+ expect(nextPollDelayMs(0, 99999)).toBe(300000)
86
+ })
87
+ })
88
+
89
+ describe('safeUrl', () => {
90
+ it('allows http(s) and app-relative, refuses the rest', () => {
91
+ expect(safeUrl('/settings')).toBe('/settings')
92
+ expect(safeUrl('https://x.test/a')).toBe('https://x.test/a')
93
+ expect(safeUrl('javascript:alert(1)')).toBeNull()
94
+ expect(safeUrl('//evil.test')).toBeNull()
95
+ expect(safeUrl('data:text/html,hi')).toBeNull()
96
+ })
97
+ it('refuses relative paths that browsers resolve to another host', () => {
98
+ expect(safeUrl('/\\evil.com/x')).toBeNull()
99
+ expect(safeUrl('/\t/evil.com')).toBeNull()
100
+ expect(safeUrl('/\n/evil.com')).toBeNull()
101
+ expect(safeUrl('\\\\evil.com')).toBeNull()
102
+ expect(safeUrl(' //evil.com ')).toBeNull()
103
+ expect(safeUrl(' javascript:alert(1)')).toBeNull()
104
+ expect(safeUrl('/ok/path?x=1#h')).toBe('/ok/path?x=1#h')
105
+ expect(safeUrl(' /padded ')).toBe('/padded')
106
+ expect(safeUrl('')).toBeNull()
107
+ })
108
+ })
109
+
110
+ describe('mergeAnswer', () => {
111
+ it('merges a step answer, replaces on a flow answer, refetches on void', () => {
112
+ const cur = map(st('a', 'pending'))
113
+ expect(mergeAnswer(cur, 'a', st('a', 'ok')).next.a.status).toBe('ok')
114
+ expect(mergeAnswer(cur, 'a', { steps: [st('b', 'ok')] }).next).toEqual({ b: st('b', 'ok') })
115
+ expect(mergeAnswer(cur, 'a', undefined).needsRefetch).toBe(true)
116
+ })
117
+ })
@@ -0,0 +1,424 @@
1
+ // @vitest-environment happy-dom
2
+ import { useState } from 'react'
3
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
4
+ import { act, cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'
5
+
6
+ vi.mock('react-i18next', () => {
7
+ const t = (k: string, o?: { defaultValue?: string }) => o?.defaultValue ?? k
8
+ return { useTranslation: () => ({ t, i18n: { language: 'es', exists: () => true } }) }
9
+ })
10
+
11
+ import { SetupFlow } from '../setup/setup-flow'
12
+ import type { SetupFlowClient, SetupFlowDef, SetupFlowState, SetupStepState } from '../setup/types'
13
+
14
+ const st = (key: string, status: SetupStepState['status'], extra: Partial<SetupStepState> = {}): SetupStepState => ({ key, status, ...extra })
15
+
16
+ const flow: SetupFlowDef = {
17
+ key: 'mail',
18
+ title: 'Configurar correo',
19
+ steps: [
20
+ {
21
+ key: 'domain',
22
+ kind: 'form',
23
+ title: 'Dominio',
24
+ fields: [
25
+ { key: 'domain', label: 'Dominio de correo', required: true },
26
+ { key: 'api_key', label: 'Llave API', type: 'secret', write_only: true, required: true },
27
+ { key: 'mode', label: 'Modo', type: 'select', options: [{ value: 'managed', label: 'Gestionado' }, { value: 'relay', label: 'Relay' }] },
28
+ { key: 'relay_host', label: 'Servidor relay', visible_when: { field: 'mode', equals: 'relay' } },
29
+ ],
30
+ },
31
+ { key: 'dns', kind: 'records', title: 'Registros DNS', depends_on: ['domain'] },
32
+ { key: 'send', kind: 'action', title: 'Enviar prueba', depends_on: ['dns'], action_label: 'Enviar correo de prueba' },
33
+ { key: 'extras', kind: 'custom', title: 'Extras', optional: true, slot: 'mail.setup.extras' },
34
+ ],
35
+ }
36
+
37
+ function makeClient(initial: SetupFlowState, over: Partial<SetupFlowClient> = {}) {
38
+ const client = {
39
+ getState: vi.fn().mockResolvedValue(initial),
40
+ runStep: vi.fn().mockResolvedValue(undefined),
41
+ skipStep: vi.fn().mockResolvedValue(undefined),
42
+ ...over,
43
+ } satisfies SetupFlowClient
44
+ return client
45
+ }
46
+
47
+ beforeEach(() => {
48
+ vi.useRealTimers()
49
+ })
50
+ afterEach(() => {
51
+ cleanup()
52
+ vi.useRealTimers()
53
+ })
54
+
55
+ describe('SetupFlow', () => {
56
+ it('resumes at the first open step and blocks dependants', async () => {
57
+ const client = makeClient({ steps: [st('domain', 'ok'), st('dns', 'warn', { records: [{ type: 'TXT', name: 'a.test', value: 'v=spf1', status: 'missing' }] })] })
58
+ render(<SetupFlow flow={flow} client={client} />)
59
+ // dns is the open step (domain is ok, dns is warn=satisfied → send is next)
60
+ await screen.findByRole('heading', { name: 'Enviar prueba' })
61
+ expect(screen.getByText('Enviar correo de prueba')).toBeTruthy()
62
+ expect(client.getState).toHaveBeenCalledTimes(1)
63
+ })
64
+
65
+ it('shows a blocked step with what it waits for and jumps to it', async () => {
66
+ const client = makeClient({ steps: [st('domain', 'pending')] })
67
+ render(<SetupFlow flow={flow} client={client} />)
68
+ await screen.findByRole('heading', { name: 'Dominio' })
69
+ fireEvent.click(screen.getByText(/2\./)) // select "Registros DNS"
70
+ expect(await screen.findByText('Antes completa: Dominio')).toBeTruthy()
71
+ fireEvent.click(screen.getByText('Ir al paso'))
72
+ expect(await screen.findByRole('heading', { name: 'Dominio' })).toBeTruthy()
73
+ })
74
+
75
+ it('runs an action step, merges the answer and shows diagnostics with a fix that runs an action', async () => {
76
+ const client = makeClient({ steps: [st('domain', 'ok'), st('dns', 'ok'), st('send', 'pending')] })
77
+ client.runStep.mockResolvedValueOnce(
78
+ st('send', 'error', { diagnostics: [{ message: 'El servidor rechazó el mensaje', fix: { label: 'Reintentar con relay', action: 'use_relay' } }] }),
79
+ )
80
+ render(<SetupFlow flow={flow} client={client} />)
81
+ fireEvent.click(await screen.findByText('Enviar correo de prueba'))
82
+ expect(await screen.findByText('El servidor rechazó el mensaje')).toBeTruthy()
83
+ expect(client.runStep).toHaveBeenCalledWith('send', undefined)
84
+ client.runStep.mockResolvedValueOnce(st('send', 'ok'))
85
+ fireEvent.click(screen.getByText('Reintentar con relay'))
86
+ await waitFor(() => expect(client.runStep).toHaveBeenCalledWith('send', { action: 'use_relay' }))
87
+ })
88
+
89
+ it('a failed runStep restores the state and shows the server message', async () => {
90
+ const client = makeClient({ steps: [st('domain', 'ok'), st('dns', 'ok'), st('send', 'pending')] })
91
+ client.runStep.mockRejectedValueOnce({ response: { data: { message: 'SMTP caído' } } })
92
+ render(<SetupFlow flow={flow} client={client} />)
93
+ fireEvent.click(await screen.findByText('Enviar correo de prueba'))
94
+ expect((await screen.findByRole('alert')).textContent).toBe('SMTP caído')
95
+ })
96
+
97
+ it('fix.step navigates inside the flow; fix.url goes through onNavigate and refuses javascript:', async () => {
98
+ const onNavigate = vi.fn()
99
+ const client = makeClient({
100
+ steps: [
101
+ st('domain', 'ok'),
102
+ st('dns', 'ok'),
103
+ st('send', 'error', {
104
+ diagnostics: [
105
+ { message: 'a', fix: { label: 'ir dns', step: 'dns' } },
106
+ { message: 'b', fix: { label: 'abrir docs', url: 'https://docs.test/x' } },
107
+ { message: 'c', fix: { label: 'malo', url: 'javascript:alert(1)' } },
108
+ ],
109
+ }),
110
+ ],
111
+ })
112
+ render(<SetupFlow flow={flow} client={client} onNavigate={onNavigate} />)
113
+ await screen.findByRole('heading', { name: 'Enviar prueba' })
114
+ fireEvent.click(screen.getByText('abrir docs'))
115
+ expect(onNavigate).toHaveBeenCalledWith('https://docs.test/x')
116
+ fireEvent.click(screen.getByText('malo'))
117
+ expect(onNavigate).toHaveBeenCalledTimes(1)
118
+ fireEvent.click(screen.getByText('ir dns'))
119
+ expect(await screen.findByRole('heading', { name: 'Registros DNS' })).toBeTruthy()
120
+ })
121
+
122
+ it('optional steps can be skipped', async () => {
123
+ const client = makeClient({ steps: [st('domain', 'ok'), st('dns', 'ok'), st('send', 'ok')] })
124
+ client.skipStep.mockResolvedValueOnce(st('extras', 'ok', { skipped: true }))
125
+ render(<SetupFlow flow={flow} client={client} renderCustom={() => <div>pantalla extra</div>} />)
126
+ await screen.findByRole('heading', { name: 'Extras' })
127
+ expect(screen.getByText('pantalla extra')).toBeTruthy()
128
+ fireEvent.click(screen.getByText('Omitir este paso'))
129
+ await waitFor(() => expect(client.skipStep).toHaveBeenCalledWith('extras'))
130
+ await waitFor(() => expect(screen.queryByText('Omitir este paso')).toBeNull())
131
+ })
132
+
133
+ it('custom steps fall back to the federated slot and say so when nothing is registered', async () => {
134
+ const client = makeClient({ steps: [st('domain', 'ok'), st('dns', 'ok'), st('send', 'ok')] })
135
+ render(<SetupFlow flow={flow} client={client} />)
136
+ expect(await screen.findByText('Este paso no tiene una pantalla disponible.')).toBeTruthy()
137
+ })
138
+
139
+ it('reports completion once, on the transition to complete', async () => {
140
+ const onComplete = vi.fn()
141
+ let push: (s: SetupFlowState) => void = () => {}
142
+ const client = makeClient({ steps: [st('domain', 'ok'), st('dns', 'ok'), st('send', 'pending')] }, {
143
+ subscribe: (cb) => {
144
+ push = cb
145
+ return () => {}
146
+ },
147
+ })
148
+ render(<SetupFlow flow={flow} client={client} onComplete={onComplete} />)
149
+ await screen.findByRole('heading', { name: 'Enviar prueba' })
150
+ expect(onComplete).not.toHaveBeenCalled()
151
+ act(() => push({ steps: [st('domain', 'ok'), st('dns', 'ok'), st('send', 'ok')] }))
152
+ expect(await screen.findByText('La configuración está completa.')).toBeTruthy()
153
+ await waitFor(() => expect(onComplete).toHaveBeenCalledTimes(1))
154
+ act(() => push({ steps: [st('domain', 'ok'), st('dns', 'ok'), st('send', 'ok')] }))
155
+ expect(onComplete).toHaveBeenCalledTimes(1)
156
+ expect(screen.getByRole('progressbar', { name: 'Progreso de la configuración' }).getAttribute('aria-valuenow')).toBe('100')
157
+ })
158
+
159
+ it('does not fire onComplete when the flow was already complete on mount', async () => {
160
+ const onComplete = vi.fn()
161
+ const client = makeClient({ steps: [st('domain', 'ok'), st('dns', 'ok'), st('send', 'ok')] })
162
+ render(<SetupFlow flow={flow} client={client} onComplete={onComplete} />)
163
+ expect(await screen.findByText('La configuración está completa.')).toBeTruthy()
164
+ await act(async () => {
165
+ await new Promise((r) => setTimeout(r, 20))
166
+ })
167
+ expect(onComplete).not.toHaveBeenCalled()
168
+ })
169
+
170
+ it('an inline client (new identity every render) does not loop requests', async () => {
171
+ const getState = vi.fn().mockResolvedValue({ steps: [st('domain', 'ok'), st('dns', 'pending')] })
172
+ function Host() {
173
+ const [n, setN] = useState(0)
174
+ return (
175
+ <>
176
+ <button onClick={() => setN(n + 1)}>rerender {n}</button>
177
+ <SetupFlow
178
+ flow={flow}
179
+ client={{ getState, runStep: vi.fn(), skipStep: vi.fn(), subscribe: () => () => {} }}
180
+ />
181
+ </>
182
+ )
183
+ }
184
+ render(<Host />)
185
+ await screen.findByRole('heading', { name: 'Registros DNS' })
186
+ fireEvent.click(screen.getByText(/rerender/))
187
+ fireEvent.click(screen.getByText(/rerender/))
188
+ await act(async () => {
189
+ await new Promise((r) => setTimeout(r, 30))
190
+ })
191
+ expect(getState).toHaveBeenCalledTimes(1)
192
+ })
193
+
194
+ it('state pushed while a run is in flight is not overwritten by its answer', async () => {
195
+ let push: (s: SetupFlowState) => void = () => {}
196
+ let resolveRun: (v: SetupStepState) => void = () => {}
197
+ const client = makeClient({ steps: [st('domain', 'ok'), st('dns', 'ok'), st('send', 'pending')] }, {
198
+ subscribe: (cb) => {
199
+ push = cb
200
+ return () => {}
201
+ },
202
+ runStep: vi.fn().mockReturnValue(new Promise<SetupStepState>((r) => (resolveRun = r))),
203
+ })
204
+ render(<SetupFlow flow={flow} client={client} />)
205
+ fireEvent.click(await screen.findByText('Enviar correo de prueba'))
206
+ // Meanwhile the server reports `extras` as done.
207
+ act(() => push({ steps: [st('domain', 'ok'), st('dns', 'ok'), st('send', 'running'), st('extras', 'ok')] }))
208
+ await act(async () => {
209
+ resolveRun(st('send', 'ok'))
210
+ })
211
+ const extrasNav = within(screen.getByRole('navigation')).getByText('Extras').closest('button')!
212
+ await waitFor(() => expect(extrasNav.textContent).toContain('Listo'))
213
+ })
214
+ it('load error offers retry', async () => {
215
+ const client = makeClient({ steps: [] })
216
+ client.getState.mockRejectedValueOnce(new Error('boom')).mockResolvedValue({ steps: [st('domain', 'pending')] })
217
+ render(<SetupFlow flow={flow} client={client} />)
218
+ expect((await screen.findByRole('alert')).textContent).toBe('boom')
219
+ fireEvent.click(screen.getByText('Reintentar'))
220
+ expect(await screen.findByRole('heading', { name: 'Dominio' })).toBeTruthy()
221
+ })
222
+
223
+ it('subscribe pushes new state', async () => {
224
+ let push: (s: SetupFlowState) => void = () => {}
225
+ const client = makeClient({ steps: [st('domain', 'ok'), st('dns', 'pending'), st('send', 'pending')] }, {
226
+ subscribe: (cb) => {
227
+ push = cb
228
+ return () => {}
229
+ },
230
+ })
231
+ render(<SetupFlow flow={flow} client={client} />)
232
+ await screen.findByRole('heading', { name: 'Registros DNS' })
233
+ act(() => push({ steps: [st('domain', 'ok'), st('dns', 'ok'), st('send', 'pending')] }))
234
+ await waitFor(() => expect(screen.getByText('Enviar correo de prueba')).toBeTruthy())
235
+ })
236
+ })
237
+
238
+ describe('SetupFlow polling', () => {
239
+ it('polls with backoff while a step runs and stops when it settles', async () => {
240
+ vi.useFakeTimers()
241
+ const client = makeClient({ steps: [st('domain', 'ok'), st('dns', 'running')] })
242
+ client.getState
243
+ .mockResolvedValueOnce({ steps: [st('domain', 'ok'), st('dns', 'running')] })
244
+ .mockResolvedValueOnce({ steps: [st('domain', 'ok'), st('dns', 'running')] })
245
+ .mockResolvedValue({ steps: [st('domain', 'ok'), st('dns', 'ok'), st('send', 'pending')] })
246
+ render(<SetupFlow flow={flow} client={client} />)
247
+ await act(async () => {
248
+ await vi.advanceTimersByTimeAsync(0)
249
+ })
250
+ expect(client.getState).toHaveBeenCalledTimes(1)
251
+ await act(async () => {
252
+ await vi.advanceTimersByTimeAsync(1900)
253
+ })
254
+ expect(client.getState).toHaveBeenCalledTimes(1) // 1st delay is 2000ms
255
+ await act(async () => {
256
+ await vi.advanceTimersByTimeAsync(200)
257
+ })
258
+ expect(client.getState).toHaveBeenCalledTimes(2)
259
+ await act(async () => {
260
+ await vi.advanceTimersByTimeAsync(3100) // 2nd delay is 3200ms → not yet
261
+ })
262
+ expect(client.getState).toHaveBeenCalledTimes(2)
263
+ await act(async () => {
264
+ await vi.advanceTimersByTimeAsync(200)
265
+ })
266
+ expect(client.getState).toHaveBeenCalledTimes(3)
267
+ // settled → no more polling
268
+ await act(async () => {
269
+ await vi.advanceTimersByTimeAsync(120000)
270
+ })
271
+ expect(client.getState).toHaveBeenCalledTimes(3)
272
+ })
273
+
274
+ it('retry_after_s from the server sets the next poll', async () => {
275
+ vi.useFakeTimers()
276
+ const client = makeClient({ steps: [st('domain', 'ok'), st('dns', 'running', { retry_after_s: 45 })] })
277
+ render(<SetupFlow flow={flow} client={client} />)
278
+ await act(async () => {
279
+ await vi.advanceTimersByTimeAsync(0)
280
+ })
281
+ await act(async () => {
282
+ await vi.advanceTimersByTimeAsync(44000)
283
+ })
284
+ expect(client.getState).toHaveBeenCalledTimes(1)
285
+ await act(async () => {
286
+ await vi.advanceTimersByTimeAsync(1500)
287
+ })
288
+ expect(client.getState).toHaveBeenCalledTimes(2)
289
+ })
290
+
291
+ it('stops polling after maxPollAttempts and offers a manual refresh', async () => {
292
+ vi.useFakeTimers()
293
+ const client = makeClient({ steps: [st('domain', 'ok'), st('dns', 'running')] })
294
+ render(<SetupFlow flow={flow} client={client} maxPollAttempts={2} />)
295
+ for (let i = 0; i < 8; i++) {
296
+ await act(async () => {
297
+ await vi.advanceTimersByTimeAsync(10000)
298
+ })
299
+ }
300
+ expect(client.getState).toHaveBeenCalledTimes(3) // mount + 2 polls
301
+ expect(screen.getByText(/Dejamos de revisar solo/)).toBeTruthy()
302
+ fireEvent.click(screen.getByText('Revisar ahora'))
303
+ await act(async () => {
304
+ await vi.advanceTimersByTimeAsync(0)
305
+ })
306
+ expect(client.getState).toHaveBeenCalledTimes(4)
307
+ })
308
+ })
309
+
310
+ describe('SetupFlow form step', () => {
311
+ it('validates required fields, never sends an empty secret and runs the step with values', async () => {
312
+ const client = makeClient({ steps: [st('domain', 'pending')] })
313
+ render(<SetupFlow flow={flow} client={client} />)
314
+ fireEvent.click(await screen.findByText('Guardar y continuar'))
315
+ expect((await screen.findByText(/Faltan campos obligatorios: Dominio de correo, Llave API/)).getAttribute('role')).toBe('alert')
316
+ expect(client.runStep).not.toHaveBeenCalled()
317
+ fireEvent.change(screen.getByLabelText(/Dominio de correo/), { target: { value: 'acme.test' } })
318
+ fireEvent.change(screen.getByLabelText(/Llave API/), { target: { value: 'sk_live_abc' } })
319
+ expect((screen.getByLabelText(/Llave API/) as HTMLInputElement).type).toBe('password')
320
+ fireEvent.click(screen.getByText('Guardar y continuar'))
321
+ await waitFor(() => expect(client.runStep).toHaveBeenCalled())
322
+ const [key, input] = client.runStep.mock.calls[0]
323
+ expect(key).toBe('domain')
324
+ expect(input.values).toMatchObject({ domain: 'acme.test', api_key: 'sk_live_abc', mode: 'managed' })
325
+ })
326
+
327
+ it('a stored write_only secret counts as filled and is not sent when empty', async () => {
328
+ const client = makeClient({ steps: [st('domain', 'pending', { values: { domain: 'acme.test' }, stored_keys: ['api_key'] })] })
329
+ render(<SetupFlow flow={flow} client={client} />)
330
+ fireEvent.click(await screen.findByText('Guardar y continuar'))
331
+ await waitFor(() => expect(client.runStep).toHaveBeenCalled())
332
+ const values = client.runStep.mock.calls[0][1].values
333
+ expect(values.domain).toBe('acme.test')
334
+ expect('api_key' in values).toBe(false)
335
+ expect((screen.getByLabelText(/Llave API/) as HTMLInputElement).placeholder).toBe('••••••••')
336
+ })
337
+
338
+ it('visible_when hides a field until its predicate holds', async () => {
339
+ const client = makeClient({ steps: [st('domain', 'pending')] })
340
+ render(<SetupFlow flow={flow} client={client} />)
341
+ await screen.findByText('Guardar y continuar')
342
+ expect(screen.queryByLabelText('Servidor relay')).toBeNull()
343
+ })
344
+
345
+ it('autosaves a draft (debounced) and shows the saved state', async () => {
346
+ const saveStep = vi.fn().mockResolvedValue(undefined)
347
+ const client = makeClient({ steps: [st('domain', 'pending')] }, { saveStep })
348
+ render(<SetupFlow flow={flow} client={client} />)
349
+ const input = await screen.findByLabelText(/Dominio de correo/)
350
+ vi.useFakeTimers()
351
+ fireEvent.change(input, { target: { value: 'acme.test' } })
352
+ await act(async () => {
353
+ await vi.advanceTimersByTimeAsync(900)
354
+ })
355
+ expect(saveStep).toHaveBeenCalledWith('domain', { domain: 'acme.test' })
356
+ expect(screen.getByText('Guardado automáticamente')).toBeTruthy()
357
+ })
358
+
359
+ it('never autosaves write-only fields; they travel only on submit', async () => {
360
+ const saveStep = vi.fn().mockResolvedValue(undefined)
361
+ const client = makeClient({ steps: [st('domain', 'pending')] }, { saveStep })
362
+ render(<SetupFlow flow={flow} client={client} />)
363
+ const input = await screen.findByLabelText(/Dominio de correo/)
364
+ const secret = screen.getByLabelText(/Llave API/) as HTMLInputElement
365
+ vi.useFakeTimers()
366
+ fireEvent.change(input, { target: { value: 'acme.test' } })
367
+ fireEvent.change(secret, { target: { value: 'sk_half' } })
368
+ await act(async () => {
369
+ await vi.advanceTimersByTimeAsync(900)
370
+ })
371
+ expect(saveStep).toHaveBeenCalledTimes(1)
372
+ expect(saveStep).toHaveBeenCalledWith('domain', { domain: 'acme.test' })
373
+ expect(secret.value).toBe('sk_half')
374
+ fireEvent.change(secret, { target: { value: 'sk_half_done' } })
375
+ await act(async () => {
376
+ await vi.advanceTimersByTimeAsync(900)
377
+ })
378
+ expect(saveStep).toHaveBeenCalledTimes(1)
379
+ fireEvent.click(screen.getByText('Guardar y continuar'))
380
+ await act(async () => {
381
+ await vi.advanceTimersByTimeAsync(0)
382
+ })
383
+ expect(client.runStep).toHaveBeenCalledWith('domain', { values: expect.objectContaining({ domain: 'acme.test', api_key: 'sk_half_done' }) })
384
+ })
385
+
386
+ it('autosave failure is visible', async () => {
387
+ const saveStep = vi.fn().mockRejectedValue(new Error('x'))
388
+ const client = makeClient({ steps: [st('domain', 'pending')] }, { saveStep })
389
+ render(<SetupFlow flow={flow} client={client} />)
390
+ const input = await screen.findByLabelText(/Dominio de correo/)
391
+ vi.useFakeTimers()
392
+ fireEvent.change(input, { target: { value: 'a' } })
393
+ await act(async () => {
394
+ await vi.advanceTimersByTimeAsync(900)
395
+ })
396
+ expect(screen.getByText('No se pudo guardar automáticamente.')).toBeTruthy()
397
+ })
398
+
399
+ it('form_layout mode=steps reuses the wizard chrome with Next/Back', async () => {
400
+ const steppedFlow: SetupFlowDef = {
401
+ key: 'f',
402
+ steps: [
403
+ {
404
+ key: 'form',
405
+ kind: 'form',
406
+ title: 'Datos',
407
+ form_layout: { mode: 'steps', sections: [{ key: 's1', title: 'Cuenta' }, { key: 's2', title: 'Avanzado' }] },
408
+ fields: [
409
+ { key: 'a', label: 'Campo A', section: 's1' },
410
+ { key: 'b', label: 'Campo B', section: 's2' },
411
+ ],
412
+ },
413
+ ],
414
+ }
415
+ const client = makeClient({ steps: [st('form', 'pending')] })
416
+ render(<SetupFlow flow={steppedFlow} client={client} />)
417
+ expect(await screen.findByLabelText('Campo A')).toBeTruthy()
418
+ expect(screen.queryByLabelText('Campo B')).toBeNull()
419
+ fireEvent.click(screen.getByText('Siguiente'))
420
+ expect(await screen.findByLabelText('Campo B')).toBeTruthy()
421
+ fireEvent.click(screen.getByText('Atrás'))
422
+ expect(await screen.findByLabelText('Campo A')).toBeTruthy()
423
+ })
424
+ })
package/src/index.ts CHANGED
@@ -189,6 +189,31 @@ export {
189
189
  export * from './barcode-scanner'
190
190
  export * from './form-layout'
191
191
  export * from './form-layout-ui'
192
+ // Setup/health: only the documented public components and their types live at
193
+ // the root; helpers (flow-model, connector-form-model, messages…) come from the
194
+ // `@asteby/metacore-runtime-react/setup` subpath so they do not pollute this namespace.
195
+ export {
196
+ SetupFlow,
197
+ SetupFormStep,
198
+ RecordsTable,
199
+ HealthCard,
200
+ StepStatusBadge,
201
+ AddonConfigSurface,
202
+ type SetupFlowProps,
203
+ type SetupCustomStepProps,
204
+ type SetupFormStepProps,
205
+ type RecordsTableProps,
206
+ type HealthCardProps,
207
+ type StepStatusBadgeProps,
208
+ type AddonConfigSurfaceProps,
209
+ type AddonConfigAddon,
210
+ type AddonConfigNotifier,
211
+ type AddonSettingDef,
212
+ type ConnectorEntry,
213
+ type ConnectorCredentialField,
214
+ type ConnectorFormLayout,
215
+ } from './setup'
216
+ export type * from './setup/types'
192
217
  export { FieldGrid, FieldCell, FieldLabel } from './field-grid'
193
218
  export {
194
219
  ActionModalDispatcher,