okami.mom/albedo
git clone https://git.nkp.pet/okami.mom/albedo
b585313
main
1//// The usage-feed driver against a fake usage CLI (ALBEDO_USAGE_CORE), a 2//// local HTTP endpoint and a command that dumps its own environment. The 3//// daemon has no route for quota yet, so no e2e scenario can reach this loop; 4//// the bugs it catches are the host half drifting from the core's protocol: 5//// the credential landing in argv instead of stdin, responses not replayed, 6//// or the command environment growing beyond the allowlist. 7 8import albedo/harness/usage_feed 9import gleam/json 10import gleam/option .{ None , Some } 11import gleam/string 12import gleeunit/should 13 14pub type Fake 15 16@external ( erlang , "albedo_usage_test_support" , "start" ) 17fn start_fake () -> Result ( Fake , Nil ) 18 19@external ( erlang , "albedo_usage_test_support" , "stop" ) 20fn stop_fake ( fake : Fake ) -> Nil 21 22@external ( erlang , "albedo_usage_test_support" , "envelope" ) 23fn fake_envelope ( fake : Fake ) -> String 24 25@external ( erlang , "albedo_usage_test_support" , "env_dump" ) 26fn fake_env_dump ( fake : Fake ) -> String 27 28@external ( erlang , "albedo_usage_test_support" , "request_line" ) 29fn fake_request_line ( fake : Fake ) -> List ( String ) 30 31@external ( erlang , "albedo_usage_test_support" , "break_binary" ) 32fn break_binary ( fake : Fake ) -> Nil 33 34@external ( erlang , "albedo_usage_test_support" , "mono_ms" ) 35fn mono_ms () -> Int 36 37@external ( erlang , "albedo_usage_test_support" , "trickle_pid_gone" ) 38fn trickle_pid_gone ( fake : Fake ) -> Bool 39 40fn credential () -> json . Json { 41json . object ([ 42#( "kind" , json . string ( "oauth" )), 43#( "access" , json . string ( "tok_n0t_a_real_secret" )), 44]) 45} 46 47pub fn fetch_runs_http_and_command_rounds_test () -> Nil { 48let assert Ok ( fake ) = start_fake () 49let assert Ok ( report ) = usage_feed . fetch ( "fake" , credential (), 1_234_567_890 ) 50// The fake core built the report from the real HTTP response body, which 51// only the loop could have delivered. 52report . account_id |> should . equal ( Some ( "acct-1" )) 53report . |> should . equal ( Some ( "dawn@example.com" )) 54report . plan |> should . equal ( Some ( "token" )) 55report . error |> should . equal ( None ) 56let assert [ limit ] = report . limits 57limit . id |> should . equal ( "five_hour" ) 58limit . label |> should . equal ( "Session" ) 59limit . used_percent |> should . equal ( Some ( 37.0 )) 60limit . window_label |> should . equal ( Some ( "5h window" )) 61limit . window_seconds |> should . equal ( Some ( 18_000 )) 62limit . resets_at |> should . equal ( Some ( 1_893_456_000_000 )) 63limit . scope |> should . equal ( None ) 64limit . status |> should . equal ( "ok" ) 65// The HTTP request went through albedo's HTTP client to the local endpoint. 66fake_request_line ( fake ) |> should . equal ([ "GET /usage HTTP/1.1" ]) 67// The envelope crossed on stdin, never argv: both logged rounds carry the 68// provider, the credential and nowMs, and the second replays the responses. 69let envelope = fake_envelope ( fake ) 70envelope |> string . contains ( "\"provider\":\"fake\"" ) |> should . be_true 71envelope |> string . contains ( "tok_n0t_a_real_secret" ) |> should . be_true 72envelope |> string . contains ( "\"nowMs\":1234567890" ) |> should . be_true 73envelope |> string . contains ( "\"responses\":[]" ) |> should . be_true 74envelope |> string . contains ( "\"status\":200" ) |> should . be_true 75// The command ran with only the allowed environment: no canary, and the 76// resolver override itself never reached the child. 77let dumped = fake_env_dump ( fake ) 78dumped |> string . contains ( "ALBEDO_USAGE_TEST_CANARY" ) |> should . be_false 79dumped |> string . contains ( "ALBEDO_USAGE_CORE" ) |> should . be_false 80dumped |> string . contains ( "PATH=" ) |> should . be_true 81dumped |> string . contains ( "HOME=" ) |> should . be_true 82stop_fake ( fake ) 83} 84 85pub fn a_provider_error_is_data_not_a_driver_failure_test () -> Nil { 86let assert Ok ( fake ) = start_fake () 87let assert Ok ( report ) = usage_feed . fetch ( "broken" , credential (), 0 ) 88report . error |> should . equal ( Some ( "the provider is unreachable" )) 89report . limits |> should . equal ([]) 90report . account_id |> should . equal ( None ) 91stop_fake ( fake ) 92} 93 94pub fn a_feed_that_never_reports_is_a_driver_failure_test () -> Nil { 95let assert Ok ( fake ) = start_fake () 96let assert Error ( message ) = usage_feed . fetch ( "endless" , credential (), 0 ) 97message |> string . contains ( "did not finish in 6 rounds" ) |> should . be_true 98stop_fake ( fake ) 99} 100 101pub fn a_missing_binary_is_a_driver_failure_test () -> Nil { 102let assert Ok ( fake ) = start_fake () 103break_binary ( fake ) 104let assert Error ( message ) = usage_feed . fetch ( "fake" , credential (), 0 ) 105message |> string . contains ( "usage is not built" ) |> should . be_true 106stop_fake ( fake ) 107} 108 109pub fn a_command_off_path_answers_126_test () -> Nil { 110let assert Ok ( fake ) = start_fake () 111// The fake core asks for a command that does not exist; the feed routes 112// around a non-zero status, so the report still arrives. 113let assert Ok ( report ) = usage_feed . fetch ( "missing-command" , credential (), 0 ) 114report . error |> should . equal ( Some ( "command answered 126" )) 115stop_fake ( fake ) 116} 117 118pub fn a_trickling_command_hits_its_deadline_and_is_killed_test () -> Nil { 119let assert Ok ( fake ) = start_fake () 120// The trickle command always has output but never exits: an idle timeout 121// would let it run forever, so only the deadline across chunks ends it. 122let started = mono_ms () 123let assert Ok ( report ) = usage_feed . fetch ( "trickle" , credential (), 0 ) 124let elapsed = mono_ms () - started 125report . error |> should . equal ( Some ( "command answered 126" )) 126// The request's 1s timeoutMs is honoured, not the 15s default. 127should . be_true ( elapsed < 4000 ) 128// And the child was killed, not orphaned with closed pipes. 129trickle_pid_gone ( fake ) |> should . be_true 130stop_fake ( fake ) 131}