pasta_lua 0.2.4

Pasta Lua - Lua integration for Pasta DSL
Documentation
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
//! Integration tests for pasta.shiori.event module – core dispatching.
//!
//! Tests verify REG/EVENT module loading, event dispatching, error handling,
//! default boot handlers, and integration with the RES module.

use crate::common;

use common::create_runtime_with_pasta_path;

// ============================================================================
// Task 1.1, 3.1: REG Module Tests
// ============================================================================

#[test]
fn test_reg_module_loads() {
    let runtime = create_runtime_with_pasta_path();

    let result = runtime.exec(
        r#"
        local REG = require "pasta.shiori.event.register"
        return REG ~= nil
    "#,
    );

    assert!(result.is_ok(), "REG module should load: {:?}", result);
    assert!(result.unwrap().as_boolean().unwrap_or(false));
}

#[test]
fn test_reg_module_exports_empty_table() {
    let runtime = create_runtime_with_pasta_path();

    // Note: Before EVENT module is loaded, REG should be empty.
    // After EVENT module loads boot.lua, REG.OnBoot will be set.
    let result = runtime.exec(
        r#"
        local REG = require "pasta.shiori.event.register"
        return type(REG) == "table" and next(REG) == nil
    "#,
    );

    assert!(result.is_ok(), "REG should be empty table: {:?}", result);
    assert!(result.unwrap().as_boolean().unwrap_or(false));
}

#[test]
fn test_reg_allows_handler_registration() {
    let runtime = create_runtime_with_pasta_path();

    let result = runtime.exec(
        r#"
        local REG = require "pasta.shiori.event.register"
        REG.OnBoot = function(act) return "test" end
        return type(REG.OnBoot) == "function"
    "#,
    );

    assert!(
        result.is_ok(),
        "REG should allow handler registration: {:?}",
        result
    );
    assert!(result.unwrap().as_boolean().unwrap_or(false));
}

// ============================================================================
// Task 2.1-2.7, 3.2-3.4: EVENT Module Tests
// ============================================================================

#[test]
fn test_event_module_loads() {
    let runtime = create_runtime_with_pasta_path();

    let result = runtime.exec(
        r#"
        local EVENT = require "pasta.shiori.event"
        return EVENT ~= nil
    "#,
    );

    assert!(result.is_ok(), "EVENT module should load: {:?}", result);
    assert!(result.unwrap().as_boolean().unwrap_or(false));
}

#[test]
fn test_event_no_entry_returns_nil_when_scene_not_found() {
    let runtime = create_runtime_with_pasta_path();

    let result = runtime.exec(
        r#"
        local EVENT = require "pasta.shiori.event"
        local SHIORI_ACT = require "pasta.shiori.act"
        local act = SHIORI_ACT.new({}, { id = "UnknownEvent", method = "get", version = 30 })
        local response = EVENT.no_entry(act)
        -- EVENT.no_entry now returns thread|nil, not SHIORI response
        -- When no scene is found, it returns nil
        return response == nil
    "#,
    );

    assert!(
        result.is_ok(),
        "EVENT.no_entry should return nil when scene not found: {:?}",
        result
    );
    assert!(result.unwrap().as_boolean().unwrap_or(false));
}

#[test]
fn test_event_fire_dispatches_registered_handler() {
    let runtime = create_runtime_with_pasta_path();

    let result = runtime.exec(
        r#"
        local REG = require "pasta.shiori.event.register"
        local EVENT = require "pasta.shiori.event"
        local RES = require "pasta.shiori.res"
        
        REG.OnTest = function(act)
            return RES.ok("test response")
        end
        
        local req = { id = "OnTest", method = "get", version = 30 }
        local response = EVENT.fire(req)
        
        return response:find("200 OK") ~= nil and response:find("Value: test response") ~= nil
    "#,
    );

    assert!(
        result.is_ok(),
        "EVENT.fire should dispatch to registered handler: {:?}",
        result
    );
    assert!(result.unwrap().as_boolean().unwrap_or(false));
}

