fraiseql-functions 2.6.0

Serverless functions runtime for FraiseQL — WASM and Deno backends
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
//! Tests for `DenoRuntime` — `JavaScript`/`TypeScript` function execution via V8

#![cfg(feature = "runtime-deno")]
#![allow(clippy::unwrap_used, clippy::panic)] // Reason: test code, panics acceptable
#![allow(unused_imports)] // Reason: used in test functions that may not be compiled in some configurations

use chrono::Utc;

use crate::{EventPayload, FunctionModule, FunctionRuntime, ResourceLimits, RuntimeType};

/// Helper to create a test event payload
#[allow(dead_code)] // Reason: used in failing tests, will be used when GREEN phase implemented
fn test_event() -> EventPayload {
    EventPayload {
        trigger_type: "test".to_string(),
        entity:       "Test".to_string(),
        event_kind:   "created".to_string(),
        data:         serde_json::json!({"value": 42}),
        timestamp:    Utc::now(),
    }
}

#[tokio::test]
async fn test_deno_execute_identity_js() {
    // JS module: `export default async (event) => event;`
    // Input: {"value": 42}
    // Expected: same JSON returned
    let source = r"
export default async (event) => {
    return event;
};
"
    .to_string();

    let module =
        FunctionModule::from_source("test_identity".to_string(), source, RuntimeType::Deno);
    let runtime = super::DenoRuntime::new(&super::DenoConfig::default())
        .expect("Failed to create DenoRuntime");

    let event = test_event();
    let event_data = event.data.clone();
    let result = runtime
        .invoke(
            &module,
            event.clone(),
            &crate::host::NoopHostContext::new(event),
            ResourceLimits::default(),
        )
        .await;

    // Should succeed and return the event as-is
    assert!(result.is_ok(), "Identity function should execute successfully");
    let result = result.unwrap();
    assert_eq!(result.value, Some(event_data));
}

#[tokio::test]
async fn test_deno_execute_transform_js() {
    // JS module that adds a field — event is the entity data directly (event.data from the trigger)
    let source = r"
export default async (event) => {
    return { ...event, processed: true };
};
"
    .to_string();

    let module =
        FunctionModule::from_source("test_transform".to_string(), source, RuntimeType::Deno);
    let runtime = super::DenoRuntime::new(&super::DenoConfig::default())
        .expect("Failed to create DenoRuntime");

    let event = test_event();
    let result = runtime
        .invoke(
            &module,
            event.clone(),
            &crate::host::NoopHostContext::new(event),
            ResourceLimits::default(),
        )
        .await;

    assert!(result.is_ok(), "Transform function should execute successfully");
    let result = result.unwrap();
    // Result should have the original data plus the new field
    if let Some(serde_json::Value::Object(obj)) = result.value {
        assert!(obj.contains_key("processed"), "Result should have 'processed' field");
        assert_eq!(obj["processed"], true);
        assert_eq!(obj["value"], 42);
    } else {
        panic!("Expected object result");
    }
}

#[tokio::test]
async fn test_deno_execute_typescript() {
    // TypeScript-style module: the host strips TS syntax before execution.
    // Event is the entity data directly (no nested .data field).
    let source = r"
export default async (event) => {
    return { result: event.value + 1 };
};
"
    .to_string();

    let module =
        FunctionModule::from_source("test_typescript".to_string(), source, RuntimeType::Deno);
    let runtime = super::DenoRuntime::new(&super::DenoConfig::default())
        .expect("Failed to create DenoRuntime");

    let event = test_event();
    let result = runtime
        .invoke(
            &module,
            event.clone(),
            &crate::host::NoopHostContext::new(event),
            ResourceLimits::default(),
        )
        .await;

    assert!(result.is_ok(), "TypeScript function should execute successfully");
    let result = result.unwrap();
    // Result should be { result: 43 }
    if let Some(serde_json::Value::Object(obj)) = result.value {
        assert_eq!(obj["result"], 43);
    } else {
        panic!("Expected object result");
    }
}

#[tokio::test]
async fn test_deno_syntax_error_returns_validation() {
    // Invalid JavaScript
    let source = "export default async (event) => { broken syntax here }".to_string();

    let module =
        FunctionModule::from_source("test_syntax_error".to_string(), source, RuntimeType::Deno);
    let runtime = super::DenoRuntime::new(&super::DenoConfig::default())
        .expect("Failed to create DenoRuntime");

    let event = test_event();
    let result = runtime
        .invoke(
            &module,
            event.clone(),
            &crate::host::NoopHostContext::new(event),
            ResourceLimits::default(),
        )
        .await;

    // Should return an error (either Validation for syntax error or Unsupported for stub)
    assert!(result.is_err(), "Syntax error should result in error");
    // When fully implemented, should be Validation error for SyntaxError
    // For now, stub returns Unsupported
}

