| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { cleanHandle, dollarsField, microsFromDollars, readAgentForm } from "./agent-form.ts"; |
| 5 | |
| 6 | function form(fields: Record<string, string>): FormData { |
| 7 | const data = new FormData(); |
| 8 | for (const [key, value] of Object.entries(fields)) data.set(key, value); |
| 9 | return data; |
| 10 | } |
| 11 | |
| 12 | test("handles are cleaned as typed", () => { |
| 13 | assert.equal(cleanHandle("@Release Manager!"), "release-manager"); |
| 14 | assert.equal(cleanHandle("--ship--"), "ship"); |
| 15 | }); |
| 16 | |
| 17 | test("the agent form becomes what the service takes, in micro-dollars", () => { |
| 18 | const read = readAgentForm( |
| 19 | form({ |
| 20 | display_name: "Ship", |
| 21 | handle: "", |
| 22 | role: "Cuts releases", |
| 23 | instructions: "Cut a release on Tuesdays.", |
| 24 | personality_preset: "terse", |
| 25 | floor: "large", |
| 26 | ceiling: "none", |
| 27 | providers: "g1t", |
| 28 | monthly: "40", |
| 29 | daily: "", |
| 30 | task: "2.50", |
| 31 | merge: "never", |
| 32 | capacity: "2", |
| 33 | }), |
| 34 | ); |
| 35 | assert.ok(read.ok); |
| 36 | assert.equal(read.input.handle, "ship"); |
| 37 | assert.equal(read.input.personality_preset, "terse"); |
| 38 | assert.deepEqual(read.input.routing, { floor: "large", ceiling: null, providers: ["g1t"], pinned: null }); |
| 39 | assert.deepEqual(read.input.budget, { monthly_micros: 40_000_000, daily_micros: null, task_micros: 2_500_000 }); |
| 40 | assert.equal(read.input.autonomy?.merge, "never"); |
| 41 | assert.equal(read.input.autonomy?.open_pull_requests, "alone"); |
| 42 | assert.equal(read.input.capacity, 2); |
| 43 | }); |
| 44 | |
| 45 | test("the agent form says what to fix", () => { |
| 46 | const read = readAgentForm(form({ display_name: "", handle: "g1t", floor: "frontier", ceiling: "small", monthly: "lots", capacity: "0" })); |
| 47 | assert.ok(!read.ok); |
| 48 | assert.deepEqual(Object.keys(read.errors).sort(), ["capacity", "ceiling", "display_name", "handle", "instructions", "monthly", "role"]); |
| 49 | }); |
| 50 | |
| 51 | test("dollars and micro-dollars", () => { |
| 52 | assert.equal(microsFromDollars("$12.50"), 12_500_000); |
| 53 | assert.equal(microsFromDollars(""), null); |
| 54 | assert.ok(Number.isNaN(microsFromDollars("lots"))); |
| 55 | assert.equal(dollarsField(2_500_000), "2.50"); |
| 56 | assert.equal(dollarsField(50_000_000), "50"); |
| 57 | }); |