harn-serve 0.10.140

Shared outbound workflow server core for Harn adapters
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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
use super::*;

#[test]
fn export_catalog_only_includes_public_functions() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("server.harn");
    std::fs::write(
        &path,
        r#"
fn hidden() { return "nope" }
pub fn greet(name: string, excited: bool = false) -> string {
  if excited { return "hi!" }
  return name
}
"#,
    )
    .expect("write script");

    let catalog = ExportCatalog::from_path(&path).expect("catalog");
    assert!(catalog.function("hidden").is_none());
    let greet = catalog.function("greet").expect("greet export");
    assert_eq!(greet.params.len(), 2);
    assert_eq!(greet.input_schema["type"], "object");
    assert_eq!(
        greet.output_schema.as_ref().expect("output")["type"],
        "string"
    );
}

#[test]
fn export_catalog_captures_scopes_attribute_from_function_decl() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("server.harn");
    std::fs::write(
        &path,
        r#"
@scopes("personas:read", "sessions:write")
pub fn list_sessions() -> string {
  return "ok"
}

pub fn ping() -> string {
  return "pong"
}
"#,
    )
    .expect("write script");

    let catalog = ExportCatalog::from_path(&path).expect("catalog");
    let list = catalog.function("list_sessions").expect("list_sessions");
    assert_eq!(
        list.required_scopes,
        BTreeSet::from(["personas:read".to_string(), "sessions:write".to_string()])
    );
    let ping = catalog.function("ping").expect("ping");
    assert!(ping.required_scopes.is_empty());
}

#[test]
fn export_catalog_splits_method_prefixed_scopes_from_the_baseline() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("server.harn");
    std::fs::write(
        &path,
        r#"
@scopes("base:read", "GET extra:get", "put extra:put")
@route("*", "/r")
pub fn r() -> string { return "ok" }
"#,
    )
    .expect("write script");

    let catalog = ExportCatalog::from_path(&path).expect("catalog");
    let r = catalog.function("r").expect("r export");
    // An un-prefixed literal stays in the method-agnostic baseline.
    assert_eq!(r.required_scopes, BTreeSet::from(["base:read".to_string()]));
    // A method prefix (case-insensitive) routes the scope into the
    // per-method bucket under the uppercased method key.
    assert_eq!(
        r.method_scopes.get("GET"),
        Some(&BTreeSet::from(["extra:get".to_string()]))
    );
    assert_eq!(
        r.method_scopes.get("PUT"),
        Some(&BTreeSet::from(["extra:put".to_string()]))
    );
}

#[test]
fn export_catalog_keeps_colon_scopes_uniform_without_a_method_prefix() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("server.harn");
    // A leading word that is *not* an HTTP method (here a normal
    // `scope:verb` token) is never misread as a per-method prefix —
    // the historic uniform form is untouched.
    std::fs::write(
        &path,
        r#"
@scopes("personas:read")
pub fn r() -> string { return "ok" }
"#,
    )
    .expect("write script");

    let catalog = ExportCatalog::from_path(&path).expect("catalog");
    let r = catalog.function("r").expect("r export");
    assert_eq!(
        r.required_scopes,
        BTreeSet::from(["personas:read".to_string()])
    );
    assert!(r.method_scopes.is_empty());
}

#[test]
fn export_catalog_parses_limits_and_budget_attributes() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("server.harn");
    std::fs::write(
        &path,
        r#"
@limits(
    per_tenant: "100/min",
    per_route: "5000/min",
    burst: 50,
    algorithm: "sliding_window",
    in_flight_max: 20,
)
@budget(llm_cost_usd: 0.50, mcp_calls: 20)
pub fn create() -> string { return "ok" }

pub fn ping() -> string { return "pong" }
"#,
    )
    .expect("write script");

    let catalog = ExportCatalog::from_path(&path).expect("catalog");
    let create = catalog.function("create").expect("create export");
    let limits = create.limits.as_ref().expect("limits parsed");
    assert_eq!(limits.per_tenant.unwrap().count, 100);
    assert_eq!(limits.per_route.unwrap().count, 5_000);
    assert_eq!(limits.burst, Some(50));
    assert_eq!(limits.algorithm, crate::limits::Algorithm::SlidingWindow);
    assert_eq!(limits.in_flight_max, Some(20));
    let budget = create.budget.as_ref().expect("budget parsed");
    assert_eq!(budget.llm_cost_usd, Some(0.50));
    assert_eq!(budget.mcp_calls, Some(20));

    // Routes without the attributes get None — the dispatch path
    // short-circuits without consulting the registry.
    let ping = catalog.function("ping").expect("ping export");
    assert!(ping.limits.is_none());
    assert!(ping.budget.is_none());
}