#[tokio::test]
async fn test_deno_runtime_error_returns_internal() {
    // JavaScript that throws at runtime
    let source = r#"
export default async (event) => {
    throw new Error("Something went wrong");
};
"#
    .to_string();

    let module =
        FunctionModule::from_source("test_runtime_error".to_string(), source, RuntimeType::Deno);
    let runtime = super::DenoRuntime::new(&super::DenoConfig::default())
        .expect("Failed to create DenoRuntime");

    let event = test_event();
    let result = runtime
        .invoke(
            &module,
            event.clone(),
            &crate::host::NoopHostContext::new(event),
            ResourceLimits::default(),
        )
        .await;

    // Should return an internal error
    assert!(result.is_err(), "Runtime error should result in error");
}

#[tokio::test]
async fn test_deno_name_returns_deno() {
    let runtime = super::DenoRuntime::new(&super::DenoConfig::default())
        .expect("Failed to create DenoRuntime");

    assert_eq!(runtime.name(), "deno");
}

#[tokio::test]
async fn test_deno_supported_extensions() {
    let runtime = super::DenoRuntime::new(&super::DenoConfig::default())
        .expect("Failed to create DenoRuntime");

    let exts = runtime.supported_extensions();
    assert!(exts.contains(&".js"));
    assert!(exts.contains(&".ts"));
    assert!(exts.contains(&".mjs"));
    assert!(exts.contains(&".mts"));
}

// Cycle 2: V8 Resource Limits (Memory & CPU Timeouts)

#[tokio::test]
async fn test_deno_memory_limit_terminates() {
    // JS that allocates memory until limit is exceeded
    let source = r"
export default async (event) => {
    const arr = [];
    while (true) {
        arr.push(new ArrayBuffer(1024 * 1024)); // 1MB allocations
    }
};
"
    .to_string();

    let module =
        FunctionModule::from_source("test_memory_limit".to_string(), source, RuntimeType::Deno);
    let runtime = super::DenoRuntime::new(&super::DenoConfig::default())
        .expect("Failed to create DenoRuntime");

    let event = test_event();
    let limits = ResourceLimits {
        max_memory_bytes: 64 * 1024 * 1024, // 64MB hard limit
        max_duration:     std::time::Duration::from_secs(5),
        max_log_entries:  10_000,
    };

    let result = runtime
        .invoke(&module, event.clone(), &crate::host::NoopHostContext::new(event), limits)
        .await;

    // Should return an error when memory limit is exceeded
    assert!(result.is_err(), "Memory limit exceeded should result in error");
}

#[tokio::test]
async fn test_deno_timeout_aborts() {
    // JS with infinite loop
    let source = r"
export default async (event) => {
    while (true) {}
};
"
    .to_string();

    let module = FunctionModule::from_source("test_timeout".to_string(), source, RuntimeType::Deno);
    let runtime = super::DenoRuntime::new(&super::DenoConfig::default())
        .expect("Failed to create DenoRuntime");

    let event = test_event();
    let limits = ResourceLimits {
        max_memory_bytes: 128 * 1024 * 1024,
        max_duration:     std::time::Duration::from_millis(100), // 100ms timeout
        max_log_entries:  10_000,
    };

    let result = runtime
        .invoke(&module, event.clone(), &crate::host::NoopHostContext::new(event), limits)
        .await;

    // Should return a timeout error
    assert!(result.is_err(), "Infinite loop should timeout and return error");
    // When fully implemented, should be Timeout error
    // For now, stub will return some error
}

#[tokio::test]
async fn test_deno_within_limits_succeeds() {
    // Lightweight function that completes quickly
    let source = r"
export default async (event) => {
    return { result: 'success' };
};
"
    .to_string();

    let module =
        FunctionModule::from_source("test_within_limits".to_string(), source, RuntimeType::Deno);
    let runtime = super::DenoRuntime::new(&super::DenoConfig::default())
        .expect("Failed to create DenoRuntime");

    let event = test_event();
    let limits = ResourceLimits {
        max_memory_bytes: 256 * 1024 * 1024,                 // 256MB
        max_duration:     std::time::Duration::from_secs(5), // 5 second timeout
        max_log_entries:  10_000,
    };

    let result = runtime
        .invoke(&module, event.clone(), &crate::host::NoopHostContext::new(event), limits)
        .await;

    // Should succeed
    assert!(result.is_ok(), "Function within limits should succeed");
}