#[test]
fn test_event_fire_returns_no_content_for_unregistered() {
    let runtime = create_runtime_with_pasta_path();

    let result = runtime.exec(
        r#"
        local EVENT = require "pasta.shiori.event"
        local req = { id = "UnregisteredEvent", method = "get", version = 30 }
        local response = EVENT.fire(req)
        return response:find("204 No Content") ~= nil
    "#,
    );

    assert!(
        result.is_ok(),
        "EVENT.fire should return 204 for unregistered event: {:?}",
        result
    );
    assert!(result.unwrap().as_boolean().unwrap_or(false));
}

#[test]
fn test_event_fire_handles_nil_id() {
    let runtime = create_runtime_with_pasta_path();

    let result = runtime.exec(
        r#"
        local EVENT = require "pasta.shiori.event"
        local req = { method = "get", version = 30 }  -- no id field
        local response = EVENT.fire(req)
        -- When req.id is nil, REG[nil] is nil, so no_entry is called
        -- no_entry expects act.req.id but will get nil from act.req
        return response:find("204 No Content") ~= nil
    "#,
    );

    assert!(
        result.is_ok(),
        "EVENT.fire should return 204 for nil id: {:?}",
        result
    );
    assert!(result.unwrap().as_boolean().unwrap_or(false));
}

#[test]
fn test_event_fire_catches_handler_error() {
    let runtime = create_runtime_with_pasta_path();

    let result = runtime.exec(
        r#"
        local REG = require "pasta.shiori.event.register"
        local SHIORI = require "pasta.shiori.entry"
        
        REG.OnError = function(act)
            error("Test error message")
        end
        
        local req = { id = "OnError", method = "get", version = 30 }
        local response = SHIORI.request(req)
        
        return response:find("500 Internal Server Error") ~= nil
    "#,
    );

    assert!(
        result.is_ok(),
        "SHIORI.request should return 500 on handler error: {:?}",
        result
    );
    assert!(result.unwrap().as_boolean().unwrap_or(false));
}

#[test]
fn test_error_message_no_newline() {
    let runtime = create_runtime_with_pasta_path();

    let result = runtime.exec(
        r#"
        local REG = require "pasta.shiori.event.register"
        local SHIORI = require "pasta.shiori.entry"
        
        REG.OnMultilineError = function(act)
            error("First line\nSecond line\nThird line")
        end
        
        local req = { id = "OnMultilineError", method = "get", version = 30 }
        local response = SHIORI.request(req)
        
        -- X-Error-Reason should contain only the first line
        local has_500 = response:find("500 Internal Server Error") ~= nil
        local has_first_line = response:find("X%-Error%-Reason:") ~= nil
        local no_newline_in_reason = response:match("X%-Error%-Reason:[^\r\n]+Second") == nil
        
        return has_500 and has_first_line and no_newline_in_reason
    "#,
    );

    assert!(
        result.is_ok(),
        "Error message should not contain newlines: {:?}",
        result
    );
    assert!(result.unwrap().as_boolean().unwrap_or(false));
}

#[test]
fn test_event_fire_handles_empty_error_message() {
    let runtime = create_runtime_with_pasta_path();

    // Note: In mlua, error("") still includes file location information,
    // so it won't be truly empty. This test verifies that the first line
    // is extracted correctly even when the user provides an empty message.
    let result = runtime.exec(
        r#"
        local REG = require "pasta.shiori.event.register"
        local SHIORI = require "pasta.shiori.entry"
        
        REG.OnEmptyError = function(act)
            error("")
        end
        
        local req = { id = "OnEmptyError", method = "get", version = 30 }
        local response = SHIORI.request(req)
        
        -- Should return 500 with file location info (mlua adds it automatically)
        local has_500 = response:find("500 Internal Server Error") ~= nil
        local has_error_reason = response:find("X%-Error%-Reason:") ~= nil
        
        return has_500 and has_error_reason
    "#,
    );

    assert!(
        result.is_ok(),
        "Should handle empty error message gracefully: {:?}",
        result
    );
    assert!(result.unwrap().as_boolean().unwrap_or(false));
}

// ============================================================================
// Task 4.1, 4.2: Integration Tests
// ============================================================================

