-
Notifications
You must be signed in to change notification settings - Fork 1
/
spawn-invoke.test.ts
137 lines (113 loc) · 3.83 KB
/
spawn-invoke.test.ts
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
import {
UserState,
ChangeUserNameStatus,
ChangeUserNameResponse,
ChangeUserName
} from './protos/user_test'
import * as crypto from 'crypto'
import spawn, { payloadFor, SpawnSystem } from '../src/spawn'
import { createRandomActor, createUserActor } from './stubs/actors'
import { describe, beforeAll, afterAll, test, expect } from 'bun:test'
describe('testing invoke', () => {
const randomActorName = crypto.randomUUID()
let system: SpawnSystem
beforeAll(async () => {
system = spawn.createSystem('SpawnSysTest')
createUserActor(system)
createRandomActor(system, randomActorName)
const registered = await system.register()
if (registered.status?.message != 'Accepted') throw new Error('Failed to register system')
})
afterAll(async () => {
await system.destroy()
})
test('using default proxy function "getState" to get the current state in a random actor', async () => {
const userState = await spawn.invoke(randomActorName, {
action: 'getState',
response: UserState,
system: 'SpawnSysTest'
})
expect(userState.name).toBe('')
})
// never use this for action discovery purpose
test('invoking non existing function throws SpawnInvocationError', async () => {
try {
await spawn.invoke(randomActorName, {
action: 'unknown',
response: UserState,
system: 'SpawnSysTest'
})
} catch (ex: any) {
expect(ex.name).toBe('SpawnInvocationError')
}
})
test('settting new name and getting it correctly after', async () => {
const expected = ChangeUserNameResponse.create({
status: ChangeUserNameStatus.OK,
newName: 'novo_nome'
})
const payload = payloadFor(ChangeUserName, ChangeUserName.create({ newName: 'novo_nome' }))
const action = 'setName'
const newNameResponse = await spawn.invoke('MockUserActor', {
action,
payload,
response: ChangeUserNameResponse,
system: 'SpawnSysTest'
})
expect(newNameResponse.newName).toBe(expected.newName)
expect(newNameResponse.status).toBe(expected.status)
const userState = await spawn.invoke(`MockUserActor`, {
action: 'getState',
response: UserState,
system: 'SpawnSysTest'
})
expect(userState.name).toBe('novo_nome')
})
test('settting new name by tags and invoke metadata', async () => {
const expected = ChangeUserNameResponse.create({
status: ChangeUserNameStatus.OK,
newName: 'newNameMeta'
})
const payload = payloadFor(
ChangeUserName,
ChangeUserName.create({ newName: 'newNameToIgnore' })
)
const action = 'setNameMetadata'
const newNameResponse = await spawn.invoke('MockUserActor', {
action,
payload,
metadata: { metakey: 'newNameMeta' },
response: ChangeUserNameResponse,
system: 'SpawnSysTest'
})
expect(newNameResponse.newName).toBe(expected.newName)
expect(newNameResponse.status).toBe(expected.status)
const resp = await spawn.invoke('MockUserActor', {
action: 'getTagsName',
response: ChangeUserNameResponse,
system: 'SpawnSysTest'
})
expect(resp.newName).toBe('newNameMeta-initialTags-initTag')
const userState = await spawn.invoke(`MockUserActor`, {
action: 'getState',
response: UserState,
system: 'SpawnSysTest'
})
expect(userState.name).toBe('newNameMeta')
})
test('invoking noreply async function and changing internal state with it', async () => {
const action = 'noreplyChangeName'
const invokeAsync = await spawn.invoke('MockUserActor', {
action,
async: true,
system: 'SpawnSysTest'
})
const stateChanged = await spawn.invoke('MockUserActor', {
system: 'SpawnSysTest',
action: 'get',
response: UserState
})
expect(invokeAsync).toBeNull()
expect(stateChanged.name).toBe('noreplyNameOk')
})
})