#[tokio::test]
async fn test_deno_memory_peak_reported() {
    // Function that allocates some memory but stays within limit
    let source = r"
export default async (event) => {
    const arr = [];
    for (let i = 0; i < 100; i++) {
        arr.push(new ArrayBuffer(1024)); // 100KB total
    }
    return { allocated: true };
};
"
    .to_string();

    let module =
        FunctionModule::from_source("test_memory_peak".to_string(), source, RuntimeType::Deno);
    let runtime = super::DenoRuntime::new(&super::DenoConfig::default())
        .expect("Failed to create DenoRuntime");

    let event = test_event();
    let result = runtime
        .invoke(
            &module,
            event.clone(),
            &crate::host::NoopHostContext::new(event),
            ResourceLimits::default(),
        )
        .await;

    // Should succeed
    assert!(result.is_ok(), "Memory allocation within bounds should succeed");
    let result = result.unwrap();
    // Memory peak should be reported (non-zero if successfully measured)
    // Current stub returns 0, but when fully implemented should be > 0
    let _memory_peak = result.memory_peak_bytes;
}

#[tokio::test]
async fn test_deno_async_timeout() {
    // Async function that never resolves (hangs forever)
    let source = r"
export default async (event) => {
    return new Promise(() => {
        // Never resolves — intentional hang
    });
};
"
    .to_string();

    let module =
        FunctionModule::from_source("test_async_timeout".to_string(), source, RuntimeType::Deno);
    let runtime = super::DenoRuntime::new(&super::DenoConfig::default())
        .expect("Failed to create DenoRuntime");

    let event = test_event();
    let limits = ResourceLimits {
        max_memory_bytes: 128 * 1024 * 1024,
        max_duration:     std::time::Duration::from_millis(200), // 200ms timeout
        max_log_entries:  10_000,
    };

    let result = runtime
        .invoke(&module, event.clone(), &crate::host::NoopHostContext::new(event), limits)
        .await;

    // Should return a timeout error
    assert!(result.is_err(), "Unresolved promise with timeout should error");
    // When fully implemented, should be Timeout error
}

// Cycle 3: Deno Host Ops (Structured Logging)

#[tokio::test]
async fn test_deno_guest_can_call_log() {
    // JavaScript that calls the fraiseql_log host op
    let source = r"
export default async (event) => {
    Deno.core.ops.fraiseql_log(1, 'hello from deno');
    return { logged: true };
};
"
    .to_string();

    let module = FunctionModule::from_source("test_log".to_string(), source, RuntimeType::Deno);
    let runtime = super::DenoRuntime::new(&super::DenoConfig::default())
        .expect("Failed to create DenoRuntime");

    let event = test_event();
    let result = runtime
        .invoke(
            &module,
            event.clone(),
            &crate::host::NoopHostContext::new(event),
            ResourceLimits::default(),
        )
        .await;

    // Should succeed and have a log entry
    assert!(result.is_ok(), "Log call should execute successfully");
    let result = result.unwrap();
    assert!(!result.logs.is_empty(), "Result should have at least one log entry");

    // Check the log entry
    let log = &result.logs[0];
    assert_eq!(log.level, crate::LogLevel::Info);
    assert_eq!(log.message, "hello from deno");
}

#[tokio::test]
async fn test_deno_guest_log_levels() {
    // JavaScript that logs at different levels
    let source = r"
export default async (event) => {
    Deno.core.ops.fraiseql_log(0, 'debug message');
    Deno.core.ops.fraiseql_log(1, 'info message');
    Deno.core.ops.fraiseql_log(2, 'warn message');
    Deno.core.ops.fraiseql_log(3, 'error message');
    return { logged: true };
};
"
    .to_string();

    let module =
        FunctionModule::from_source("test_log_levels".to_string(), source, RuntimeType::Deno);
    let runtime = super::DenoRuntime::new(&super::DenoConfig::default())
        .expect("Failed to create DenoRuntime");

    let event = test_event();
    let result = runtime
        .invoke(
            &module,
            event.clone(),
            &crate::host::NoopHostContext::new(event),
            ResourceLimits::default(),
        )
        .await;

    assert!(result.is_ok(), "Log calls should execute successfully");
    let result = result.unwrap();
    assert_eq!(result.logs.len(), 4, "Should have 4 log entries");

    // Verify each log level
    assert_eq!(result.logs[0].level, crate::LogLevel::Debug);
    assert_eq!(result.logs[0].message, "debug message");

    assert_eq!(result.logs[1].level, crate::LogLevel::Info);
    assert_eq!(result.logs[1].message, "info message");

    assert_eq!(result.logs[2].level, crate::LogLevel::Warn);
    assert_eq!(result.logs[2].message, "warn message");

    assert_eq!(result.logs[3].level, crate::LogLevel::Error);
    assert_eq!(result.logs[3].message, "error message");
}