/// Task 4.1: Tests integration with RES module (RES.ok, RES.no_content, RES.err)
#[test]
fn test_event_module_with_res_module() {
    let runtime = create_runtime_with_pasta_path();

    let result = runtime.exec(
        r#"
        local REG = require "pasta.shiori.event.register"
        local EVENT = require "pasta.shiori.event"
        local SHIORI = require "pasta.shiori.entry"
        local RES = require "pasta.shiori.res"
        
        -- Test 1: RES.ok integration via handler
        REG.TestOk = function(act)
            return RES.ok("Hello World")
        end
        local res1 = EVENT.fire({ id = "TestOk", method = "get", version = 30 })
        local ok_works = res1:find("200 OK") ~= nil and res1:find("Value: Hello World") ~= nil
        
        -- Test 2: RES.no_content integration via EVENT.no_entry
        local res2 = EVENT.fire({ id = "Unregistered", method = "get", version = 30 })
        local no_content_works = res2:find("204 No Content") ~= nil
        
        -- Test 3: RES.err integration via error handling (use SHIORI.request for xpcall)
        REG.TestErr = function(act)
            error("Intentional error")
        end
        local res3 = SHIORI.request({ id = "TestErr", method = "get", version = 30 })
        local err_works = res3:find("500 Internal Server Error") ~= nil and res3:find("X%-Error%-Reason:") ~= nil
        
        return ok_works and no_content_works and err_works
    "#,
    );

    assert!(
        result.is_ok(),
        "EVENT module should integrate correctly with RES module: {:?}",
        result
    );
    assert!(result.unwrap().as_boolean().unwrap_or(false));
}

/// Task 4.2: Tests complete handler registration and dispatch flow
#[test]
fn test_handler_registration_and_dispatch() {
    let runtime = create_runtime_with_pasta_path();

    let result = runtime.exec(
        r#"
        local REG = require "pasta.shiori.event.register"
        local EVENT = require "pasta.shiori.event"
        local RES = require "pasta.shiori.res"
        
        -- Register multiple handlers
        REG.OnBoot = function(act)
            return RES.ok("Booting up!")
        end
        
        REG.OnClose = function(act)
            return RES.ok("Shutting down!")
        end
        
        REG.OnGhostChanged = function(act)
            return RES.ok("Ghost changed!")
        end
        
        -- Dispatch to each handler and verify correct handler is called
        local boot_res = EVENT.fire({ id = "OnBoot", method = "get", version = 30 })
        local close_res = EVENT.fire({ id = "OnClose", method = "get", version = 30 })
        local ghost_res = EVENT.fire({ id = "OnGhostChanged", method = "get", version = 30 })
        local unknown_res = EVENT.fire({ id = "OnUnknown", method = "get", version = 30 })
        
        local boot_correct = boot_res:find("Booting up!") ~= nil
        local close_correct = close_res:find("Shutting down!") ~= nil
        local ghost_correct = ghost_res:find("Ghost changed!") ~= nil
        local unknown_correct = unknown_res:find("204 No Content") ~= nil
        
        return boot_correct and close_correct and ghost_correct and unknown_correct
    "#,
    );

    assert!(
        result.is_ok(),
        "Multiple handlers should be dispatched correctly: {:?}",
        result
    );
    assert!(result.unwrap().as_boolean().unwrap_or(false));
}

// ============================================================================
// Default Event Handlers: boot.lua
// ============================================================================

/// Tests that default OnBoot handler is registered via boot.lua
#[test]
fn test_default_onboot_handler_registered() {
    let runtime = create_runtime_with_pasta_path();

    let result = runtime.exec(
        r#"
        local EVENT = require "pasta.shiori.event"
        local REG = require "pasta.shiori.event.register"
        
        -- OnBoot should be registered after loading EVENT module
        return type(REG.OnBoot) == "function"
    "#,
    );

    assert!(
        result.is_ok(),
        "Default OnBoot handler should be registered: {:?}",
        result
    );
    assert!(result.unwrap().as_boolean().unwrap_or(false));
}

/// Tests that default OnBoot returns 204 No Content
#[test]
fn test_default_onboot_returns_204() {
    let runtime = create_runtime_with_pasta_path();

    let result = runtime.exec(
        r#"
        local EVENT = require "pasta.shiori.event"
        
        local req = { id = "OnBoot", method = "get", version = 30 }
        local response = EVENT.fire(req)
        
        return response:find("204 No Content") ~= nil
    "#,
    );

    assert!(
        result.is_ok(),
        "Default OnBoot should return 204: {:?}",
        result
    );
    assert!(result.unwrap().as_boolean().unwrap_or(false));
}