#[test]
fn route_attribute_parses_method_and_path() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("server.harn");
    std::fs::write(
        &path,
        r#"
@route("POST", "/users/{id}")
pub fn update_user(req: dict) -> dict { return req }

@route("/health")
pub fn liveness(req: dict) -> dict { return req }

@route("any", "metrics")
pub fn metrics(req: dict) -> dict { return req }

pub fn helper(req: dict) -> dict { return req }
"#,
    )
    .expect("write script");

    let catalog = ExportCatalog::from_path(&path).expect("catalog");
    let update = catalog.function("update_user").expect("update_user");
    assert_eq!(
        update.route,
        Some(RouteSpec {
            method: "POST".to_string(),
            path: "/users/{id}".to_string()
        })
    );
    // Single-arg form defaults to GET.
    let liveness = catalog.function("liveness").expect("liveness");
    assert_eq!(
        liveness.route,
        Some(RouteSpec {
            method: "GET".to_string(),
            path: "/health".to_string()
        })
    );
    // `any` lowercases to the `*` wildcard; a path missing its leading
    // slash is normalized.
    let metrics = catalog.function("metrics").expect("metrics");
    assert_eq!(
        metrics.route,
        Some(RouteSpec {
            method: "*".to_string(),
            path: "/metrics".to_string()
        })
    );
    // A plain `pub fn` with no attribute and no `handler_` prefix is
    // dispatch-only — it gets no HTTP route.
    let helper = catalog.function("helper").expect("helper");
    assert_eq!(helper.route, None);
}

#[test]
fn handler_naming_convention_infers_route() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("server.harn");
    std::fs::write(
        &path,
        r"
pub fn handler(req: dict) -> dict { return req }
pub fn handler_echo(req: dict) -> dict { return req }
",
    )
    .expect("write script");

    let catalog = ExportCatalog::from_path(&path).expect("catalog");
    // Bare `handler` mounts at the site root.
    assert_eq!(
        catalog.function("handler").expect("handler").route,
        Some(RouteSpec {
            method: "*".to_string(),
            path: "/".to_string()
        })
    );
    // `handler_echo` mounts at `/echo`, answering every method.
    assert_eq!(
        catalog
            .function("handler_echo")
            .expect("handler_echo")
            .route,
        Some(RouteSpec {
            method: "*".to_string(),
            path: "/echo".to_string()
        })
    );
}

#[test]
fn export_catalog_falls_back_to_legacy_pipelines_without_public_exports() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("server.harn");
    std::fs::write(
        &path,
        r"
pipeline default(harness: Harness, task: unknown) {
  harness.stdio.println(task)
}
",
    )
    .expect("write script");

    let catalog = ExportCatalog::from_path(&path).expect("catalog");
    let default = catalog.function("default").expect("default pipeline");
    assert_eq!(default.kind, ExportedCallableKind::Pipeline);
    assert_eq!(default.params.len(), 1);
    assert_eq!(default.params[0].name, "task");
    assert!(default.input_schema["properties"].get("harness").is_none());
}

/// Build a catalog from inline source, asserting it parses cleanly.
fn catalog_from_source(source: &str) -> ExportCatalog {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("server.harn");
    std::fs::write(&path, source).expect("write script");
    ExportCatalog::from_path(&path).expect("catalog")
}

#[test]
fn well_formed_attributes_emit_no_diagnostics() {
    let catalog = catalog_from_source(
        r#"
@scopes("personas:read")
@route("POST", "/users/{id}")
pub fn update_user(req: dict) -> dict { return req }

@route("/health")
pub fn liveness(req: dict) -> dict { return req }
"#,
    );
    assert!(
        catalog.diagnostics().is_empty(),
        "unexpected diagnostics: {:?}",
        catalog.diagnostics()
    );
}