#[tokio::test]
async fn test_deno_guest_log_with_timestamp() {
    // JavaScript that logs a message
    // Verify that each log entry has a proper timestamp
    let source = r"
export default async (event) => {
    Deno.core.ops.fraiseql_log(1, 'test message with timestamp');
    return { done: true };
};
"
    .to_string();

    let module =
        FunctionModule::from_source("test_log_timestamp".to_string(), source, RuntimeType::Deno);
    let runtime = super::DenoRuntime::new(&super::DenoConfig::default())
        .expect("Failed to create DenoRuntime");

    let event = test_event();
    let before = chrono::Utc::now();
    let result = runtime
        .invoke(
            &module,
            event.clone(),
            &crate::host::NoopHostContext::new(event),
            ResourceLimits::default(),
        )
        .await;
    let after = chrono::Utc::now();

    // Should succeed and have captured log with proper timestamp
    assert!(result.is_ok(), "Log call should execute successfully");
    let result = result.unwrap();
    assert_eq!(result.logs.len(), 1);
    assert_eq!(result.logs[0].message, "test message with timestamp");

    // Verify timestamp is within the invocation period
    assert!(result.logs[0].timestamp >= before);
    assert!(result.logs[0].timestamp <= after);
}

#[tokio::test]
async fn test_deno_guest_log_limit_enforced() {
    // Generate JavaScript with many individual log calls (can't use a loop in stub)
    use std::fmt::Write;
    let mut source = String::from("export default async (event) => {\n");
    for i in 0..1500 {
        writeln!(source, "    Deno.core.ops.fraiseql_log(1, 'log entry {}');", i)
            .expect("write to string");
    }
    source.push_str("    return { logged: true };\n};\n");

    let module =
        FunctionModule::from_source("test_log_limit".to_string(), source, RuntimeType::Deno);
    let runtime = super::DenoRuntime::new(&super::DenoConfig::default())
        .expect("Failed to create DenoRuntime");

    let event = test_event();
    let limits = ResourceLimits {
        max_memory_bytes: 128 * 1024 * 1024,
        max_duration:     std::time::Duration::from_secs(5),
        max_log_entries:  1000, // Only allow 1000 logs
    };

    let result = runtime
        .invoke(&module, event.clone(), &crate::host::NoopHostContext::new(event), limits)
        .await;

    // Should succeed but with limited logs
    assert!(result.is_ok(), "Log calls should execute successfully");
    let result = result.unwrap();
    assert_eq!(result.logs.len(), 1000, "Should cap logs at max_log_entries");

    // Verify the last captured log
    assert_eq!(result.logs[999].message, "log entry 999");
}

// ========== Phase 5B Cycle 2: Deno Host Op Bridge Tests (RED Phase) ==========

#[cfg(feature = "host-live")]
#[tokio::test]
async fn test_deno_guest_calls_query_op() {
    // RED: JS calls Deno.core.ops.fraiseql_query()
    // Should receive GraphQL result
    let source = r"
export default async (event) => {
    // In real implementation, this would call:
    // const result = await Deno.core.ops.fraiseql_query('{ users { id } }', '{}');
    // For now, just verify the op infrastructure is in place
    return { query_test: true };
};
"
    .to_string();

    let module =
        FunctionModule::from_source("test_query_op".to_string(), source, RuntimeType::Deno);
    let runtime = super::DenoRuntime::new(&super::DenoConfig::default())
        .expect("Failed to create DenoRuntime");

    let event = test_event();
    let result = runtime
        .invoke(
            &module,
            event.clone(),
            &crate::host::NoopHostContext::new(event),
            ResourceLimits::default(),
        )
        .await;

    // Should execute successfully
    assert!(result.is_ok(), "Query op call should execute");
    let result = result.unwrap();
    assert!(result.value.is_some(), "Query op should return a value");
}