/// Tests that custom OnBoot overrides default
#[test]
fn test_custom_onboot_overrides_default() {
    let runtime = create_runtime_with_pasta_path();

    let result = runtime.exec(
        r#"
        local EVENT = require "pasta.shiori.event"
        local REG = require "pasta.shiori.event.register"
        local RES = require "pasta.shiori.res"
        
        -- Override default OnBoot
        REG.OnBoot = function(act)
            return RES.ok("Custom Boot!")
        end
        
        local req = { id = "OnBoot", method = "get", version = 30 }
        local response = EVENT.fire(req)
        
        return response:find("200 OK") ~= nil and response:find("Custom Boot!") ~= nil
    "#,
    );

    assert!(
        result.is_ok(),
        "Custom OnBoot should override default: {:?}",
        result
    );
    assert!(result.unwrap().as_boolean().unwrap_or(false));
}

// ============================================================================
// Task 2.4: Unregistered Event Fallback Tests (Additional)
// ============================================================================

/// Tests that nil Reference access returns nil
#[test]
fn test_nil_reference_access() {
    let runtime = create_runtime_with_pasta_path();

    let result = runtime.exec(
        r#"
        local EVENT = require "pasta.shiori.event"
        local REG = require "pasta.shiori.event.register"
        local RES = require "pasta.shiori.res"
        
        local ref5, ref7 = "unset", "unset"
        REG.OnTestNil = function(act)
            ref5 = act.req.reference[5]
            ref7 = act.req.reference[7]
            return RES.ok("OK")
        end
        
        local req = {
            id = "OnTestNil",
            method = "get",
            version = 30,
            reference = { [0] = "exists" }  -- Only ref0 exists
        }
        local response = EVENT.fire(req)
        
        return ref5 == nil and ref7 == nil
    "#,
    );

    assert!(
        result.is_ok(),
        "Nil Reference access should return nil: {:?}",
        result
    );
    assert!(result.unwrap().as_boolean().unwrap_or(false));
}

// ============================================================================
// Default Event Handlers: choice_select.lua (Task 4.3)
// ============================================================================

/// Tests that default OnChoiceSelectEx handler is registered via choice_select.lua
#[test]
fn test_default_onchoiceselectex_handler_registered() {
    let runtime = create_runtime_with_pasta_path();

    let result = runtime.exec(
        r#"
        local EVENT = require "pasta.shiori.event"
        local REG = require "pasta.shiori.event.register"
        
        -- OnChoiceSelectEx should be registered after loading EVENT module
        return type(REG.OnChoiceSelectEx) == "function"
    "#,
    );

    assert!(
        result.is_ok(),
        "Default OnChoiceSelectEx handler should be registered: {:?}",
        result
    );
    assert!(result.unwrap().as_boolean().unwrap_or(false));
}

/// Tests that default OnChoiceSelectEx returns 204 when no matching scene exists
#[test]
fn test_default_onchoiceselectex_returns_204_no_match() {
    let runtime = create_runtime_with_pasta_path();

    let result = runtime.exec(
        r#"
        local EVENT = require "pasta.shiori.event"
        
        local req = {
            id = "OnChoiceSelectEx",
            method = "get",
            version = 30,
            reference = { [0] = "nonexistent_choice_id" }
        }
        local response = EVENT.fire(req)
        
        return response:find("204 No Content") ~= nil
    "#,
    );

    assert!(
        result.is_ok(),
        "Default OnChoiceSelectEx should return 204 when no scene matches: {:?}",
        result
    );
    assert!(result.unwrap().as_boolean().unwrap_or(false));
}

/// Tests that OnChoiceSelectEx returns 204 when reference is missing
#[test]
fn test_default_onchoiceselectex_returns_204_no_reference() {
    let runtime = create_runtime_with_pasta_path();

    let result = runtime.exec(
        r#"
        local EVENT = require "pasta.shiori.event"
        
        local req = {
            id = "OnChoiceSelectEx",
            method = "get",
            version = 30,
        }
        local response = EVENT.fire(req)
        
        return response:find("204 No Content") ~= nil
    "#,
    );

    assert!(
        result.is_ok(),
        "Default OnChoiceSelectEx should return 204 when no reference: {:?}",
        result
    );
    assert!(result.unwrap().as_boolean().unwrap_or(false));
}