-
Notifications
You must be signed in to change notification settings - Fork 463
Expand file tree
/
Copy pathfiber.spec.ts
More file actions
220 lines (196 loc) · 7.03 KB
/
Copy pathfiber.spec.ts
File metadata and controls
220 lines (196 loc) · 7.03 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
import { Context, FiberState, Service } from '../src'
import { expect, describe, it, vi } from 'vitest'
import { mock } from 'node:test'
import { event, sleep, withTimers } from './utils'
describe('Fiber', () => {
it('inertia lock 1', withTimers(async (root) => {
const dispose = root.provide('foo', 1)
const fiber = root.inject(['foo'], async () => {
await sleep(1000)
return () => sleep(1000)
})
await vi.advanceTimersByTimeAsync(400) // 400
expect(fiber.state).to.equal(FiberState.LOADING)
dispose()
await vi.advanceTimersByTimeAsync(400) // 800
expect(fiber.state).to.equal(FiberState.LOADING)
await vi.advanceTimersByTimeAsync(400) // 1200
expect(fiber.state).to.equal(FiberState.UNLOADING)
root.provide('foo', 1)
await vi.advanceTimersByTimeAsync(1000) // 2200
expect(fiber.state).to.equal(FiberState.LOADING)
await vi.advanceTimersByTimeAsync(1000) // 3200
expect(fiber.state).to.equal(FiberState.ACTIVE)
}))
it('inertia lock 2', withTimers(async (root) => {
const dispose = root.provide('foo', 1)
const fiber = root.inject(['foo'], async () => {
await sleep(1000)
return () => sleep(1000)
})
await vi.advanceTimersByTimeAsync(400) // 400
expect(fiber.state).to.equal(FiberState.LOADING)
dispose()
await vi.advanceTimersByTimeAsync(400) // 800
expect(fiber.state).to.equal(FiberState.LOADING)
root.provide('foo', 2)
await vi.advanceTimersByTimeAsync(400) // 1200
expect(fiber.state).to.equal(FiberState.ACTIVE)
}))
it('inertia lock 3', withTimers(async (root) => {
class Foo extends Service {
constructor(ctx: Context) {
super(ctx, 'foo')
}
}
const provider = await root.plugin(Foo)
const fiber = root.inject(['foo'], async () => {
await sleep(1000)
return () => sleep(1000)
})
await vi.advanceTimersByTimeAsync(400) // 400
expect(fiber.state).to.equal(FiberState.LOADING)
await vi.runAllTimersAsync() // 1000
expect(fiber.state).to.equal(FiberState.ACTIVE)
await Promise.all([
provider.dispose(),
vi.runAllTimersAsync(), // 2000
])
expect(fiber.state).to.equal(FiberState.PENDING)
}))
it('plugin error', async () => {
const root = new Context()
const callback = mock.fn()
const error = mock.fn()
;(root.logger as any).error = error
const apply = mock.fn((ctx: Context, config: { foo?: boolean } | undefined) => {
ctx.on(event, callback)
if (!config?.foo) throw new Error('plugin error')
})
const fiber1 = root.plugin(apply)
const fiber2 = root.plugin(apply, { foo: true })
await sleep()
expect(fiber1.state).to.equal(FiberState.FAILED)
expect(fiber2.state).to.equal(FiberState.ACTIVE)
// expect(apply.mock.calls).to.have.length(2)
expect(error.mock.calls).to.have.length(1)
root.emit(event)
expect(callback.mock.calls).to.have.length(1)
})
it('dispose error', async () => {
const root = new Context()
const error = mock.fn()
;(root.logger as any).error = error
const dispose = mock.fn(() => {
throw new Error('test')
})
const plugin = (ctx: Context) => {
return dispose
}
const fiber = await root.plugin(plugin)
expect(dispose.mock.calls).to.have.length(0)
await expect(fiber.dispose()).resolves.toBeUndefined()
await sleep()
expect(dispose.mock.calls).to.have.length(1)
expect(error.mock.calls).to.have.length(1)
})
it('update config on wrapped fiber', async () => {
const root = new Context()
const callback = mock.fn()
const fiber = root.plugin(callback, { msg: 'hello' })
await fiber
expect(callback.mock.calls).to.have.length(1)
expect(callback.mock.calls[0].arguments[1]).to.deep.equal({ msg: 'hello' })
fiber.update({ msg: 'world' })
await fiber
expect(callback.mock.calls).to.have.length(2)
expect(callback.mock.calls[1].arguments[1]).to.deep.equal({ msg: 'world' })
fiber.update({ msg: '!!!' })
await fiber
expect(callback.mock.calls).to.have.length(3)
expect(callback.mock.calls[2].arguments[1]).to.deep.equal({ msg: '!!!' })
})
it('restart wrapped fiber', async () => {
const root = new Context()
const callback = mock.fn()
const fiber = root.plugin(callback)
await fiber
await fiber.restart()
expect(callback.mock.calls).to.have.length(2)
expect(fiber.state).to.equal(FiberState.ACTIVE)
expect(Object.hasOwn(fiber, 'state')).to.equal(false)
expect(Object.hasOwn(fiber, 'inertia')).to.equal(false)
})
it('update config while injected service reloads', async () => {
const applied: [number, string][] = []
class Provider extends Service {
value: number
constructor(ctx: Context, config: { value: number }) {
super(ctx, 'provider')
this.value = config.value
}
}
const Consumer = {
inject: ['provider'],
apply(ctx: Context, config: { mode: string }) {
applied.push([(ctx as any).provider.value, config.mode])
},
}
const root = new Context()
const provider = root.plugin(Provider, { value: 1 })
const consumer = root.plugin(Consumer, { mode: 'old' })
await provider
await consumer
provider.update({ value: 2 })
consumer.update({ mode: 'new' })
await Promise.all([provider.await(), consumer.await()])
const base = Object.getPrototypeOf(consumer)
expect(applied).to.deep.equal([
[1, 'old'],
[2, 'new'],
])
expect(consumer.config).to.equal(base.config)
expect(consumer.state).to.equal(base.state)
expect(consumer.state).to.equal(FiberState.ACTIVE)
expect(Object.hasOwn(consumer, 'config')).to.equal(false)
expect(Object.hasOwn(consumer, 'state')).to.equal(false)
expect(Object.hasOwn(consumer, 'inertia')).to.equal(false)
})
it('refuses effect registration while UNLOADING (G5 residue)', withTimers(async (root) => {
// roadmap 74(a): an undo that registers a new effect during deactivation
// must be refused (INACTIVE_EFFECT), not accepted-and-leaked. The
// deactivation path: provider.dispose() withdraws 'svc' -> Rogue
// deactivates (passes through UNLOADING) -> its generator effect's undo
// runs -> ctx.effect() inside must throw.
let leaked = false
let leakDisposed = false
const Provider = {
name: 'Provider',
apply(ctx: any) {
ctx.provide('svc', {})
},
}
const Rogue = {
name: 'Rogue',
inject: ['svc'],
apply(ctx: any) {
ctx.effect(function* () {
yield () => {
ctx.effect(() => {
leaked = true
return () => { leakDisposed = true }
})
}
})
},
}
const provider = await root.plugin(Provider)
const rogue = await root.plugin(Rogue)
await provider.dispose() // withdraw svc -> Rogue deactivates -> undo runs
expect(leaked).to.equal(false)
expect(rogue.state).to.equal(FiberState.PENDING)
expect(rogue.getEffects().length).to.equal(0)
await rogue.dispose()
expect(leakDisposed).to.equal(false)
}))
})