#[test]
fn route_with_non_string_arg_is_diagnosed_and_unmounted() {
    // The second arg is an identifier, not a string literal. Left
    // unchecked the collector would treat this as `@route("GET")` and
    // mis-mount the handler at `/GET`.
    let catalog = catalog_from_source(
        r#"
pub fn make_path(req: dict) -> string { return "/x" }

@route("GET", make_path)
pub fn handler_users(req: dict) -> dict { return req }
"#,
    );
    let handler = catalog.function("handler_users").expect("handler_users");
    assert_eq!(
        handler.route, None,
        "a malformed @route must not fall back to the handler_ convention route"
    );
    let codes: Vec<&str> = catalog.diagnostics().iter().map(|d| d.code).collect();
    assert_eq!(codes, vec![ROUTE_ARG_NOT_STRING]);
}

#[test]
fn route_with_zero_args_is_diagnosed_and_unmounted() {
    let catalog = catalog_from_source(
        r"
@route()
pub fn handler_status(req: dict) -> dict { return req }
",
    );
    let handler = catalog.function("handler_status").expect("handler_status");
    assert_eq!(handler.route, None);
    let codes: Vec<&str> = catalog.diagnostics().iter().map(|d| d.code).collect();
    assert_eq!(codes, vec![ROUTE_BAD_ARITY]);
}

#[test]
fn route_with_too_many_args_is_diagnosed_and_unmounted() {
    let catalog = catalog_from_source(
        r#"
@route("GET", "/x", "/y")
pub fn handler_overspecified(req: dict) -> dict { return req }
"#,
    );
    let handler = catalog
        .function("handler_overspecified")
        .expect("handler_overspecified");
    assert_eq!(handler.route, None);
    let codes: Vec<&str> = catalog.diagnostics().iter().map(|d| d.code).collect();
    assert_eq!(codes, vec![ROUTE_BAD_ARITY]);
}

#[test]
fn scopes_with_non_string_arg_is_diagnosed_but_keeps_valid_scopes() {
    let catalog = catalog_from_source(
        r#"
pub fn make_scope(req: dict) -> string { return "sessions:write" }

@scopes("personas:read", make_scope)
pub fn list_sessions() -> string { return "ok" }
"#,
    );
    let list = catalog.function("list_sessions").expect("list_sessions");
    // The valid literal is still enforced; only the bad arg is dropped.
    assert_eq!(
        list.required_scopes,
        BTreeSet::from(["personas:read".to_string()])
    );
    let diagnostic = catalog
        .diagnostics()
        .iter()
        .find(|d| d.code == SCOPES_ARG_NOT_STRING)
        .expect("scopes diagnostic");
    assert!(diagnostic.message.contains("list_sessions"));
}

#[test]
fn policy_attribute_parses_allowed_kinds() {
    let catalog = catalog_from_source(
        r#"
@scopes("admin:dlq:write")
@policy(kinds: "operator platform_admin", matches: "tenant owner", methods: "doc.read doc.write")
@route("POST", "/admin/dlq/replay")
pub fn replay_dlq(req: dict) -> dict { return req }
"#,
    );
    assert!(
        catalog.diagnostics().is_empty(),
        "unexpected diagnostics: {:?}",
        catalog.diagnostics()
    );
    let policy = catalog
        .function("replay_dlq")
        .expect("replay_dlq")
        .policy
        .as_ref()
        .expect("policy present");
    assert_eq!(
        policy.allowed_kinds,
        BTreeSet::from(["operator".to_string(), "platform_admin".to_string()])
    );
    assert_eq!(
        policy.match_labels,
        BTreeSet::from(["owner".to_string(), "tenant".to_string()])
    );
    assert_eq!(
        policy.method_guards,
        BTreeSet::from(["doc.read".to_string(), "doc.write".to_string()])
    );
}

#[test]
fn policy_without_attribute_leaves_policy_none() {
    let catalog = catalog_from_source(
        r#"
@route("GET", "/open")
pub fn open_route(req: dict) -> dict { return req }
"#,
    );
    assert!(catalog
        .function("open_route")
        .expect("open_route")
        .policy
        .is_none());
}

