okami.mom/albedo

git clone https://git.nkp.pet/okami.mom/albedo

Aly Raffaufrefactor(gleam): clean up code and enforce focused lint checksb585313

main
5.4 KiB131 linesraw
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 {
41  json.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 {
48  let assert Ok(fake) = start_fake()
49  let 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.
52  report.account_id |> should.equal(Some("acct-1"))
53  report.email |> should.equal(Some("dawn@example.com"))
54  report.plan |> should.equal(Some("token"))
55  report.error |> should.equal(None)
56  let assert [limit] = report.limits
57  limit.id |> should.equal("five_hour")
58  limit.label |> should.equal("Session")
59  limit.used_percent |> should.equal(Some(37.0))
60  limit.window_label |> should.equal(Some("5h window"))
61  limit.window_seconds |> should.equal(Some(18_000))
62  limit.resets_at |> should.equal(Some(1_893_456_000_000))
63  limit.scope |> should.equal(None)
64  limit.status |> should.equal("ok")
65  // The HTTP request went through albedo's HTTP client to the local endpoint.
66  fake_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.
69  let envelope = fake_envelope(fake)
70  envelope |> string.contains("\"provider\":\"fake\"") |> should.be_true
71  envelope |> string.contains("tok_n0t_a_real_secret") |> should.be_true
72  envelope |> string.contains("\"nowMs\":1234567890") |> should.be_true
73  envelope |> string.contains("\"responses\":[]") |> should.be_true
74  envelope |> 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.
77  let dumped = fake_env_dump(fake)
78  dumped |> string.contains("ALBEDO_USAGE_TEST_CANARY") |> should.be_false
79  dumped |> string.contains("ALBEDO_USAGE_CORE") |> should.be_false
80  dumped |> string.contains("PATH=") |> should.be_true
81  dumped |> string.contains("HOME=") |> should.be_true
82  stop_fake(fake)
83}
84
85pub fn a_provider_error_is_data_not_a_driver_failure_test() -> Nil {
86  let assert Ok(fake) = start_fake()
87  let assert Ok(report) = usage_feed.fetch("broken", credential(), 0)
88  report.error |> should.equal(Some("the provider is unreachable"))
89  report.limits |> should.equal([])
90  report.account_id |> should.equal(None)
91  stop_fake(fake)
92}
93
94pub fn a_feed_that_never_reports_is_a_driver_failure_test() -> Nil {
95  let assert Ok(fake) = start_fake()
96  let assert Error(message) = usage_feed.fetch("endless", credential(), 0)
97  message |> string.contains("did not finish in 6 rounds") |> should.be_true
98  stop_fake(fake)
99}
100
101pub fn a_missing_binary_is_a_driver_failure_test() -> Nil {
102  let assert Ok(fake) = start_fake()
103  break_binary(fake)
104  let assert Error(message) = usage_feed.fetch("fake", credential(), 0)
105  message |> string.contains("usage is not built") |> should.be_true
106  stop_fake(fake)
107}
108
109pub fn a_command_off_path_answers_126_test() -> Nil {
110  let 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.
113  let assert Ok(report) = usage_feed.fetch("missing-command", credential(), 0)
114  report.error |> should.equal(Some("command answered 126"))
115  stop_fake(fake)
116}
117
118pub fn a_trickling_command_hits_its_deadline_and_is_killed_test() -> Nil {
119  let 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.
122  let started = mono_ms()
123  let assert Ok(report) = usage_feed.fetch("trickle", credential(), 0)
124  let elapsed = mono_ms() - started
125  report.error |> should.equal(Some("command answered 126"))
126  // The request's 1s timeoutMs is honoured, not the 15s default.
127  should.be_true(elapsed < 4000)
128  // And the child was killed, not orphaned with closed pipes.
129  trickle_pid_gone(fake) |> should.be_true
130  stop_fake(fake)
131}