#[cfg(feature = "host-live")]
#[tokio::test]
async fn test_deno_guest_calls_http_request_op() {
    // RED: JS calls Deno.core.ops.fraiseql_http_request()
    // Should receive HTTP response
    let source = r"
export default async (event) => {
    // In real implementation:
    // const response = await Deno.core.ops.fraiseql_http_request('GET', 'https://example.com', [], null);
    return { http_test: true };
};
"
    .to_string();

    let module = FunctionModule::from_source("test_http_op".to_string(), source, RuntimeType::Deno);
    let runtime = super::DenoRuntime::new(&super::DenoConfig::default())
        .expect("Failed to create DenoRuntime");

    let event = test_event();
    let result = runtime
        .invoke(
            &module,
            event.clone(),
            &crate::host::NoopHostContext::new(event),
            ResourceLimits::default(),
        )
        .await;

    assert!(result.is_ok(), "HTTP request op should execute");
    let result = result.unwrap();
    assert!(result.value.is_some(), "HTTP op should return a value");
}

#[cfg(feature = "host-live")]
#[tokio::test]
async fn test_deno_guest_calls_storage_get_op() {
    // RED: JS calls Deno.core.ops.fraiseql_storage_get()
    // Should receive bytes from storage
    let source = r"
export default async (event) => {
    // In real implementation:
    // const data = await Deno.core.ops.fraiseql_storage_get('bucket', 'key');
    return { storage_get_test: true };
};
"
    .to_string();

    let module =
        FunctionModule::from_source("test_storage_get_op".to_string(), source, RuntimeType::Deno);
    let runtime = super::DenoRuntime::new(&super::DenoConfig::default())
        .expect("Failed to create DenoRuntime");

    let event = test_event();
    let result = runtime
        .invoke(
            &module,
            event.clone(),
            &crate::host::NoopHostContext::new(event),
            ResourceLimits::default(),
        )
        .await;

    assert!(result.is_ok(), "Storage get op should execute");
    let result = result.unwrap();
    assert!(result.value.is_some(), "Storage get op should return a value");
}

#[cfg(feature = "host-live")]
#[tokio::test]
async fn test_deno_guest_calls_storage_put_op() {
    // RED: JS calls Deno.core.ops.fraiseql_storage_put()
    let source = r"
export default async (event) => {
    // In real implementation:
    // await Deno.core.ops.fraiseql_storage_put('bucket', 'key', data, 'text/plain');
    return { storage_put_test: true };
};
"
    .to_string();

    let module =
        FunctionModule::from_source("test_storage_put_op".to_string(), source, RuntimeType::Deno);
    let runtime = super::DenoRuntime::new(&super::DenoConfig::default())
        .expect("Failed to create DenoRuntime");

    let event = test_event();
    let result = runtime
        .invoke(
            &module,
            event.clone(),
            &crate::host::NoopHostContext::new(event),
            ResourceLimits::default(),
        )
        .await;

    assert!(result.is_ok(), "Storage put op should execute");
    let result = result.unwrap();
    assert!(result.value.is_some(), "Storage put op should return a value");
}

#[cfg(feature = "host-live")]
#[tokio::test]
async fn test_deno_guest_calls_auth_context_op() {
    // RED: JS calls Deno.core.ops.fraiseql_auth_context()
    // Should receive auth context JSON
    let source = r"
export default async (event) => {
    // In real implementation:
    // const auth = Deno.core.ops.fraiseql_auth_context();
    return { auth_test: true };
};
"
    .to_string();

    let module = FunctionModule::from_source("test_auth_op".to_string(), source, RuntimeType::Deno);
    let runtime = super::DenoRuntime::new(&super::DenoConfig::default())
        .expect("Failed to create DenoRuntime");

    let event = test_event();
    let result = runtime
        .invoke(
            &module,
            event.clone(),
            &crate::host::NoopHostContext::new(event),
            ResourceLimits::default(),
        )
        .await;

    assert!(result.is_ok(), "Auth context op should execute");
    let result = result.unwrap();
    assert!(result.value.is_some(), "Auth context op should return a value");
}

#[cfg(feature = "host-live")]
#[tokio::test]
async fn test_deno_guest_calls_env_var_op() {
    // RED: JS calls Deno.core.ops.fraiseql_env_var()
    // Should receive environment variable value or null
    let source = r"
export default async (event) => {
    // In real implementation:
    // const value = Deno.core.ops.fraiseql_env_var('TEST_VAR');
    return { env_test: true };
};
"
    .to_string();

    let module =
        FunctionModule::from_source("test_env_var_op".to_string(), source, RuntimeType::Deno);
    let runtime = super::DenoRuntime::new(&super::DenoConfig::default())
        .expect("Failed to create DenoRuntime");

    let event = test_event();
    let result = runtime
        .invoke(
            &module,
            event.clone(),
            &crate::host::NoopHostContext::new(event),
            ResourceLimits::default(),
        )
        .await;

    assert!(result.is_ok(), "Env var op should execute");
    let result = result.unwrap();
    assert!(result.value.is_some(), "Env var op should return a value");
}