#[test]
fn policy_with_unknown_arg_is_diagnosed_and_dropped() {
    let catalog = catalog_from_source(
        r#"
@policy(roles: "operator")
@route("POST", "/x")
pub fn guarded(req: dict) -> dict { return req }
"#,
    );
    // The unrecognized `roles:` key is dropped, leaving no effective
    // policy — and a loud diagnostic is emitted.
    assert!(catalog
        .function("guarded")
        .expect("guarded")
        .policy
        .is_none());
    let diagnostic = catalog
        .diagnostics()
        .iter()
        .find(|d| d.code == POLICY_BAD_ARGS)
        .expect("policy diagnostic");
    assert!(diagnostic.message.contains("guarded"));
}

#[test]
fn job_attribute_parses_name_schedule_queue_and_retry() {
    let catalog = catalog_from_source(
        r#"
@job("scan", retry: { max: 3, backoff: "exponential" })
@schedule("0 * * * *", "UTC")
@queue("scan-jobs")
pub fn scan(harness: Harness, event: TriggerEvent) -> dict { return {ok: true} }

@job
pub fn sweep(harness: Harness, event: TriggerEvent) -> dict { return {ok: true} }

pub fn helper(req: dict) -> dict { return req }
"#,
    );
    assert!(
        catalog.diagnostics().is_empty(),
        "unexpected diagnostics: {:?}",
        catalog.diagnostics()
    );

    let scan = catalog.function("scan").expect("scan export");
    let job = scan.job.as_ref().expect("scan is a job");
    assert_eq!(job.name, "scan");
    assert_eq!(
        job.schedule,
        Some(ScheduleSpec {
            cron: "0 * * * *".to_string(),
            timezone: Some("UTC".to_string()),
        })
    );
    assert_eq!(job.queue.as_deref(), Some("scan-jobs"));
    assert_eq!(
        job.retry,
        Some(RetrySpec {
            max_attempts: 3,
            backoff: RetryBackoff::Exponential,
        })
    );

    // Bare `@job` defaults the job name to the function name and
    // carries no schedule/queue/retry.
    let sweep = catalog.function("sweep").expect("sweep export");
    let sweep_job = sweep.job.as_ref().expect("sweep is a job");
    assert_eq!(sweep_job.name, "sweep");
    assert!(sweep_job.schedule.is_none());
    assert!(sweep_job.queue.is_none());
    assert!(sweep_job.retry.is_none());

    // A plain `pub fn` is not a job.
    let helper = catalog.function("helper").expect("helper export");
    assert!(helper.job.is_none());
}

#[test]
fn job_with_non_string_name_is_diagnosed_and_unregistered() {
    let catalog = catalog_from_source(
        r#"
pub fn name_of(event: TriggerEvent) -> string { return "x" }

@job(name_of)
pub fn scan(harness: Harness, event: TriggerEvent) -> dict { return {ok: true} }
"#,
    );
    let scan = catalog.function("scan").expect("scan export");
    assert!(scan.job.is_none());
    let codes: Vec<&str> = catalog.diagnostics().iter().map(|d| d.code).collect();
    assert_eq!(codes, vec![JOB_BAD_NAME]);
}

#[test]
fn schedule_modifier_without_job_is_diagnosed() {
    let catalog = catalog_from_source(
        r#"
@schedule("0 * * * *")
pub fn orphan(harness: Harness, event: TriggerEvent) -> dict { return {ok: true} }
"#,
    );
    let orphan = catalog.function("orphan").expect("orphan export");
    assert!(orphan.job.is_none());
    let codes: Vec<&str> = catalog.diagnostics().iter().map(|d| d.code).collect();
    assert_eq!(codes, vec![JOB_MODIFIER_WITHOUT_JOB]);
}

#[test]
fn retry_with_unknown_backoff_keeps_max_and_diagnoses() {
    let catalog = catalog_from_source(
        r#"
@job("scan", retry: { max: 5, backoff: "wishful" })
pub fn scan(harness: Harness, event: TriggerEvent) -> dict { return {ok: true} }
"#,
    );
    let scan = catalog.function("scan").expect("scan export");
    let retry = scan
        .job
        .as_ref()
        .expect("job")
        .retry
        .as_ref()
        .expect("retry");
    // The valid `max` survives; the bad backoff falls back to svix.
    assert_eq!(retry.max_attempts, 5);
    assert_eq!(retry.backoff, RetryBackoff::Svix);
    let codes: Vec<&str> = catalog.diagnostics().iter().map(|d| d.code).collect();
    assert_eq!(codes, vec![RETRY_BAD_ARGS]);
}

