Public: anyone can read this; the crew can change it.
Data: object buckets with price reads and binding to an app secret (#16696) #17086
Merged 41h 52m ago, live in production
scout/forge:scout/17073-data-object-buckets-with-price-reads-and → main 0 commits fork 2 threads · 2 unresolved
9 files
changed +585
−119
docs/features/infra.md +6
| @@ -25,3 +25,9 @@ Build refuses a plan that changed since it was shown, so nobody builds what they | ||
| 25 | 25 | ## The build |
| 26 | 26 | |
| 27 | 27 | Build runs in the background and appends `infra.applied` (the wants and each step's sentence), then one `infra.step` per step (started, done or failed), or `infra.refused` when it was refused before its first step. The tray is a live region on those events: each step shows as it happens, then "Ready at" its name with the link. A build that stopped says why with Build again, which plans the same wants against what exists now and skips what is done. |
| 28 | ||
| 29 | ## Buckets (#17073) | |
| 30 | ||
| 31 | Phase 3's first kind, DigitalOcean Spaces (`src/cloud/bucket.ts`; a new provider is one more row of `BUCKETS`). In Talk or MCP, `infra` `price` with `bucket: {region, name, stored_gib?, out_gib?}` says what a bucket costs a month and where it would answer: the provider's list price ($5 with 250 GiB stored and 1 TiB out in it, 2¢ and 1¢ a GiB over), held as numbers because Spaces has no price endpoint. `bind` with `repo` sets that app's secret `BUCKET_URL` to the bucket's address (`src/actions/bucket.ts`), which restarts the app as any secret change does. Not done yet: making the bucket from a plan (it needs Spaces keys, which the forge does not hold), Hetzner and B2, and the page and terminal doors; make the bucket at the provider and keep its keys as the app's secrets. | |
| 32 | ||
| 33 | Terminal: the **Buckets** palette entry (`src/tui/screens/buckets.ts`): `p` reads a price for anyone signed in, `b` binds a bucket's address to an app as `BUCKET_URL`, a forge admin's only. |
src/actions/bucket.ts added +46
| @@ -0,0 +1,46 @@ | ||
| 1 | /** | |
| 2 | * Buckets on the owner's cloud (#17073): what one costs a month, and binding one to an app. The | |
| 3 | * binding sets the bucket's address as the app's secret `BUCKET_URL` (at the repository's scope, so | |
| 4 | * its previews and production read it), which restarts the app as any secret change does. The | |
| 5 | * forge does not make the bucket or hold its keys: a person makes it at the provider, and puts the | |
| 6 | * keys in as the app's own secrets. | |
| 7 | */ | |
| 8 | ||
| 9 | import type { App } from "../app.ts"; | |
| 10 | import { bucketCents, bucketOf, bucketUrl } from "../cloud/bucket.ts"; | |
| 11 | import { dollars } from "../cloud/prices.ts"; | |
| 12 | import type { User } from "../core/users.ts"; | |
| 13 | import { err, ok, type Result } from "../kernel/result.ts"; | |
| 14 | import { putSecret } from "./secrets.ts"; | |
| 15 | ||
| 16 | export const BUCKET_SECRET = "BUCKET_URL"; | |
| 17 | ||
| 18 | export interface BucketRead { | |
| 19 | readonly url: string; | |
| 20 | readonly cents: number; | |
| 21 | readonly says: string; | |
| 22 | } | |
| 23 | ||
| 24 | /** What a bucket costs a month, in a sentence; reads nothing, so anyone may ask. */ | |
| 25 | export function readBucket(provider: string, region: string, name: string, storedGiB = 0, outGiB = 0): Result<BucketRead, string> { | |
| 26 | const b = bucketOf(provider, region, name); | |
| 27 | if (!b.ok) return b; | |
| 28 | const cents = bucketCents(b.value.kind, storedGiB, outGiB); | |
| 29 | if (!cents.ok) return cents; | |
| 30 | const k = b.value.kind; | |
| 31 | const url = bucketUrl(k, region, name); | |
| 32 | return ok({ | |
| 33 | url, | |
| 34 | cents: cents.value, | |
| 35 | says: `A ${k.label} bucket in ${k.regions[region]} is ${dollars(cents.value)} a month, with ${k.storageGiB} GiB stored and ${k.transferGiB} GiB sent out in it; ${storedGiB} GiB stored and ${outGiB} GiB out comes to that. It would answer at ${url}.`, | |
| 36 | }); | |
| 37 | } | |
| 38 | ||
| 39 | /** Set `BUCKET_URL` for the app `repo` (`owner/name`) to the bucket's address. */ | |
| 40 | export function bindBucket(app: App, actor: User | undefined, repo: string, provider: string, region: string, name: string): Result<BucketRead, string> { | |
| 41 | const read = readBucket(provider, region, name); | |
| 42 | if (!read.ok) return read; | |
| 43 | const set = putSecret(app, actor, "repo", repo, BUCKET_SECRET, read.value.url); | |
| 44 | if (!set.ok) return err(set.error === "not_found" ? "No such app, or you may not set its secrets." : "The secret could not be set."); | |
| 45 | return ok({ ...read.value, says: `${repo} now reads ${BUCKET_SECRET}=${read.value.url}; it restarts to take it. Make the bucket at the provider if it does not exist, and keep its keys as secrets of the app.` }); | |
| 46 | } |
src/cloud/bucket.ts added +87
| @@ -0,0 +1,87 @@ | ||
| 1 | /** | |
| 2 | * Object buckets (#16696, phase 3, #17073): a bucket's name, its address and what it costs a | |
| 3 | * month, as plain data. One provider to start: DigitalOcean Spaces. A new provider is one more row | |
| 4 | * of `BUCKETS` and nothing else here changes. | |
| 5 | * | |
| 6 | * The price is the provider's published list price, held here as numbers (US cents), because | |
| 7 | * Spaces has no price endpoint to read; it is a flat base that includes an allowance of storage and | |
| 8 | * outbound transfer, and a rate for each GiB over. Figures as published for Spaces ($5 a month with | |
| 9 | * 250 GiB stored and 1 TiB out; 2¢ and 1¢ a GiB over), NOT re-checked against DigitalOcean's | |
| 10 | * pricing page on 2026-12-01: this build cannot reach it. Check the page before relying on them. | |
| 11 | * The forge makes no provider call in this module. | |
| 12 | */ | |
| 13 | ||
| 14 | import { err, ok, type Result } from "../kernel/result.ts"; | |
| 15 | ||
| 16 | export interface BucketKind { | |
| 17 | /** The provider's id, as in `src/cloud/providers.ts`. */ | |
| 18 | readonly provider: string; | |
| 19 | readonly label: string; | |
| 20 | /** Regions a bucket can live in, id to city. */ | |
| 21 | readonly regions: Readonly<Record<string, string>>; | |
| 22 | /** The host a bucket answers on: `<bucket>.<region>.<domain>`. */ | |
| 23 | readonly domain: string; | |
| 24 | /** | |
| 25 | * The flat monthly base in cents, with `storageGiB` of storage and `transferGiB` of outbound | |
| 26 | * transfer in it. | |
| 27 | */ | |
| 28 | readonly baseCents: number; | |
| 29 | readonly storageGiB: number; | |
| 30 | readonly transferGiB: number; | |
| 31 | /** Cents per GiB over the allowance, a month. */ | |
| 32 | readonly storageOverCentsPerGiB: number; | |
| 33 | readonly transferOverCentsPerGiB: number; | |
| 34 | } | |
| 35 | ||
| 36 | export const BUCKETS: Readonly<Record<string, BucketKind>> = { | |
| 37 | digitalocean: { | |
| 38 | provider: "digitalocean", | |
| 39 | label: "DigitalOcean Spaces", | |
| 40 | regions: { nyc3: "New York", ams3: "Amsterdam", sfo3: "San Francisco", sgp1: "Singapore", fra1: "Frankfurt", syd1: "Sydney" }, | |
| 41 | domain: "digitaloceanspaces.com", | |
| 42 | baseCents: 500, | |
| 43 | storageGiB: 250, | |
| 44 | transferGiB: 1024, | |
| 45 | storageOverCentsPerGiB: 2, | |
| 46 | transferOverCentsPerGiB: 1, | |
| 47 | }, | |
| 48 | }; | |
| 49 | ||
| 50 | /** | |
| 51 | * A bucket's name: 3 to 63 of lowercase letters, digits and hyphens, not starting or ending on a | |
| 52 | * hyphen. | |
| 53 | */ | |
| 54 | export const BUCKET_NAME = /^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$/; | |
| 55 | ||
| 56 | export const BUCKET_WORDS = { | |
| 57 | provider: "No bucket is offered there yet: DigitalOcean Spaces is the one.", | |
| 58 | region: "That is not a region of that provider's buckets.", | |
| 59 | name: "A bucket's name is 3 to 63 lowercase letters, digits and hyphens, and does not start or end on a hyphen.", | |
| 60 | amount: "Storage and transfer are whole GiB, 0 or more.", | |
| 61 | } as const; | |
| 62 | ||
| 63 | /** A bucket's place and name checked, with its provider's kind. */ | |
| 64 | export function bucketOf(provider: string, region: string, name: string): Result<{ kind: BucketKind; region: string; name: string }, string> { | |
| 65 | const kind = Object.hasOwn(BUCKETS, provider) ? BUCKETS[provider] : undefined; | |
| 66 | if (kind === undefined) return err(BUCKET_WORDS.provider); | |
| 67 | if (!Object.hasOwn(kind.regions, region)) return err(BUCKET_WORDS.region); | |
| 68 | if (!BUCKET_NAME.test(name) || name.includes("--")) return err(BUCKET_WORDS.name); | |
| 69 | return ok({ kind, region, name }); | |
| 70 | } | |
| 71 | ||
| 72 | /** The address a bucket is reached at, with no path and no credential in it. */ | |
| 73 | export const bucketUrl = (kind: BucketKind, region: string, name: string): string => `https://${name}.${region}.${kind.domain}`; | |
| 74 | ||
| 75 | /** | |
| 76 | * What a month costs, US cents, for `storedGiB` kept and `outGiB` sent out: the base, plus each GiB | |
| 77 | * over its allowance. | |
| 78 | */ | |
| 79 | export function bucketCents(kind: BucketKind, storedGiB = 0, outGiB = 0): Result<number, string> { | |
| 80 | const whole = (n: number): boolean => Number.isInteger(n) && n >= 0 && n <= 1e9; | |
| 81 | if (!whole(storedGiB) || !whole(outGiB)) return err(BUCKET_WORDS.amount); | |
| 82 | return ok( | |
| 83 | kind.baseCents + | |
| 84 | Math.max(0, storedGiB - kind.storageGiB) * kind.storageOverCentsPerGiB + | |
| 85 | Math.max(0, outGiB - kind.transferGiB) * kind.transferOverCentsPerGiB, | |
| 86 | ); | |
| 87 | } |
src/generated/screens.ts +117 −115
| @@ -16,115 +16,116 @@ import * as s11 from "../tui/screens/billinglinks.ts"; | ||
| 16 | 16 | import * as s12 from "../tui/screens/bills.ts"; |
| 17 | 17 | import * as s13 from "../tui/screens/boards.ts"; |
| 18 | 18 | import * as s14 from "../tui/screens/bottleneck.ts"; |
| 19 | import * as s15 from "../tui/screens/bugs.ts"; | |
| 20 | import * as s16 from "../tui/screens/bulk.ts"; | |
| 21 | import * as s17 from "../tui/screens/captain.ts"; | |
| 22 | import * as s18 from "../tui/screens/childcredit.ts"; | |
| 23 | import * as s19 from "../tui/screens/claim_link.ts"; | |
| 24 | import * as s20 from "../tui/screens/claim_start.ts"; | |
| 25 | import * as s21 from "../tui/screens/cloud.ts"; | |
| 26 | import * as s22 from "../tui/screens/codes.ts"; | |
| 27 | import * as s23 from "../tui/screens/connections.ts"; | |
| 28 | import * as s24 from "../tui/screens/controls.ts"; | |
| 29 | import * as s25 from "../tui/screens/cores.ts"; | |
| 30 | import * as s26 from "../tui/screens/crewbudgets.ts"; | |
| 31 | import * as s27 from "../tui/screens/crewhandover.ts"; | |
| 32 | import * as s28 from "../tui/screens/crewkey.ts"; | |
| 33 | import * as s29 from "../tui/screens/crewsplits.ts"; | |
| 34 | import * as s30 from "../tui/screens/crewtop.ts"; | |
| 35 | import * as s31 from "../tui/screens/dataset.ts"; | |
| 36 | import * as s32 from "../tui/screens/design.ts"; | |
| 37 | import * as s33 from "../tui/screens/design_gallery.ts"; | |
| 38 | import * as s34 from "../tui/screens/devices.ts"; | |
| 39 | import * as s35 from "../tui/screens/disks.ts"; | |
| 40 | import * as s36 from "../tui/screens/displays.ts"; | |
| 41 | import * as s37 from "../tui/screens/domains.ts"; | |
| 42 | import * as s38 from "../tui/screens/dotfiles.ts"; | |
| 43 | import * as s39 from "../tui/screens/drift.ts"; | |
| 44 | import * as s40 from "../tui/screens/dutybudgets.ts"; | |
| 45 | import * as s41 from "../tui/screens/dutyswitch.ts"; | |
| 46 | import * as s42 from "../tui/screens/economics.ts"; | |
| 47 | import * as s43 from "../tui/screens/edgeonly.ts"; | |
| 48 | import * as s44 from "../tui/screens/experiments.ts"; | |
| 49 | import * as s45 from "../tui/screens/featurefeed.ts"; | |
| 50 | import * as s46 from "../tui/screens/features.ts"; | |
| 51 | import * as s47 from "../tui/screens/featureswitch.ts"; | |
| 52 | import * as s48 from "../tui/screens/feed.ts"; | |
| 53 | import * as s49 from "../tui/screens/flowsall.ts"; | |
| 54 | import * as s50 from "../tui/screens/footing.ts"; | |
| 55 | import * as s51 from "../tui/screens/forgeready.ts"; | |
| 56 | import * as s52 from "../tui/screens/forges.ts"; | |
| 57 | import * as s53 from "../tui/screens/forgeview.ts"; | |
| 58 | import * as s54 from "../tui/screens/front.ts"; | |
| 59 | import * as s55 from "../tui/screens/frontms.ts"; | |
| 60 | import * as s56 from "../tui/screens/funnel.ts"; | |
| 61 | import * as s57 from "../tui/screens/games.ts"; | |
| 62 | import * as s58 from "../tui/screens/gifts.ts"; | |
| 63 | import * as s59 from "../tui/screens/health_voice.ts"; | |
| 64 | import * as s60 from "../tui/screens/home_pins.ts"; | |
| 65 | import * as s61 from "../tui/screens/hostreach.ts"; | |
| 66 | import * as s62 from "../tui/screens/infra.ts"; | |
| 67 | import * as s63 from "../tui/screens/inventory.ts"; | |
| 68 | import * as s64 from "../tui/screens/invites.ts"; | |
| 69 | import * as s65 from "../tui/screens/knobs.ts"; | |
| 70 | import * as s66 from "../tui/screens/lapsed.ts"; | |
| 71 | import * as s67 from "../tui/screens/limits.ts"; | |
| 72 | import * as s68 from "../tui/screens/loopstalls.ts"; | |
| 73 | import * as s69 from "../tui/screens/madefrom.ts"; | |
| 74 | import * as s70 from "../tui/screens/maintainruns.ts"; | |
| 75 | import * as s71 from "../tui/screens/maintenance.ts"; | |
| 76 | import * as s72 from "../tui/screens/maintenanceweek.ts"; | |
| 77 | import * as s73 from "../tui/screens/mergedbranches.ts"; | |
| 78 | import * as s74 from "../tui/screens/mirrorpacks.ts"; | |
| 79 | import * as s75 from "../tui/screens/model.ts"; | |
| 80 | import * as s76 from "../tui/screens/money.ts"; | |
| 81 | import * as s77 from "../tui/screens/newissue.ts"; | |
| 82 | import * as s78 from "../tui/screens/next.ts"; | |
| 83 | import * as s79 from "../tui/screens/nowlist.ts"; | |
| 84 | import * as s80 from "../tui/screens/offers.ts"; | |
| 85 | import * as s81 from "../tui/screens/offsite.ts"; | |
| 86 | import * as s82 from "../tui/screens/orggrants.ts"; | |
| 87 | import * as s83 from "../tui/screens/orgteams.ts"; | |
| 88 | import * as s84 from "../tui/screens/originahead.ts"; | |
| 89 | import * as s85 from "../tui/screens/owner.ts"; | |
| 90 | import * as s86 from "../tui/screens/packrepos.ts"; | |
| 91 | import * as s87 from "../tui/screens/payments.ts"; | |
| 92 | import * as s88 from "../tui/screens/people.ts"; | |
| 93 | import * as s89 from "../tui/screens/pinned.ts"; | |
| 94 | import * as s90 from "../tui/screens/planner.ts"; | |
| 95 | import * as s91 from "../tui/screens/planrow.ts"; | |
| 96 | import * as s92 from "../tui/screens/plans.ts"; | |
| 97 | import * as s93 from "../tui/screens/previewdirs.ts"; | |
| 98 | import * as s94 from "../tui/screens/profile.ts"; | |
| 99 | import * as s95 from "../tui/screens/quotas.ts"; | |
| 100 | import * as s96 from "../tui/screens/recoverykit.ts"; | |
| 101 | import * as s97 from "../tui/screens/refused.ts"; | |
| 102 | import * as s98 from "../tui/screens/repolist.ts"; | |
| 103 | import * as s99 from "../tui/screens/requests.ts"; | |
| 104 | import * as s100 from "../tui/screens/resilience.ts"; | |
| 105 | import * as s101 from "../tui/screens/returning.ts"; | |
| 106 | import * as s102 from "../tui/screens/returns.ts"; | |
| 107 | import * as s103 from "../tui/screens/scorecard.ts"; | |
| 108 | import * as s104 from "../tui/screens/scrapes.ts"; | |
| 109 | import * as s105 from "../tui/screens/secrets.ts"; | |
| 110 | import * as s106 from "../tui/screens/sentinel.ts"; | |
| 111 | import * as s107 from "../tui/screens/ship_time.ts"; | |
| 112 | import * as s108 from "../tui/screens/slowpages.ts"; | |
| 113 | import * as s109 from "../tui/screens/sold.ts"; | |
| 114 | import * as s110 from "../tui/screens/spend.ts"; | |
| 115 | import * as s111 from "../tui/screens/stalepulls.ts"; | |
| 116 | import * as s112 from "../tui/screens/subscriptions.ts"; | |
| 117 | import * as s113 from "../tui/screens/system.ts"; | |
| 118 | import * as s114 from "../tui/screens/teams.ts"; | |
| 119 | import * as s115 from "../tui/screens/telemetry.ts"; | |
| 120 | import * as s116 from "../tui/screens/tell_undo.ts"; | |
| 121 | import * as s117 from "../tui/screens/traffic.ts"; | |
| 122 | import * as s118 from "../tui/screens/trafficview.ts"; | |
| 123 | import * as s119 from "../tui/screens/valves.ts"; | |
| 124 | import * as s120 from "../tui/screens/waiting.ts"; | |
| 125 | import * as s121 from "../tui/screens/waiting_for_me.ts"; | |
| 126 | import * as s122 from "../tui/screens/wayback.ts"; | |
| 127 | import * as s123 from "../tui/screens/weekcard.ts"; | |
| 19 | import * as s15 from "../tui/screens/buckets.ts"; | |
| 20 | import * as s16 from "../tui/screens/bugs.ts"; | |
| 21 | import * as s17 from "../tui/screens/bulk.ts"; | |
| 22 | import * as s18 from "../tui/screens/captain.ts"; | |
| 23 | import * as s19 from "../tui/screens/childcredit.ts"; | |
| 24 | import * as s20 from "../tui/screens/claim_link.ts"; | |
| 25 | import * as s21 from "../tui/screens/claim_start.ts"; | |
| 26 | import * as s22 from "../tui/screens/cloud.ts"; | |
| 27 | import * as s23 from "../tui/screens/codes.ts"; | |
| 28 | import * as s24 from "../tui/screens/connections.ts"; | |
| 29 | import * as s25 from "../tui/screens/controls.ts"; | |
| 30 | import * as s26 from "../tui/screens/cores.ts"; | |
| 31 | import * as s27 from "../tui/screens/crewbudgets.ts"; | |
| 32 | import * as s28 from "../tui/screens/crewhandover.ts"; | |
| 33 | import * as s29 from "../tui/screens/crewkey.ts"; | |
| 34 | import * as s30 from "../tui/screens/crewsplits.ts"; | |
| 35 | import * as s31 from "../tui/screens/crewtop.ts"; | |
| 36 | import * as s32 from "../tui/screens/dataset.ts"; | |
| 37 | import * as s33 from "../tui/screens/design.ts"; | |
| 38 | import * as s34 from "../tui/screens/design_gallery.ts"; | |
| 39 | import * as s35 from "../tui/screens/devices.ts"; | |
| 40 | import * as s36 from "../tui/screens/disks.ts"; | |
| 41 | import * as s37 from "../tui/screens/displays.ts"; | |
| 42 | import * as s38 from "../tui/screens/domains.ts"; | |
| 43 | import * as s39 from "../tui/screens/dotfiles.ts"; | |
| 44 | import * as s40 from "../tui/screens/drift.ts"; | |
| 45 | import * as s41 from "../tui/screens/dutybudgets.ts"; | |
| 46 | import * as s42 from "../tui/screens/dutyswitch.ts"; | |
| 47 | import * as s43 from "../tui/screens/economics.ts"; | |
| 48 | import * as s44 from "../tui/screens/edgeonly.ts"; | |
| 49 | import * as s45 from "../tui/screens/experiments.ts"; | |
| 50 | import * as s46 from "../tui/screens/featurefeed.ts"; | |
| 51 | import * as s47 from "../tui/screens/features.ts"; | |
| 52 | import * as s48 from "../tui/screens/featureswitch.ts"; | |
| 53 | import * as s49 from "../tui/screens/feed.ts"; | |
| 54 | import * as s50 from "../tui/screens/flowsall.ts"; | |
| 55 | import * as s51 from "../tui/screens/footing.ts"; | |
| 56 | import * as s52 from "../tui/screens/forgeready.ts"; | |
| 57 | import * as s53 from "../tui/screens/forges.ts"; | |
| 58 | import * as s54 from "../tui/screens/forgeview.ts"; | |
| 59 | import * as s55 from "../tui/screens/front.ts"; | |
| 60 | import * as s56 from "../tui/screens/frontms.ts"; | |
| 61 | import * as s57 from "../tui/screens/funnel.ts"; | |
| 62 | import * as s58 from "../tui/screens/games.ts"; | |
| 63 | import * as s59 from "../tui/screens/gifts.ts"; | |
| 64 | import * as s60 from "../tui/screens/health_voice.ts"; | |
| 65 | import * as s61 from "../tui/screens/home_pins.ts"; | |
| 66 | import * as s62 from "../tui/screens/hostreach.ts"; | |
| 67 | import * as s63 from "../tui/screens/infra.ts"; | |
| 68 | import * as s64 from "../tui/screens/inventory.ts"; | |
| 69 | import * as s65 from "../tui/screens/invites.ts"; | |
| 70 | import * as s66 from "../tui/screens/knobs.ts"; | |
| 71 | import * as s67 from "../tui/screens/lapsed.ts"; | |
| 72 | import * as s68 from "../tui/screens/limits.ts"; | |
| 73 | import * as s69 from "../tui/screens/loopstalls.ts"; | |
| 74 | import * as s70 from "../tui/screens/madefrom.ts"; | |
| 75 | import * as s71 from "../tui/screens/maintainruns.ts"; | |
| 76 | import * as s72 from "../tui/screens/maintenance.ts"; | |
| 77 | import * as s73 from "../tui/screens/maintenanceweek.ts"; | |
| 78 | import * as s74 from "../tui/screens/mergedbranches.ts"; | |
| 79 | import * as s75 from "../tui/screens/mirrorpacks.ts"; | |
| 80 | import * as s76 from "../tui/screens/model.ts"; | |
| 81 | import * as s77 from "../tui/screens/money.ts"; | |
| 82 | import * as s78 from "../tui/screens/newissue.ts"; | |
| 83 | import * as s79 from "../tui/screens/next.ts"; | |
| 84 | import * as s80 from "../tui/screens/nowlist.ts"; | |
| 85 | import * as s81 from "../tui/screens/offers.ts"; | |
| 86 | import * as s82 from "../tui/screens/offsite.ts"; | |
| 87 | import * as s83 from "../tui/screens/orggrants.ts"; | |
| 88 | import * as s84 from "../tui/screens/orgteams.ts"; | |
| 89 | import * as s85 from "../tui/screens/originahead.ts"; | |
| 90 | import * as s86 from "../tui/screens/owner.ts"; | |
| 91 | import * as s87 from "../tui/screens/packrepos.ts"; | |
| 92 | import * as s88 from "../tui/screens/payments.ts"; | |
| 93 | import * as s89 from "../tui/screens/people.ts"; | |
| 94 | import * as s90 from "../tui/screens/pinned.ts"; | |
| 95 | import * as s91 from "../tui/screens/planner.ts"; | |
| 96 | import * as s92 from "../tui/screens/planrow.ts"; | |
| 97 | import * as s93 from "../tui/screens/plans.ts"; | |
| 98 | import * as s94 from "../tui/screens/previewdirs.ts"; | |
| 99 | import * as s95 from "../tui/screens/profile.ts"; | |
| 100 | import * as s96 from "../tui/screens/quotas.ts"; | |
| 101 | import * as s97 from "../tui/screens/recoverykit.ts"; | |
| 102 | import * as s98 from "../tui/screens/refused.ts"; | |
| 103 | import * as s99 from "../tui/screens/repolist.ts"; | |
| 104 | import * as s100 from "../tui/screens/requests.ts"; | |
| 105 | import * as s101 from "../tui/screens/resilience.ts"; | |
| 106 | import * as s102 from "../tui/screens/returning.ts"; | |
| 107 | import * as s103 from "../tui/screens/returns.ts"; | |
| 108 | import * as s104 from "../tui/screens/scorecard.ts"; | |
| 109 | import * as s105 from "../tui/screens/scrapes.ts"; | |
| 110 | import * as s106 from "../tui/screens/secrets.ts"; | |
| 111 | import * as s107 from "../tui/screens/sentinel.ts"; | |
| 112 | import * as s108 from "../tui/screens/ship_time.ts"; | |
| 113 | import * as s109 from "../tui/screens/slowpages.ts"; | |
| 114 | import * as s110 from "../tui/screens/sold.ts"; | |
| 115 | import * as s111 from "../tui/screens/spend.ts"; | |
| 116 | import * as s112 from "../tui/screens/stalepulls.ts"; | |
| 117 | import * as s113 from "../tui/screens/subscriptions.ts"; | |
| 118 | import * as s114 from "../tui/screens/system.ts"; | |
| 119 | import * as s115 from "../tui/screens/teams.ts"; | |
| 120 | import * as s116 from "../tui/screens/telemetry.ts"; | |
| 121 | import * as s117 from "../tui/screens/tell_undo.ts"; | |
| 122 | import * as s118 from "../tui/screens/traffic.ts"; | |
| 123 | import * as s119 from "../tui/screens/trafficview.ts"; | |
| 124 | import * as s120 from "../tui/screens/valves.ts"; | |
| 125 | import * as s121 from "../tui/screens/waiting.ts"; | |
| 126 | import * as s122 from "../tui/screens/waiting_for_me.ts"; | |
| 127 | import * as s123 from "../tui/screens/wayback.ts"; | |
| 128 | import * as s124 from "../tui/screens/weekcard.ts"; | |
| 128 | 129 | |
| 129 | 130 | /** Each screen file that declares palette entries: its module and the export names. */ |
| 130 | 131 | export const SCREEN_MODULES: readonly (readonly [Record<string, unknown>, readonly string[]])[] = [ |
| @@ -147,8 +148,8 @@ export const SCREEN_MODULES: readonly (readonly [Record<string, unknown>, readon | ||
| 147 | 148 | [s16, ["entry"]], |
| 148 | 149 | [s17, ["entry"]], |
| 149 | 150 | [s18, ["entry"]], |
| 150 | [s19, ["entry","recoveryEntry"]], | |
| 151 | [s20, ["entry"]], | |
| 151 | [s19, ["entry"]], | |
| 152 | [s20, ["entry","recoveryEntry"]], | |
| 152 | 153 | [s21, ["entry"]], |
| 153 | 154 | [s22, ["entry"]], |
| 154 | 155 | [s23, ["entry"]], |
| @@ -203,11 +204,11 @@ export const SCREEN_MODULES: readonly (readonly [Record<string, unknown>, readon | ||
| 203 | 204 | [s72, ["entry"]], |
| 204 | 205 | [s73, ["entry"]], |
| 205 | 206 | [s74, ["entry"]], |
| 206 | [s75, ["modelGrantEntry","modelCeilingEntry"]], | |
| 207 | [s76, ["entry"]], | |
| 207 | [s75, ["entry"]], | |
| 208 | [s76, ["modelGrantEntry","modelCeilingEntry"]], | |
| 208 | 209 | [s77, ["entry"]], |
| 209 | [s78, ["nextAllEntry"]], | |
| 210 | [s79, ["entry"]], | |
| 210 | [s78, ["entry"]], | |
| 211 | [s79, ["nextAllEntry"]], | |
| 211 | 212 | [s80, ["entry"]], |
| 212 | 213 | [s81, ["entry"]], |
| 213 | 214 | [s82, ["entry"]], |
| @@ -252,4 +253,5 @@ export const SCREEN_MODULES: readonly (readonly [Record<string, unknown>, readon | ||
| 252 | 253 | [s121, ["entry"]], |
| 253 | 254 | [s122, ["entry"]], |
| 254 | 255 | [s123, ["entry"]], |
| 256 | [s124, ["entry"]], | |
| 255 | 257 | ]; |
src/http/mcp/tools/infra.ts +16 −4
| @@ -1,4 +1,5 @@ | ||
| 1 | 1 | import { buildOf, gameWant, goneWords, infraOf, infraSays, machineOf, nameFor, planById, planCompose, planWants, startBuild } from "../../../actions/infra.ts"; |
| 2 | import { bindBucket, readBucket } from "../../../actions/bucket.ts"; | |
| 2 | 3 | import { endsWords, keepPinned } from "../../../actions/pinned.ts"; |
| 3 | 4 | import { EXPERIMENT_WORDS, attachMachine, beginExperiment, findExperiment, recordResult } from "../../../cloud/experiments.ts"; |
| 4 | 5 | import { good, bad, type Tool, str } from "../shared.ts"; |
| @@ -14,12 +15,12 @@ const s = (v: unknown, max: number): string => str(v).slice(0, max); | ||
| 14 | 15 | export const tool: Tool = { |
| 15 | 16 | name: "infra", |
| 16 | 17 | listedTo: "admin", |
| 17 | doc: "`{ action, id?, want?, question?, names?, result? }`: `list` (its `says` names every machine), `show` an `id` (a plan, an experiment or a machine's name), `plan` a `want` `{app, where, question?, experiment?, days?}` or a game box `{game: true, where, name?, repo?, host?}` or dead `names`, `apply` its `id`, `keep`, `begin`, `attach`, `record_result`.", | |
| 18 | description: "Cloud machines and spend.", | |
| 18 | doc: "`{ action, id?, want?, question?, names?, result?, bucket? }`: `list`, `show` an `id`, `plan` a `want` `{app, where, question?, experiment?, days?}` or a game box `{game: true, where, name?, repo?, host?}` or dead `names`, `apply` its `id`, `keep`, `begin`, `attach`, `record_result`; `price`/`bind` a `bucket` `{region, name, repo?, stored_gib?, out_gib?}`.", | |
| 19 | description: "Machines and spend.", | |
| 19 | 20 | inputSchema: { |
| 20 | 21 | type: "object", |
| 21 | 22 | properties: { |
| 22 | action: { type: "string", enum: ["list", "show", "plan", "apply", "keep", "begin", "attach", "record_result"] }, | |
| 23 | action: { type: "string", enum: ["list", "show", "plan", "apply", "keep", "begin", "attach", "record_result", "price", "bind"] }, | |
| 23 | 24 | id: { type: "string" }, |
| 24 | 25 | want: { type: "object" }, |
| 25 | 26 | question: { type: "string" }, |
| @@ -102,6 +103,17 @@ export const tool: Tool = { | ||
| 102 | 103 | etaSeconds: p.plan.etaSeconds, |
| 103 | 104 | }); |
| 104 | 105 | } |
| 106 | if (action === "price" || action === "bind") { | |
| 107 | const b = (typeof args["bucket"] === "object" && args["bucket"] !== null ? args["bucket"] : {}) as Record<string, unknown>; | |
| 108 | // Absent is 0; a present value must be a whole non-negative number, never defaulted. | |
| 109 | const gib = (v: unknown): number => (v === undefined ? 0 : typeof v === "number" ? v : Number.NaN); | |
| 110 | const provider = s(b["provider"], 40) || "digitalocean"; | |
| 111 | const done = | |
| 112 | action === "price" | |
| 113 | ? readBucket(provider, s(b["region"], 20), s(b["name"], 80), gib(b["stored_gib"]), gib(b["out_gib"])) | |
| 114 | : bindBucket(ctx.app, user, s(b["repo"], 140), provider, s(b["region"], 20), s(b["name"], 80)); | |
| 115 | return done.ok ? good({ says: done.value.says, url: done.value.url, cents: done.value.cents }) : bad("refused", done.error); | |
| 116 | } | |
| 105 | 117 | if (action === "apply") { |
| 106 | 118 | const p = planById(ctx.app, id); |
| 107 | 119 | if (p === undefined) return bad("no_plan", "That plan is not one this forge showed lately: plan it again, then apply its id."); |
| @@ -135,7 +147,7 @@ export const tool: Tool = { | ||
| 135 | 147 | } |
| 136 | 148 | return good({ says: `Attached ${names.join(", ")}.`, experiment: findExperiment(db, id) }); |
| 137 | 149 | } |
| 138 | if (action !== "record_result") return bad("bad_action", "action is list, show, plan, apply, keep, begin, attach or record_result"); | |
| 150 | if (action !== "record_result") return bad("bad_action", "action is list, show, plan, apply, keep, begin, attach, record_result, price or bind"); | |
| 139 | 151 | const done = recordResult(ctx.app, user.id, id, s(args["result"], 500)); |
| 140 | 152 | if (!done.ok) return bad(done.error, EXPERIMENT_WORDS[done.error]); |
| 141 | 153 | return good({ says: "Result recorded: the experiment is done.", experiment: findExperiment(db, id) }); |
src/tui/screens/buckets.ts added +115
| @@ -0,0 +1,115 @@ | ||
| 1 | /** | |
| 2 | * Buckets, in the terminal (#17073): what an object bucket costs a month and binding its address to | |
| 3 | * an app, from the same actions the infra MCP tool uses (`actions/bucket.ts`). `p` reads a price | |
| 4 | * (region, name, GiB stored, GiB sent out); `b` binds (app, region, name), a forge admin's only, | |
| 5 | * through `bindBucket`, which holds the gate `putSecret` holds. | |
| 6 | */ | |
| 7 | ||
| 8 | import { bindBucket, readBucket } from "../../actions/bucket.ts"; | |
| 9 | import { BUCKETS } from "../../cloud/bucket.ts"; | |
| 10 | import type { Ctx, Screen } from "../app/app.ts"; | |
| 11 | import { binding } from "../app/keys.ts"; | |
| 12 | import type { Session } from "../session.ts"; | |
| 13 | import { wrap } from "../term/width.ts"; | |
| 14 | import { dialog } from "../ui/widgets.ts"; | |
| 15 | import { later, promptScreen } from "./common.ts"; | |
| 16 | import type { PaletteEntry } from "./entries.ts"; | |
| 17 | ||
| 18 | const WIDTH = 78; | |
| 19 | const INNER = WIDTH - 6; | |
| 20 | const PROVIDER = "digitalocean"; | |
| 21 | ||
| 22 | /** The screen's lines: the provider, its regions and what its base buys. */ | |
| 23 | export function bucketLines(): string[] { | |
| 24 | const k = BUCKETS[PROVIDER]!; | |
| 25 | const regions = Object.entries(k.regions).map(([id, city]) => `${id} ${city}`).join(", "); | |
| 26 | return [...wrap(`${k.label}: regions ${regions}.`, INNER), ...wrap(`The base includes ${k.storageGiB} GiB stored and ${k.transferGiB} GiB sent out a month.`, INNER)]; | |
| 27 | } | |
| 28 | ||
| 29 | /** A GiB answer: empty is 0; anything but whole digits is NaN, which the price refuses. */ | |
| 30 | export const gibOf = (text: string): number => (text.trim() === "" ? 0 : /^\d{1,10}$/.test(text.trim()) ? Number(text.trim()) : Number.NaN); | |
| 31 | ||
| 32 | export function bucketsScreen(session: Session, ctx: Ctx): Screen { | |
| 33 | const may = (): boolean => session.user?.isAdmin === true; | |
| 34 | let note = ""; | |
| 35 | const say = (t: string): void => { | |
| 36 | note = t; | |
| 37 | ctx.invalidate(); | |
| 38 | }; | |
| 39 | const ask = (title: string, label: string, onSubmit: Parameters<typeof promptScreen>[2]["onSubmit"]): Screen => promptScreen(ctx, screen, { title, label, onSubmit }); | |
| 40 | ||
| 41 | const price = (): void => | |
| 42 | ctx.push( | |
| 43 | ask("Price 1/4", "Region (e.g. nyc3)", (region) => | |
| 44 | later(ctx, () => | |
| 45 | ask("Price 2/4", "Bucket name", (name) => | |
| 46 | later(ctx, () => | |
| 47 | ask("Price 3/4", "GiB stored (empty is 0)", (stored) => | |
| 48 | later(ctx, () => | |
| 49 | ask("Price 4/4", "GiB sent out (empty is 0)", (out) => { | |
| 50 | const r = readBucket(PROVIDER, region.trim(), name.trim(), gibOf(stored), gibOf(out)); | |
| 51 | if (!r.ok) return r.error; | |
| 52 | say(r.value.says); | |
| 53 | return undefined; | |
| 54 | }), | |
| 55 | ), | |
| 56 | ), | |
| 57 | ), | |
| 58 | ), | |
| 59 | ), | |
| 60 | ), | |
| 61 | ); | |
| 62 | ||
| 63 | const bind = (): void => | |
| 64 | ctx.push( | |
| 65 | ask("Bind 1/3", "App (owner/name)", (repo) => | |
| 66 | later(ctx, () => | |
| 67 | ask("Bind 2/3", "Region (e.g. nyc3)", (region) => | |
| 68 | later(ctx, () => | |
| 69 | ask("Bind 3/3", "Bucket name", (name) => { | |
| 70 | const r = bindBucket(session.app, session.user, repo.trim(), PROVIDER, region.trim(), name.trim()); | |
| 71 | if (!r.ok) return r.error; | |
| 72 | say(r.value.says); | |
| 73 | return undefined; | |
| 74 | }), | |
| 75 | ), | |
| 76 | ), | |
| 77 | ), | |
| 78 | ), | |
| 79 | ); | |
| 80 | ||
| 81 | const screen: Screen = { | |
| 82 | title: "Buckets", | |
| 83 | get keys() { | |
| 84 | return [binding(["p"], "read a price"), ...(may() ? [binding(["b"], "bind to an app")] : []), binding(["enter", "esc", "q"], "close")]; | |
| 85 | }, | |
| 86 | render(area, s) { | |
| 87 | const buttons = dialog(s, area, ctx.theme.c, { | |
| 88 | title: "Buckets", | |
| 89 | body: [...(note === "" ? [] : [...wrap(note, INNER), ""]), ...bucketLines(), "", may() ? "p reads a price, b binds a bucket to an app." : "p reads a price; binding is a forge admin's."], | |
| 90 | actions: [{ label: "Close" }], | |
| 91 | selected: 0, | |
| 92 | width: WIDTH, | |
| 93 | }); | |
| 94 | buttons.forEach((rect) => ctx.hit(rect, () => ctx.pop())); | |
| 95 | }, | |
| 96 | key(e) { | |
| 97 | if (e.ctrl || e.alt) return true; | |
| 98 | if (e.key === "enter" || e.key === "esc" || e.key === "q") ctx.pop(); | |
| 99 | else if (e.key === "p") price(); | |
| 100 | else if (e.key === "b" && may()) bind(); | |
| 101 | return true; | |
| 102 | }, | |
| 103 | }; | |
| 104 | return screen; | |
| 105 | } | |
| 106 | ||
| 107 | /** Offered to everyone signed in: the price is anyone's, the bind is the admin's. */ | |
| 108 | export const entry: PaletteEntry = { | |
| 109 | rank: 245, | |
| 110 | id: "buckets", | |
| 111 | title: "Buckets", | |
| 112 | hint: "an object bucket's price a month, and binding it to an app", | |
| 113 | offered: (session) => session.user !== undefined, | |
| 114 | run: (session, ctx) => ctx.push(bucketsScreen(session, ctx)), | |
| 115 | }; |
test/infra-bucket.test.ts added +113
| @@ -0,0 +1,113 @@ | ||
| 1 | /** | |
| 2 | * Buckets (#17073): the price read, the name checks, and binding a bucket's address to an app as a | |
| 3 | * secret. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp, type App } from "../src/app.ts"; | |
| 10 | import { BUCKET_SECRET, bindBucket, readBucket } from "../src/actions/bucket.ts"; | |
| 11 | import { BUCKETS, bucketCents, bucketOf, bucketUrl } from "../src/cloud/bucket.ts"; | |
| 12 | import { createRepo } from "../src/core/repos.ts"; | |
| 13 | import { createUser, type User } from "../src/core/users.ts"; | |
| 14 | import type { Ctx } from "../src/http/context.ts"; | |
| 15 | import { tool } from "../src/http/mcp/tools/infra.ts"; | |
| 16 | ||
| 17 | const spaces = BUCKETS["digitalocean"]!; | |
| 18 | ||
| 19 | describe("bucket prices and names", () => { | |
| 20 | test("the base covers its allowance and each GiB over costs its rate", () => { | |
| 21 | expect(bucketCents(spaces)).toEqual({ ok: true, value: 500 }); | |
| 22 | expect(bucketCents(spaces, 250, 1024)).toEqual({ ok: true, value: 500 }); | |
| 23 | expect(bucketCents(spaces, 350, 1124)).toEqual({ ok: true, value: 500 + 100 * 2 + 100 * 1 }); | |
| 24 | }); | |
| 25 | test("an amount that is not whole GiB is refused", () => { | |
| 26 | expect(bucketCents(spaces, -1).ok).toBe(false); | |
| 27 | expect(bucketCents(spaces, 1.5).ok).toBe(false); | |
| 28 | expect(bucketCents(spaces, 0, Number.NaN).ok).toBe(false); | |
| 29 | }); | |
| 30 | test("names, regions and providers are checked", () => { | |
| 31 | expect(bucketOf("digitalocean", "nyc3", "my-files").ok).toBe(true); | |
| 32 | for (const bad of ["ab", "-x-", "Up", "a_b", "a..b", "a--b", "x".repeat(64), "a/b", "a.b.c"]) expect(bucketOf("digitalocean", "nyc3", bad).ok).toBe(false); | |
| 33 | expect(bucketOf("digitalocean", "mars1", "my-files").ok).toBe(false); | |
| 34 | expect(bucketOf("b2", "nyc3", "my-files").ok).toBe(false); | |
| 35 | expect(bucketOf("__proto__", "nyc3", "my-files").ok).toBe(false); | |
| 36 | }); | |
| 37 | test("the address has the bucket, the region and no path", () => { | |
| 38 | expect(bucketUrl(spaces, "nyc3", "my-files")).toBe("https://my-files.nyc3.digitaloceanspaces.com"); | |
| 39 | }); | |
| 40 | test("the read says the price in dollars and where it answers", () => { | |
| 41 | const r = readBucket("digitalocean", "nyc3", "my-files", 350, 0); | |
| 42 | expect(r.ok && r.value.cents).toBe(700); | |
| 43 | expect(r.ok && r.value.says).toContain("$7.00 a month"); | |
| 44 | expect(r.ok && r.value.says).toContain("https://my-files.nyc3.digitaloceanspaces.com"); | |
| 45 | }); | |
| 46 | }); | |
| 47 | ||
| 48 | describe("binding to an app", () => { | |
| 49 | let dir: string; | |
| 50 | let app: App; | |
| 51 | let admin: User; | |
| 52 | let other: User; | |
| 53 | beforeAll(async () => { | |
| 54 | dir = await mkdtemp(join(tmpdir(), "forge-bucket-")); | |
| 55 | app = createApp({ dataDir: dir, port: 0, url: "https://forge.example.net" }); | |
| 56 | const mk = async (username: string, isAdmin: boolean): Promise<User> => { | |
| 57 | const u = await createUser(app.db, { username, email: `${username}@f.test`, password: "a-good-passphrase", origin: "seed", isAdmin, trusted: true }); | |
| 58 | if (!u.ok) throw new Error(u.error); | |
| 59 | return u.value; | |
| 60 | }; | |
| 61 | admin = await mk("ada", true); | |
| 62 | other = await mk("bob", false); | |
| 63 | const r = await createRepo(app.db, app.reposRoot, { ownerId: admin.id, name: "files", visibility: "public" }); | |
| 64 | if (!r.ok) throw new Error(String(r.error)); | |
| 65 | }); | |
| 66 | afterAll(async () => { | |
| 67 | app.db.close(); | |
| 68 | await rm(dir, { recursive: true, force: true }); | |
| 69 | }); | |
| 70 | ||
| 71 | test("an admin binds: the app's secret holds the address", () => { | |
| 72 | const b = bindBucket(app, admin, "ada/files", "digitalocean", "nyc3", "my-files"); | |
| 73 | expect(b.ok).toBe(true); | |
| 74 | const repoId = (app.db.query("SELECT id FROM repo WHERE name = 'files'").get() as { id: string }).id; | |
| 75 | const row = app.db.query("SELECT scope, scope_id FROM secret WHERE name = ?").get(BUCKET_SECRET) as { scope: string; scope_id: string }; | |
| 76 | expect(row).toEqual({ scope: "repo", scope_id: repoId }); | |
| 77 | }); | |
| 78 | test("a non-admin, an unknown app and a bad bucket are refused", () => { | |
| 79 | expect(bindBucket(app, other, "ada/files", "digitalocean", "nyc3", "my-files").ok).toBe(false); | |
| 80 | expect(bindBucket(app, admin, "ada/nope", "digitalocean", "nyc3", "my-files").ok).toBe(false); | |
| 81 | expect(bindBucket(app, admin, "ada/files", "digitalocean", "nyc3", "A B").ok).toBe(false); | |
| 82 | }); | |
| 83 | test("the infra tool reads a price and binds", async () => { | |
| 84 | const url = new URL("http://forge.test/mcp"); | |
| 85 | const ctx: Ctx = { app, request: new Request(url.href), url, params: {}, auth: { user: admin, scope: "admin", via: "token" }, theme: undefined }; | |
| 86 | const p = (await tool.run(ctx, admin, { action: "price", bucket: { region: "ams3", name: "pics", stored_gib: 300 } })) as any; | |
| 87 | expect(p.ok).toBe(true); | |
| 88 | expect(p.value.cents).toBe(600); | |
| 89 | const b = (await tool.run(ctx, admin, { action: "bind", bucket: { repo: "ada/files", region: "ams3", name: "pics" } })) as any; | |
| 90 | expect(b.ok).toBe(true); | |
| 91 | expect(b.value.url).toBe("https://pics.ams3.digitaloceanspaces.com"); | |
| 92 | const bad = (await tool.run(ctx, admin, { action: "price", bucket: { region: "ams3", name: "x" } })) as any; | |
| 93 | expect(bad.ok).toBe(false); | |
| 94 | }); | |
| 95 | test("a value that is not a whole non-negative number is refused, not priced as 0", async () => { | |
| 96 | const url = new URL("http://forge.test/mcp"); | |
| 97 | const ctx: Ctx = { app, request: new Request(url.href), url, params: {}, auth: { user: admin, scope: "admin", via: "token" }, theme: undefined }; | |
| 98 | for (const v of ["350", null, 1.5, -1]) { | |
| 99 | const r = (await tool.run(ctx, admin, { action: "price", bucket: { region: "ams3", name: "pics", stored_gib: v } })) as any; | |
| 100 | expect(r.ok).toBe(false); | |
| 101 | } | |
| 102 | const out = (await tool.run(ctx, admin, { action: "price", bucket: { region: "ams3", name: "pics", out_gib: "9" } })) as any; | |
| 103 | expect(out.ok).toBe(false); | |
| 104 | }); | |
| 105 | test("a non-admin calling bind through the tool is refused and sets nothing", async () => { | |
| 106 | const url = new URL("http://forge.test/mcp"); | |
| 107 | const ctx: Ctx = { app, request: new Request(url.href), url, params: {}, auth: { user: other, scope: "admin", via: "token" }, theme: undefined }; | |
| 108 | const before = (app.db.query("SELECT COUNT(*) AS n FROM secret").get() as { n: number }).n; | |
| 109 | const r = (await tool.run(ctx, other, { action: "bind", bucket: { repo: "ada/files", region: "ams3", name: "pics" } })) as any; | |
| 110 | expect(r.ok).toBe(false); | |
| 111 | expect((app.db.query("SELECT COUNT(*) AS n FROM secret").get() as { n: number }).n).toBe(before); | |
| 112 | }); | |
| 113 | }); |
test/palette-roster.test.ts +3
| @@ -212,6 +212,7 @@ test("a site-admin person is offered the whole roster", () => { | ||
| 212 | 212 | ["spend", "Spend", "what the crew's runs spent, by credential, app, agent, issue and kind"], |
| 213 | 213 | ["payments", "Payments", "what was charged for the forges you claimed"], |
| 214 | 214 | ["ship-time", "Ship time", "median time from the crew's first run to done"], |
| 215 | ["buckets", "Buckets", "an object bucket's price a month, and binding it to an app"], | |
| 215 | 216 | ["edge-only-accounts", "Edge-only accounts", "who would be locked out if the platform edge sign-in went off"], |
| 216 | 217 | ["way-back-links", "Way-back links", "a link that signs in an account the edge made, once the edge is gone"], |
| 217 | 218 | ["traffic-sources", "Traffic sources", "where people come from and where they go, for the forge"], |
| @@ -335,6 +336,7 @@ test("a site-admin agent is offered the roster without the person-only entries", | ||
| 335 | 336 | ["health-voice", "Health — spoken", "how the forge is, in two plain sentences"], |
| 336 | 337 | ["design", "Design: the forge's theme", "colours and scales, the AA verdict; publish or revert"], |
| 337 | 338 | ["ship-time", "Ship time", "median time from the crew's first run to done"], |
| 339 | ["buckets", "Buckets", "an object bucket's price a month, and binding it to an app"], | |
| 338 | 340 | ["maintenance-runs", "Maintenance duty runs", "what each duty found, did and probed"], |
| 339 | 341 | ["traffic-sources", "Traffic sources", "where people come from and where they go, for the forge"], |
| 340 | 342 | ["audit", "Who can do what", "each app's doors from its contract.json: yes, own or no"], |
| @@ -456,6 +458,7 @@ test("a member person is offered the signed-in roster without the admin entries" | ||
| 456 | 458 | ["next-all", "Next up, every repository", "the top of each list you administer"], |
| 457 | 459 | ["offers-upstream", "Offers upstream", "the receipts of what you offered from your forks"], |
| 458 | 460 | ["tell-undo", "Take a tell back", "a tell you sent in the last minute, while nobody has read it"], |
| 461 | ["buckets", "Buckets", "an object bucket's price a month, and binding it to an app"], | |
| 459 | 462 | ["audit", "Who can do what", "each app's doors from its contract.json: yes, own or no"], |
| 460 | 463 | ["archived", "Archived repositories", "put away, not gone: open one or restore it"], |
| 461 | 464 | ["apps-crew", "What the crew is doing", "one plain sentence per app: who is on what"], |
test/tui-buckets.test.ts added +82
| @@ -0,0 +1,82 @@ | ||
| 1 | /** Buckets (#17073) in the terminal: anyone reads a price, only an admin binds. */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { createRepo } from "../src/core/repos.ts"; | |
| 8 | import { createUser, type User } from "../src/core/users.ts"; | |
| 9 | import { createForgeApp } from "../src/tui/forge.ts"; | |
| 10 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; | |
| 11 | import { quietLogs } from "./helpers/globals.ts"; | |
| 12 | import { settleApp } from "./helpers/tui.ts"; | |
| 13 | ||
| 14 | quietLogs(); | |
| 15 | ||
| 16 | let dir: string; | |
| 17 | let forge: App; | |
| 18 | let alice: User; | |
| 19 | let bob: User; | |
| 20 | ||
| 21 | beforeAll(async () => { | |
| 22 | dir = await mkdtemp(join(tmpdir(), "forge-tui-buckets-")); | |
| 23 | forge = createApp({ dataDir: dir, port: 0 }); | |
| 24 | const make = async (username: string, adminIfFirst: boolean): Promise<User> => { | |
| 25 | const made = await createUser(forge.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst }); | |
| 26 | if (!made.ok) throw new Error(made.error); | |
| 27 | return made.value; | |
| 28 | }; | |
| 29 | alice = await make("alice", true); | |
| 30 | bob = await make("bob", false); | |
| 31 | const r = await createRepo(forge.db, forge.reposRoot, { ownerId: alice.id, name: "files", visibility: "public" }); | |
| 32 | if (!r.ok) throw new Error(String(r.error)); | |
| 33 | }); | |
| 34 | ||
| 35 | afterAll(async () => { | |
| 36 | forge?.close(); | |
| 37 | await rm(dir, { recursive: true, force: true }); | |
| 38 | }); | |
| 39 | ||
| 40 | const open = async (user: User) => { | |
| 41 | const terminal = headlessTerminal(160, 40); | |
| 42 | const { app } = createForgeApp(forge, { sshUsername: "t", remoteAddress: "127.0.0.1", user }, { terminal, exitHooks: false }); | |
| 43 | app.start(); | |
| 44 | const key = (k: string) => app.dispatch({ type: "key", key: k, ctrl: k.startsWith("ctrl+"), alt: false, shift: false }); | |
| 45 | const type = async (text: string) => { | |
| 46 | for (const ch of text) key(ch); | |
| 47 | key("enter"); | |
| 48 | await settleApp(app); | |
| 49 | }; | |
| 50 | await settleApp(app); | |
| 51 | app.dispatch({ type: "key", key: "k", ctrl: true, alt: false, shift: false }); | |
| 52 | for (const ch of "Buckets") key(ch); | |
| 53 | key("enter"); | |
| 54 | await settleApp(app); | |
| 55 | return { key, type, text: () => app.screen.toText().replace(/\s+/g, " ") }; | |
| 56 | }; | |
| 57 | const secrets = () => (forge.db.query("SELECT COUNT(*) AS n FROM secret WHERE name = 'BUCKET_URL'").get() as { n: number }).n; | |
| 58 | ||
| 59 | test("anyone reads a price", async () => { | |
| 60 | const t = await open(bob); | |
| 61 | expect(t.text()).toContain("DigitalOcean Spaces"); | |
| 62 | t.key("p"); | |
| 63 | await t.type("nyc3"); | |
| 64 | await t.type("my-files"); | |
| 65 | await t.type("350"); | |
| 66 | await t.type(""); | |
| 67 | expect(t.text()).toContain("$7.00 a month"); | |
| 68 | }); | |
| 69 | ||
| 70 | test("only an admin binds", async () => { | |
| 71 | const b = await open(bob); | |
| 72 | expect(b.text()).toContain("binding is a forge admin's"); | |
| 73 | b.key("b"); | |
| 74 | expect(secrets()).toBe(0); | |
| 75 | const a = await open(alice); | |
| 76 | a.key("b"); | |
| 77 | await a.type("alice/files"); | |
| 78 | await a.type("nyc3"); | |
| 79 | await a.type("my-files"); | |
| 80 | expect(a.text().replace(/ │ │ /g, " ")).toContain("alice/files now reads BUCKET_URL=https://my-files.nyc3.digitaloceanspaces.com"); | |
| 81 | expect(secrets()).toBe(1); | |
| 82 | }); |
new above
offline
The
docline is what pushed the tool list over its ceiling. Shorten this text rather than cutting the other tools' descriptions.