-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDOCS.test.js
More file actions
256 lines (207 loc) · 7.79 KB
/
Copy pathDOCS.test.js
File metadata and controls
256 lines (207 loc) · 7.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { createRequire } from 'module'
const require = createRequire(import.meta.url)
const docs_path = './src/node_modules/DOCS/index.js'
describe('DOCS sys API', () => {
let DOCS
beforeEach(() => {
delete global.__DOCS_GLOBAL_STATE__
delete require.cache[require.resolve(docs_path)]
DOCS = require(docs_path)
})
afterEach(() => {
vi.restoreAllMocks()
})
function create_docs () {
return DOCS('test_component.js')('sid_1')
}
function create_action () {
return {
name: 'Open File',
info: 'Open the selected file.',
icon: 'file',
status: {},
steps: []
}
}
it('runs wrap_isolated handlers in docs mode', async () => {
const docs = create_docs()
docs.admin.set_docs_mode(true)
const handler = docs.wrap_isolated(
'function (event, sys) { event.ran = true; event.docs_mode = sys.is_docs_mode() }',
'Isolated docs'
)
const event = {
preventDefault: vi.fn(),
stopPropagation: vi.fn()
}
await handler(event)
expect(event.ran).toBe(true)
expect(event.docs_mode).toBe(true)
expect(event.preventDefault).not.toHaveBeenCalled()
expect(event.stopPropagation).not.toHaveBeenCalled()
})
it('keeps docs.wrap blocking behavior in docs mode', async () => {
const docs = create_docs()
const displays = []
let ran = false
docs.admin.set_doc_display_handler(display => displays.push(display))
docs.admin.set_docs_mode(true)
const handler = docs.wrap(function onclick () { ran = true }, 'Wrapped docs')
const event = {
preventDefault: vi.fn(),
stopPropagation: vi.fn()
}
await handler(event)
expect(ran).toBe(false)
expect(event.preventDefault).toHaveBeenCalled()
expect(event.stopPropagation).toHaveBeenCalled()
expect(displays).toEqual([{ content: 'Wrapped docs', sid: 'sid_1' }])
})
it('shows action info from sys.trigger_action in docs mode', async () => {
const docs = create_docs()
const displays = []
docs.register_actions([create_action()])
docs.admin.set_doc_display_handler(display => displays.push(display))
docs.admin.set_docs_mode(true)
const handler = docs.wrap_isolated(
'function (event, sys) { event.result = sys.trigger_action("Open File", { channel: "up", type: "selected_action" }) }',
'Trigger docs'
)
const event = {}
await handler(event)
expect(event.result).toBe(true)
expect(displays).toEqual([{ content: 'Open the selected file.', sid: 'sid_1' }])
})
it('sends action messages from sys.trigger_action in normal mode', async () => {
const docs = create_docs()
const sent = []
docs.set_sys({
_: {
up: function up (type, refs, data) {
sent.push({ type, refs, data })
return ['sid_1', 'parent', 0]
}
}
})
const handler = docs.wrap_isolated(
'function (event, sys) { event.head = sys.trigger_action({ name: "Open File", info: "Open the selected file.", icon: "file", status: {}, steps: [] }, { channel: "up", type: "selected_action", refs: { source: "test" } }) }',
'Trigger docs'
)
const event = {}
await handler(event)
expect(event.head).toEqual(['sid_1', 'parent', 0])
expect(sent).toEqual([
{
type: 'selected_action',
refs: { source: 'test' },
data: create_action()
}
])
})
it('returns empty subscriptions from sys.sdb.watch in docs mode', async () => {
const docs = create_docs()
docs.admin.set_docs_mode(true)
const handler = docs.wrap_isolated(
'async function (event, sys) { event.subs = await sys.sdb.watch(function onbatch () {}) }',
'Watch docs'
)
const event = {}
await handler(event)
expect(event.subs).toEqual([])
})
it('suppresses missing sys resources instead of throwing', async () => {
const docs = create_docs()
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
const handler = docs.wrap_isolated(
'function (event, sys) { event.sent = sys._.up("selected_action", {}, {}); event.file = sys.drive.get("missing.json"); event.wrote = sys.drive.put("missing.json", {}); event.subs = sys.sdb.watch(function onbatch () {}) }',
'Missing resources docs'
)
const event = {}
await handler(event)
expect(event.sent).toBe(false)
await expect(event.file).resolves.toEqual({ raw: null, path: 'missing.json' })
await expect(event.wrote).resolves.toBe(false)
await expect(event.subs).resolves.toEqual([])
expect(warn).toHaveBeenCalled()
})
it('keeps sys.state across calls and clears it when docs mode deactivates', async () => {
const docs = create_docs()
docs.admin.set_docs_mode(true)
const handler = docs.wrap_isolated(
'function (event, sys) { sys.state.clicks = (sys.state.clicks || 0) + 1; event.clicks = sys.state.clicks }',
'Stateful docs'
)
const event1 = {}
const event2 = {}
await handler(event1)
await handler(event2)
expect(event1.clicks).toBe(1)
expect(event2.clicks).toBe(2)
docs.admin.set_docs_mode(false)
const event3 = {}
await handler(event3)
expect(event3.clicks).toBe(1)
})
it('lists registered actions and handler docs in get_toc', () => {
vi.spyOn(console, 'warn').mockImplementation(() => {})
const docs = create_docs()
docs.register_actions([create_action()])
docs.wrap_isolated('function (event, sys) {}', '# Handler One\nDocs.')
docs.wrap(function onclick () {}, '# Handler Two\nDocs.')
const toc = docs.get_toc()
expect(toc.actions).toHaveLength(1)
expect(toc.actions[0].name).toBe('Open File')
expect(toc.handlers).toHaveLength(2)
expect(toc.handlers[0].doc).toBe('# Handler One\nDocs.')
expect(toc.handlers[1].doc).toBe('# Handler Two\nDocs.')
})
it('does not duplicate handler docs for repeated wraps and clears via clear_handler_docs', () => {
vi.spyOn(console, 'warn').mockImplementation(() => {})
const docs = create_docs()
docs.wrap_isolated('function (event, sys) {}', '# Same\nDocs.')
docs.wrap_isolated('function (event, sys) {}', '# Same\nDocs.')
docs.wrap(function onclick () {}, '# Other\nDocs.')
expect(docs.get_toc().handlers).toHaveLength(2)
docs.clear_handler_docs()
expect(docs.get_toc().handlers).toHaveLength(0)
docs.wrap_isolated('function (event, sys) {}', '# Same\nDocs.')
expect(docs.get_toc().handlers).toHaveLength(1)
})
it('shows doc instead of running wrap_isolated when run_in_docs_mode is false', async () => {
const docs = create_docs()
const displays = []
docs.admin.set_doc_display_handler(display => displays.push(display))
docs.admin.set_docs_mode(true)
const handler = docs.wrap_isolated(
'function (event, sys) { event.ran = true }',
'# Show Doc\nShown in docs mode.',
{ run_in_docs_mode: false }
)
const event = { preventDefault: vi.fn(), stopPropagation: vi.fn() }
await handler(event)
expect(event.ran).toBeUndefined()
expect(event.preventDefault).toHaveBeenCalled()
expect(event.stopPropagation).toHaveBeenCalled()
expect(displays).toEqual([{ content: '# Show Doc\nShown in docs mode.', sid: 'sid_1' }])
})
it('runs wrap_isolated body in normal mode when run_in_docs_mode is false', async () => {
const docs = create_docs()
const sent = []
docs.set_sys({
_: {
up: function up (type, refs, data) {
sent.push({ type, refs, data })
return ['sid_1', 'parent', 0]
}
}
})
const handler = docs.wrap_isolated(
'function (event, sys) { sys._.up("clicked", {}, null) }',
'# Click\nSends a click.',
{ run_in_docs_mode: false }
)
await handler({})
expect(sent).toEqual([{ type: 'clicked', refs: {}, data: null }])
})
})