#[test]
fn standalone_retry_unknown_key_is_diagnosed() {
    let catalog = catalog_from_source(
        r#"
@job("scan")
@retry(max: 5, patience: "high")
pub fn scan(harness: Harness, event: TriggerEvent) -> dict { return {ok: true} }
"#,
    );
    let scan = catalog.function("scan").expect("scan export");
    let retry = scan
        .job
        .as_ref()
        .expect("job")
        .retry
        .as_ref()
        .expect("retry");
    assert_eq!(retry.max_attempts, 5);
    assert_eq!(retry.backoff, RetryBackoff::Svix);
    let codes: Vec<&str> = catalog.diagnostics().iter().map(|d| d.code).collect();
    assert_eq!(codes, vec![RETRY_BAD_ARGS]);
}

#[test]
fn stream_attribute_marks_routed_functions_only() {
    let catalog = catalog_from_source(
        r#"
@stream
@route("GET", "/events")
pub fn events(req: dict) -> dict { return http_ok({}) }

@stream
pub fn handler_feed(req: dict) -> dict { return http_ok({}) }

@route("GET", "/plain")
pub fn plain(req: dict) -> dict { return http_ok({}) }
"#,
    );
    assert!(
        catalog.diagnostics().is_empty(),
        "unexpected diagnostics: {:?}",
        catalog.diagnostics()
    );
    // Works with an explicit @route and with the handler_* convention.
    assert!(catalog.function("events").expect("events").stream);
    assert!(catalog.function("handler_feed").expect("feed").stream);
    // A routed fn without the marker is a plain dispatch route.
    assert!(!catalog.function("plain").expect("plain").stream);
}

#[test]
fn stream_with_args_is_diagnosed_and_dropped() {
    let catalog = catalog_from_source(
        r#"
@stream("sse")
@route("GET", "/events")
pub fn events(req: dict) -> dict { return http_ok({}) }
"#,
    );
    assert!(!catalog.function("events").expect("events").stream);
    let codes: Vec<&str> = catalog.diagnostics().iter().map(|d| d.code).collect();
    assert_eq!(codes, vec![STREAM_BAD_ARGS]);
}

#[test]
fn stream_without_route_is_diagnosed_and_ignored() {
    let catalog = catalog_from_source(
        r"
@stream
pub fn helper(req: dict) -> dict { return req }
",
    );
    assert!(!catalog.function("helper").expect("helper").stream);
    let codes: Vec<&str> = catalog.diagnostics().iter().map(|d| d.code).collect();
    assert_eq!(codes, vec![STREAM_WITHOUT_ROUTE]);
}

#[test]
fn raw_attribute_marks_routed_functions_only() {
    let catalog = catalog_from_source(
        r#"
@raw
@route("POST", "/packs/publish")
pub fn publish(req: dict) -> dict { return http_ok({}) }

@raw
pub fn handler_upload(req: dict) -> dict { return http_ok({}) }

@route("GET", "/plain")
pub fn plain(req: dict) -> dict { return http_ok({}) }
"#,
    );
    assert!(
        catalog.diagnostics().is_empty(),
        "unexpected diagnostics: {:?}",
        catalog.diagnostics()
    );
    // Works with an explicit @route and with the handler_* convention.
    assert!(catalog.function("publish").expect("publish").raw);
    assert!(catalog.function("handler_upload").expect("upload").raw);
    // A routed fn without the marker is a plain dispatch route.
    assert!(!catalog.function("plain").expect("plain").raw);
    // `@raw` never implies `@stream`.
    assert!(!catalog.function("publish").expect("publish").stream);
}

#[test]
fn raw_with_args_is_diagnosed_and_dropped() {
    let catalog = catalog_from_source(
        r#"
@raw("bytes")
@route("POST", "/upload")
pub fn upload(req: dict) -> dict { return http_ok({}) }
"#,
    );
    assert!(!catalog.function("upload").expect("upload").raw);
    let codes: Vec<&str> = catalog.diagnostics().iter().map(|d| d.code).collect();
    assert_eq!(codes, vec![RAW_BAD_ARGS]);
}

#[test]
fn raw_without_route_is_diagnosed_and_ignored() {
    let catalog = catalog_from_source(
        r"
@raw
pub fn helper(req: dict) -> dict { return req }
",
    );
    assert!(!catalog.function("helper").expect("helper").raw);
    let codes: Vec<&str> = catalog.diagnostics().iter().map(|d| d.code).collect();
    assert_eq!(codes, vec![RAW_WITHOUT_ROUTE]);
}

