-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_yamamva_ffi.py
More file actions
338 lines (259 loc) · 8.68 KB
/
test_yamamva_ffi.py
File metadata and controls
338 lines (259 loc) · 8.68 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import ctypes
import json
import os
import sys
LIB_PATH = os.path.join(os.path.dirname(__file__), "target", "release", "libyamamva.so")
if not os.path.exists(LIB_PATH):
dylib = LIB_PATH.replace(".so", ".dylib")
if os.path.exists(dylib):
LIB_PATH = dylib
lib = ctypes.cdll.LoadLibrary(LIB_PATH)
class FfiElement(ctypes.Structure):
_fields_ = [
("key", ctypes.c_char_p),
("label", ctypes.c_char_p),
("extra_json", ctypes.c_char_p),
]
class FfiArgs(ctypes.Structure):
_fields_ = [
("node_type", ctypes.c_char_p),
("node_json", ctypes.c_char_p),
("element_count", ctypes.c_uint32),
("elements", ctypes.POINTER(FfiElement)),
("result", ctypes.c_char_p),
]
lib.yamamva_load.argtypes = [ctypes.c_char_p, ctypes.c_uint32]
lib.yamamva_load.restype = ctypes.c_void_p
lib.yamamva_register.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_int32, ctypes.c_int32]
lib.yamamva_register.restype = None
lib.yamamva_exec.argtypes = [ctypes.c_void_p, ctypes.POINTER(FfiArgs)]
lib.yamamva_exec.restype = ctypes.c_int32
lib.yamamva_get_state.argtypes = [ctypes.c_void_p, ctypes.c_char_p]
lib.yamamva_get_state.restype = ctypes.c_void_p
lib.yamamva_meta.argtypes = [ctypes.c_void_p, ctypes.c_char_p]
lib.yamamva_meta.restype = ctypes.c_void_p
lib.yamamva_save.argtypes = [ctypes.c_void_p]
lib.yamamva_save.restype = ctypes.c_void_p
lib.yamamva_restore.argtypes = [ctypes.c_char_p, ctypes.c_uint32, ctypes.c_char_p]
lib.yamamva_restore.restype = ctypes.c_void_p
lib.yamamva_free.argtypes = [ctypes.c_void_p]
lib.yamamva_free.restype = None
lib.yamamva_free_string.argtypes = [ctypes.c_void_p]
lib.yamamva_free_string.restype = None
YAMAMVA_END = -1
YAMAMVA_PASS = 0
YAMAMVA_BLOCKING = 1
CMD_BG = 1
CMD_TEXT = 2
CMD_SPEAKER = 3
CMD_MENU = 4
CMD_MXBS = 5
def read_and_free(ptr):
if not ptr:
return None
s = ctypes.cast(ptr, ctypes.c_char_p).value.decode("utf-8")
lib.yamamva_free_string(ptr)
return s
# ─── Test 1: Simple linear scenario ───
print("=== Test 1: Linear flow ===")
yaml1 = b"""
id: test_linear
title: Linear Test
entry: scene_start
scenes:
scene_start:
- bg: lobby
- text: "Hello world"
- speaker: elmar
text: "Hi!"
- end: true
"""
h = lib.yamamva_load(yaml1, len(yaml1))
assert h, "load failed"
lib.yamamva_register(h, b"bg", CMD_BG, YAMAMVA_PASS)
lib.yamamva_register(h, b"text", CMD_TEXT, YAMAMVA_PASS)
lib.yamamva_register(h, b"speaker", CMD_SPEAKER, YAMAMVA_PASS)
args = FfiArgs()
results = []
for _ in range(10):
cmd = lib.yamamva_exec(h, ctypes.byref(args))
if cmd == YAMAMVA_END:
results.append("END")
break
nt = args.node_type.decode("utf-8") if args.node_type else "?"
results.append((cmd, nt))
assert results == [(CMD_BG, "bg"), (CMD_TEXT, "text"), (CMD_SPEAKER, "speaker"), "END"]
print(f" OK: {results}")
lib.yamamva_free(h)
# ─── Test 2: Blocking + incase ───
print("\n=== Test 2: Blocking + incase ===")
yaml2 = b"""
id: test_blocking
title: Blocking Test
entry: scene_start
state:
heard: false
scenes:
scene_start:
- bg: lobby
- hearingmenu:
style: vertical
elements:
- { key: elmar, label: "Go Elmar" }
- { key: leave, label: "Leave" }
- incase:
- when: "$result == 'elmar'"
do: { heard: true }
next: scene_elmar
- next: scene_bye
scene_elmar:
- text: "Elmar says hi"
- end: true
scene_bye:
- text: "Goodbye"
- end: true
"""
h = lib.yamamva_load(yaml2, len(yaml2))
assert h, "load failed"
lib.yamamva_register(h, b"bg", CMD_BG, YAMAMVA_PASS)
lib.yamamva_register(h, b"text", CMD_TEXT, YAMAMVA_PASS)
lib.yamamva_register(h, b"hearingmenu", CMD_MENU, YAMAMVA_BLOCKING)
args = FfiArgs()
# Step 1: bg
cmd = lib.yamamva_exec(h, ctypes.byref(args))
assert cmd == CMD_BG
print(f" Step 1: cmd={cmd} type={args.node_type.decode()}")
# Step 2: hearingmenu (BLOCKING)
cmd = lib.yamamva_exec(h, ctypes.byref(args))
assert cmd == CMD_MENU
assert args.element_count == 2
el0_key = args.elements[0].key.decode("utf-8")
el1_key = args.elements[1].key.decode("utf-8")
print(f" Step 2: cmd={cmd} type={args.node_type.decode()} elements=[{el0_key}, {el1_key}]")
# Player chooses "elmar" - keep reference alive
result_buf = ctypes.c_char_p(b"elmar")
args.result = result_buf
# Step 3: incase processes internally, lands on scene_elmar text
cmd = lib.yamamva_exec(h, ctypes.byref(args))
assert cmd == CMD_TEXT
nj = json.loads(args.node_json.decode("utf-8"))
assert "Elmar says hi" in nj.get("text", "")
print(f" Step 3: cmd={cmd} text={nj['text']}")
# Step 4: end
cmd = lib.yamamva_exec(h, ctypes.byref(args))
assert cmd == YAMAMVA_END
print(f" Step 4: END")
# Check state
ptr = lib.yamamva_get_state(h, b"heard")
val = read_and_free(ptr)
assert val == "true", f"expected 'true', got '{val}'"
print(f" State heard = {val}")
lib.yamamva_free(h)
# ─── Test 3: Save / Restore ───
print("\n=== Test 3: Save / Restore ===")
yaml3 = b"""
id: test_save
title: Save Test
entry: scene_start
state:
counter: 0
scenes:
scene_start:
- bg: lobby
- do:
counter: "counter + 1"
- text: "After do"
- text: "Final text"
- end: true
"""
h = lib.yamamva_load(yaml3, len(yaml3))
assert h, "load failed"
lib.yamamva_register(h, b"bg", CMD_BG, YAMAMVA_PASS)
lib.yamamva_register(h, b"text", CMD_TEXT, YAMAMVA_PASS)
args = FfiArgs()
# bg
cmd = lib.yamamva_exec(h, ctypes.byref(args))
assert cmd == CMD_BG
# do is consumed internally, "After do" returned
cmd = lib.yamamva_exec(h, ctypes.byref(args))
assert cmd == CMD_TEXT
# Save here
save_ptr = lib.yamamva_save(h)
save_json = read_and_free(save_ptr)
save_data = json.loads(save_json)
print(f" Saved: counter={save_data['state']['counter']}, scene={save_data['scene_id']}")
assert save_data["state"]["counter"] == 1
lib.yamamva_free(h)
# Restore
save_bytes = save_json.encode("utf-8")
h2 = lib.yamamva_restore(yaml3, len(yaml3), save_bytes)
assert h2, "restore failed"
lib.yamamva_register(h2, b"bg", CMD_BG, YAMAMVA_PASS)
lib.yamamva_register(h2, b"text", CMD_TEXT, YAMAMVA_PASS)
args2 = FfiArgs()
cmd = lib.yamamva_exec(h2, ctypes.byref(args2))
assert cmd == CMD_TEXT
nj = json.loads(args2.node_json.decode("utf-8"))
assert "Final text" in nj.get("text", "")
print(f" Restored, next text: {nj['text']}")
cmd = lib.yamamva_exec(h2, ctypes.byref(args2))
assert cmd == YAMAMVA_END
print(f" END after restore")
lib.yamamva_free(h2)
# ─── Test 4: Meta API ───
print("\n=== Test 4: Meta API ===")
h = lib.yamamva_load(yaml2, len(yaml2))
assert h, "load failed"
ptr = lib.yamamva_meta(h, b"state")
state_json = read_and_free(ptr)
state = json.loads(state_json)
print(f" Initial state: {state}")
assert state.get("heard") == False
lib.yamamva_free(h)
# ─── Test 5: Oyatsu ADV (full scenario) ───
print("\n=== Test 5: Oyatsu ADV full scenario ===")
yaml_path = os.path.join(os.path.dirname(__file__), "examples", "oyatsu_adv.yaml")
with open(yaml_path, "r") as f:
yaml_full = f.read().encode("utf-8")
h = lib.yamamva_load(yaml_full, len(yaml_full))
assert h, "load failed"
lib.yamamva_register(h, b"bg", CMD_BG, YAMAMVA_PASS)
lib.yamamva_register(h, b"text", CMD_TEXT, YAMAMVA_PASS)
lib.yamamva_register(h, b"speak", CMD_SPEAKER, YAMAMVA_PASS)
lib.yamamva_register(h, b"hearingmenu", CMD_MENU, YAMAMVA_BLOCKING)
lib.yamamva_register(h, b"accusemenu", CMD_MXBS, YAMAMVA_BLOCKING)
args = FfiArgs()
trace = []
# Script: menu → choose elmar → hear_elmar → menu → choose accuse → accuse → choose elmar → win
menu_choices = ["elmar", "accuse", "elmar"]
menu_idx = 0
for step in range(200):
cmd = lib.yamamva_exec(h, ctypes.byref(args))
if cmd == YAMAMVA_END:
trace.append("END")
break
nt = args.node_type.decode("utf-8") if args.node_type else "?"
trace.append(nt)
if cmd in (CMD_MENU, CMD_MXBS) and args.element_count > 0:
chosen = None
if menu_idx < len(menu_choices):
chosen = menu_choices[menu_idx]
else:
chosen = args.elements[0].key.decode("utf-8") if args.elements[0].key else ""
result_buf = ctypes.c_char_p(chosen.encode("utf-8"))
args.result = result_buf
menu_idx += 1
node_types = [t for t in trace if t != "END"]
print(f" Total nodes dispatched: {len(node_types)}")
print(f" Node types: {set(node_types)}")
print(f" Ended: {'END' in trace}")
assert "END" in trace, "Scenario did not reach END"
# Check final state
ptr = lib.yamamva_get_state(h, b"heard_elmar")
val = read_and_free(ptr)
print(f" heard_elmar = {val}")
assert val == "true", f"expected heard_elmar=true, got {val}"
lib.yamamva_free(h)
print("\n" + "=" * 40)
print("=== ALL FFI TESTS PASSED ===")
print("=" * 40)