#[test]
fn raw_conflicting_with_stream_is_diagnosed_and_dropped() {
    let catalog = catalog_from_source(
        r#"
@stream
@raw
@route("GET", "/both")
pub fn both(req: dict) -> dict { return http_ok({}) }
"#,
    );
    // `@stream` wins; `@raw` is dropped with a diagnostic.
    let function = catalog.function("both").expect("both");
    assert!(function.stream);
    assert!(!function.raw);
    let codes: Vec<&str> = catalog.diagnostics().iter().map(|d| d.code).collect();
    assert_eq!(codes, vec![RAW_CONFLICTS_WITH_STREAM]);
}

#[test]
fn ws_attribute_marks_routed_functions_only() {
    let catalog = catalog_from_source(
        r#"
@ws
@route("GET", "/socket")
pub fn socket(req: dict) -> dict { return http_ok({}) }

@ws
pub fn handler_live(req: dict) -> dict { return http_ok({}) }

@route("GET", "/plain")
pub fn plain(req: dict) -> dict { return http_ok({}) }
"#,
    );
    assert!(
        catalog.diagnostics().is_empty(),
        "unexpected diagnostics: {:?}",
        catalog.diagnostics()
    );
    // Works with an explicit @route and with the handler_* convention.
    assert!(catalog.function("socket").expect("socket").ws);
    assert!(catalog.function("handler_live").expect("live").ws);
    // A routed fn without the marker is a plain dispatch route.
    assert!(!catalog.function("plain").expect("plain").ws);
    // `@ws` never implies `@stream` / `@raw`.
    assert!(!catalog.function("socket").expect("socket").stream);
    assert!(!catalog.function("socket").expect("socket").raw);
}

#[test]
fn ws_with_args_is_diagnosed_and_dropped() {
    let catalog = catalog_from_source(
        r#"
@ws("chat")
@route("GET", "/socket")
pub fn socket(req: dict) -> dict { return http_ok({}) }
"#,
    );
    assert!(!catalog.function("socket").expect("socket").ws);
    let codes: Vec<&str> = catalog.diagnostics().iter().map(|d| d.code).collect();
    assert_eq!(codes, vec![WS_BAD_ARGS]);
}

#[test]
fn ws_without_route_is_diagnosed_and_ignored() {
    let catalog = catalog_from_source(
        r"
@ws
pub fn helper(req: dict) -> dict { return req }
",
    );
    assert!(!catalog.function("helper").expect("helper").ws);
    let codes: Vec<&str> = catalog.diagnostics().iter().map(|d| d.code).collect();
    assert_eq!(codes, vec![WS_WITHOUT_ROUTE]);
}

#[test]
fn ws_combined_with_stream_carries_both_flags_without_diagnostic() {
    // `@ws` + `@stream` is the *combined* route (one route that both
    // upgrades a genuine WebSocket handshake and falls through to the
    // SSE/stream path otherwise): both flags survive, no diagnostic.
    let catalog = catalog_from_source(
        r#"
@stream
@ws
@route("GET", "/both")
pub fn both(req: dict) -> dict { return http_ok({}) }
"#,
    );
    let function = catalog.function("both").expect("both");
    assert!(function.stream);
    assert!(function.ws);
    assert!(!function.raw);
    let codes: Vec<&str> = catalog.diagnostics().iter().map(|d| d.code).collect();
    assert!(
        codes.is_empty(),
        "combined @ws @stream must not be diagnosed, got {codes:?}"
    );
}

#[test]
fn ws_conflicting_with_raw_is_diagnosed_and_dropped() {
    let catalog = catalog_from_source(
        r#"
@raw
@ws
@route("POST", "/both")
pub fn both(req: dict) -> dict { return http_ok({}) }
"#,
    );
    // `@raw` wins; `@ws` is dropped with a diagnostic.
    let function = catalog.function("both").expect("both");
    assert!(function.raw);
    assert!(!function.ws);
    let codes: Vec<&str> = catalog.diagnostics().iter().map(|d| d.code).collect();
    assert_eq!(codes, vec![WS_CONFLICTS_WITH_STREAM_OR_RAW]);
}