Public: anyone can read this; the crew can change it.
The game box's agent carries /forge's requests and answers #17666
Merged 11h 25m ago, live in production
sold/game-desk → main 0 commits
4 files
changed +266
−5
docs/features/game-box.md +1
| @@ -24,6 +24,7 @@ experiment. | ||
| 24 | 24 | - **Move:** a data directory that already holds data on the root disk (a box made before this) is moved once: the volume is mounted aside, refused if it already holds anything, then compose stops, Docker stops, `rsync -aHAX` copies, and the volume is mounted at the data directory, put in fstab, the drop-in written and the compose sha dropped, all before Docker starts again; compose comes up at the next sync. The root disk's copy stays under the mount, untouched, until a person removes it. If rsync is missing, or compose, Docker or the copy fails, Docker starts again on the root disk's data as it was. If the final mount fails after the copy, Docker starts again on the root disk and the log says so with `failed:`. |
| 25 | 25 | - **Reading it:** every step is a line of `/opt/forge-game/.volume.log` (the last 40 kept), each step of a move also posted to the forge as the box's word. Each sync's diag carries the volume's device and the log's last 12 lines (`disk.device`, `disk.log`). The owner reads them under "The volume (.volume.log)" in `/games/<box>`'s run log and on the terminal's box screen, and a last line starting `failed:` or `refused:` comes first in the Box section and the Games screen's top line as "The volume: …". |
| 26 | 26 | - **Updating itself:** every sync answers `agentSha`, the sha256 of the agent this forge ships. An agent whose own sha differs fetches `GET /api/v1/games/agent/script` with its token, checks that the fetched script's sha is the one answered and that `bash -n` parses it, puts it in place (`agent.sh.next`, then a rename) and exits, and systemd starts the new one. A script whose sha is not the one answered is deleted and never runs; an agent already current fetches nothing. An agent older than this step does not update itself; it takes the new one with its machine's next replace. |
| 27 | - **`/forge` on the proxy:** the proxy's plugin leaves each request as `plugins/forge/outbox/<id>.json` (at most 2 KiB) and shows a player what it finds in `plugins/forge/inbox`. The agent reads and writes both inside the running `velocity` container, as `bungeecord`; with no running `velocity` service it does nothing there. A full sync's body carries the oldest 20 as `ask`, and only the files whose ids the answer's `asked` names are deleted: with no `asked` (an older forge) none is, and the plugin stops at 64 waiting. A file over 2 KiB, one that is no JSON object, or one whose `id` is not its file's name in lowercase is dropped, with one line in the agent's log that never holds its text. The answer's `told` (at most 20, 16 KiB each) goes to the inbox, each file put in place whole, none while 64 wait. A quick poll that says `"told": true` may be answered with `told` too; `ask` goes with full syncs only. | |
| 27 | 28 | - **The token:** made whenever the box's machine is made (a create or a replace, which ends the old one's), kept only as its sha256 (`game_box.token_hash`, starting `fgb_`), and written only into the machine's `/opt/forge-game/agent.env` (mode 600), which the unit reads; the agent hands it to curl in a header file, never on a command line. `rotateGameBoxToken` makes a new one. |
| 28 | 29 | - **DNS:** an A record, not proxied, for the box's `host` in any Cloudflare zone a connected token can write; with none, the plan says to set it by hand. |
| 29 | 30 |
src/games/agent.sh +67 −2
| @@ -1039,12 +1039,71 @@ world_steps() { | ||
| 1039 | 1039 | export -f world_job world_steps world_cid world_sh world_level world_index world_snapshot world_own_sh compose capped |
| 1040 | 1040 | export WORLDS HELPER |
| 1041 | 1041 | |
| 1042 | # ---- /forge on the proxy: its plugin leaves each request as a file in plugins/forge/outbox and shows | |
| 1043 | # what it finds in plugins/forge/inbox. Both are read and written inside the proxy's own container | |
| 1044 | # (which runs no player's mods), as the proxy's own user: the host never opens a file there. ---- | |
| 1045 | ||
| 1046 | # `sh` in the proxy, in one of the plugin's two folders, when neither it nor its parents is a link. | |
| 1047 | desk() { | |
| 1048 | local box="$1" script="$2" | |
| 1049 | shift 2 | |
| 1050 | capped 5 "" exec -T -u bungeecord velocity sh -c 'for d in plugins plugins/forge "plugins/forge/$0"; do [ -d "$d" ] && [ ! -L "$d" ] || exit 0; done | |
| 1051 | cd "plugins/forge/$0" || exit 0 | |
| 1052 | '"$script" "$box" "$@" 2>/dev/null | |
| 1053 | } | |
| 1054 | # Whether the proxy runs, as the last full sync's services say: nothing is asked of one that does not. | |
| 1055 | desk_here() { jq -e 'any(.[]; .name == "velocity" and .state == "running")' "/.sync.services" >/dev/null 2>&1; } | |
| 1056 | # The requests among the oldest 20 files. One the forge could never name (over 2 KiB, no JSON object, | |
| 1057 | # or an id that is not the file's own lowercase name) would hold its place for ever: it is dropped. | |
| 1058 | # A line without its closing tab was cut short (the 5 s, a read that failed): left for the next sync. | |
| 1059 | desk_asks() { | |
| 1060 | local rows | |
| 1061 | local -a bad | |
| 1062 | desk_here || return 0 | |
| 1063 | # shellcheck disable=SC2016 # the script runs in the container's own shell | |
| 1064 | rows="$(desk outbox 'ls -tr | grep -E "^[0-9a-fA-F-]{36}[.]json$" | { n=0; while read -r f; do | |
| 1065 | [ -f "" ] && [ ! -L "" ] || continue | |
| 1066 | printf "%s\t" ""; { [ "$(wc -c < "")" -gt 2048 ] || tr "\n\r\t" " " < ""; } && printf "\t"; echo | |
| 1067 | n=; [ "" -lt 20 ] || break | |
| 1068 | done; }' </dev/null | jq -cRn '[inputs | split("\t") | select(length == 3 and (.[0] | test("\\A[0-9a-fA-F-]{36}[.]json\\z"))) | {file: .[0], ask: (.[1] // "" | fromjson? // null)} | |
| 1069 | | .file as | if (.ask | type) == "object" and (.ask.id | type) == "string" and .ask.id + ".json" == and (.ask.id | test("\\A[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}\\z")) then . else .ask = null end]')" | |
| 1070 | mapfile -t bad < <(jq -r '.[] | select(.ask == null) | .file' <<<"" 2>/dev/null) | |
| 1071 | if [ "" != 0 ]; then | |
| 1072 | # shellcheck disable=SC2016 | |
| 1073 | desk outbox 'rm -f -- "$@"' "" </dev/null | |
| 1074 | echo "forge-game-agent: dropped files in /forge's outbox that are no request" >&2 | |
| 1075 | fi | |
| 1076 | jq -c '[.[].ask | objects | |
| 1077 | | with_entries(select((.key == "at" and (.value | type) == "number") or ((.key | IN("id", "uuid", "name", "server", "words", "confirms")) and (.value | type) == "string")))]' <<<"$rows" 2>/dev/null | |
| 1078 | } | |
| 1079 | # Of the requests this sync carried, those the answer's `asked` names are deleted. An older forge | |
| 1080 | # names none: they stay, and the plugin itself stops at 64 waiting. | |
| 1081 | desk_asked() { | |
| 1082 | local -a files | |
| 1083 | mapfile -t files < <(jq -r --slurpfile sent "/.sync.ask" '(.asked | arrays) as $took | $sent[0][].id | select(. as $id | $took | index([$id])) | . + ".json"' <<<"$1" 2>/dev/null) | |
| 1084 | # shellcheck disable=SC2016 | |
| 1085 | [ "" = 0 ] || desk outbox 'rm -f -- "$@"' "" </dev/null | |
| 1086 | return 0 | |
| 1087 | } | |
| 1088 | # What the forge answers, each a file put in place whole, under a name made here. | |
| 1089 | desk_told() { | |
| 1090 | desk_here && jq -e '(.told // []) | length > 0' >/dev/null 2>&1 <<<"$1" || return 0 | |
| 1091 | # shellcheck disable=SC2016 | |
| 1092 | jq -c '.told[0:20][] | {id, uuid, text, confirm} | select((tojson | utf8bytelength) <= 16384)' <<<"$1" 2>/dev/null | | |
| 1093 | desk inbox 'umask 022; n=0; while IFS= read -r line; do | |
| 1094 | [ "$(ls | wc -l)" -lt 64 ] || exit 0 | |
| 1095 | n=; printf "%s\n" "" > "$1-.tmp" && mv "$1-.tmp" "$1-.json" | |
| 1096 | done' "$(date +%s)-$RANDOM" || echo "forge-game-agent: an answer to /forge could not be put in the proxy" >&2 | |
| 1097 | return 0 | |
| 1098 | } | |
| 1099 | ||
| 1042 | 1100 | sync() { |
| 1043 | 1101 | local next answer code |
| 1044 | 1102 | next="$(now)" |
| 1045 | 1103 | # Each part through a file, checked as JSON on the way: a sync can pass the 128 KB one argument |
| 1046 | 1104 | # may hold (200 log lines from each of several servers), and then curl would never run. |
| 1047 | 1105 | services | json_or '[]' >"/.sync.services" |
| 1106 | desk_asks | json_or '[]' >"/.sync.ask" | |
| 1048 | 1107 | logs "" | json_or '{}' >"/.sync.logs" |
| 1049 | 1108 | json_or '[]' <"/.results" >"/.sync.results" |
| 1050 | 1109 | host | json_or '{}' >"/.sync.host" |
| @@ -1071,6 +1130,7 @@ sync() { | ||
| 1071 | 1130 | --argjson retry "$([ -f "/.sync.retry" ] && echo true || echo false)" \ |
| 1072 | 1131 | --arg lend "$(lend_has)" \ |
| 1073 | 1132 | --arg tokenSha "$(token_offer)" \ |
| 1133 | --slurpfile ask "/.sync.ask" \ | |
| 1074 | 1134 | 'def sect: {"0":"r","1":"l","4":"n","22":"r","24":"r","39":"r","30":"0","31":"4","32":"2","33":"6","34":"1","35":"5","36":"3","37":"7","90":"8","91":"c","92":"a","93":"e","94":"9","95":"d","96":"b","97":"f"} as $t |
| 1075 | 1135 | | ["30","31","32","33","34","35","36","37","90","91","92","93","94","95","96","97"] as |
| 1076 | 1136 | # 38;5;n is one of the sixteen colours by its number; a background (48) and the other |
| @@ -1083,6 +1143,7 @@ sync() { | ||
| 1083 | 1143 | def plain: gsub("\u001b\\[(?<p>[0-9;]*)m"; (.p // "" | sect)) | gsub("^(\u00a7r)+|(\u00a7r)+$"; "") | gsub("(\u00a7r)+"; "\u00a7r") | gsub("\u001b\\[[0-9;?]*[ -/]*[@-~]"; "") | gsub("\u001b[@-_]?"; "") | gsub("[\u0001-\u0008\u000b\u000c\u000e-\u001f\u007f]"; ""); |
| 1084 | 1144 | {agent: 1, composeSha: , services: [0], logs: [0], results: [0], host: [0], diag: [0], people: [0], metrics: [0], snapshots: [0], chat: [0].lines, chatId: [0].id, lend: , retry: } |
| 1085 | 1145 | + (if == "" then {} else {tokenSha: } end) |
| 1146 | + (if [0] == [] then {} else {ask: [0]} end) | |
| 1086 | 1147 | | walk(if type == "string" then plain else . end)' >"$DIR/.sync.body" || return 1 |
| 1087 | 1148 | # The answer is never a file: it can carry a world's signed link. Its status is its last line. |
| 1088 | 1149 | answer="$(curl -sS -m 20 -H "@" -H 'content-type: application/json' --data-binary "@/.sync.body" -w '\n%{http_code}' "/api/v1/games/agent/sync")" || answer=$'\nnothing' |
| @@ -1097,6 +1158,9 @@ sync() { | ||
| 1097 | 1158 | return 1 |
| 1098 | 1159 | fi |
| 1099 | 1160 | token_answered "" |
| 1161 | # First of all that can fail: the forge hands each answer out once. | |
| 1162 | desk_told "" | |
| 1163 | desk_asked "" | |
| 1100 | 1164 | rm -f "/.sync.retry" |
| 1101 | 1165 | # Delivered: the results and the log lines are the forge's now. |
| 1102 | 1166 | echo '[]' >"/.results" |
| @@ -1516,10 +1580,11 @@ light_sync() { | ||
| 1516 | 1580 | job_tick |
| 1517 | 1581 | shell_tick |
| 1518 | 1582 | json_or '[]' <"/.results" >"/.sync.results" |
| 1519 | jq -cn --arg sha "$(cat "/.compose-sha" 2>/dev/null)" --slurpfile results "/.sync.results" \ | |
| 1520 | '{agent: 1, light: true, composeSha: $sha, results: $results[0]}' >"/.sync.light" || return 1 | |
| 1583 | jq -cn --arg sha "$(cat "/.compose-sha" 2>/dev/null)" --slurpfile results "/.sync.results" --argjson told "$(desk_here && echo true || echo false)" \ | |
| 1584 | '{agent: 1, light: true, composeSha: $sha, results: $results[0]} + (if $told then {told: true} else {} end)' >"/.sync.light" || return 1 | |
| 1521 | 1585 | answer="$(curl -fsS -m 5 -H "@" -H 'content-type: application/json' --data-binary "@/.sync.light" "/api/v1/games/agent/sync")" || return 1 |
| 1522 | 1586 | echo '[]' >"/.results" |
| 1587 | desk_told "" | |
| 1523 | 1588 | apply "" |
| 1524 | 1589 | # Something ran: its result goes in now. Nothing ran: the next poll is a second away. |
| 1525 | 1590 | [ "$(jq 'length' "/.results" 2>/dev/null || echo 0)" = 0 ] && return 0 |
test/game-desk.test.ts added +193
| @@ -0,0 +1,193 @@ | ||
| 1 | /** | |
| 2 | * `/forge` on the proxy: its plugin leaves requests as files and reads answers as files, and the agent | |
| 3 | * carries both, inside the proxy's own container, so the host never opens a file of that folder. A | |
| 4 | * request is deleted only when an answer's `asked` names it; a file that is no request is dropped. The | |
| 5 | * container here is a shell in a folder. | |
| 6 | */ | |
| 7 | import { afterEach, expect, test } from "bun:test"; | |
| 8 | import { chmodSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, symlinkSync, utimesSync, writeFileSync } from "node:fs"; | |
| 9 | import { rm } from "node:fs/promises"; | |
| 10 | import { tmpdir } from "node:os"; | |
| 11 | import { join } from "node:path"; | |
| 12 | import { pollUntil } from "./helpers/poll.ts"; | |
| 13 | ||
| 14 | const AGENT = join(import.meta.dir, "../src/games/agent.sh"); | |
| 15 | const lacks = ["bash", "curl", "jq"].filter((t) => Bun.which(t) === null); | |
| 16 | const cleanups: (() => Promise<void>)[] = []; | |
| 17 | afterEach(async () => { | |
| 18 | for (const c of cleanups.splice(0)) await c(); | |
| 19 | }); | |
| 20 | const uuid = (n: number): string => `00000000-0000-4000-8000-${String(n).padStart(12, "0")}`; | |
| 21 | ||
| 22 | function made(answer: (n: number, ask: { id: string }[] | undefined) => Response, service = "velocity") { | |
| 23 | const root = mkdtempSync(join(tmpdir(), "forge-game-desk-")); | |
| 24 | const proxy = join(root, "proxy"); | |
| 25 | const desk = join(proxy, "plugins/forge"); | |
| 26 | mkdirSync(join(desk, "outbox"), { recursive: true }); | |
| 27 | mkdirSync(join(desk, "inbox")); | |
| 28 | const fake = join(root, "docker"); | |
| 29 | writeFileSync( | |
| 30 | fake, | |
| 31 | `#!/usr/bin/env bash | |
| 32 | case "$*" in *" ps -a --format json") echo '{"Name":"c1","Service":"${service}","State":"running"}'; exit ;; esac | |
| 33 | while [ $# -gt 0 ] && [ "$1" != exec ]; do shift; done | |
| 34 | [ $# = 0 ] || echo x >>"${root}/execs" | |
| 35 | [ "\${1:-}" = exec ] && [ "$2 $3 $4 $5" = "-T -u bungeecord velocity" ] || exit 0 | |
| 36 | shift 5 | |
| 37 | cd "${proxy}" || exit 1 | |
| 38 | if [ -f "${root}/cut" ]; then "$@" | head -c 150; else exec "$@"; fi | |
| 39 | `, | |
| 40 | ); | |
| 41 | chmodSync(fake, 0o755); | |
| 42 | const bodies: Record<string, any>[] = []; | |
| 43 | const forge = Bun.serve({ | |
| 44 | port: 0, | |
| 45 | hostname: "127.0.0.1", | |
| 46 | async fetch(req) { | |
| 47 | bodies.push((await req.json()) as Record<string, any>); | |
| 48 | return answer(bodies.length, bodies.at(-1)!["ask"]); | |
| 49 | }, | |
| 50 | }); | |
| 51 | cleanups.push(async () => { | |
| 52 | forge.stop(true); | |
| 53 | await rm(root, { recursive: true, force: true }); | |
| 54 | }); | |
| 55 | const run = async (until?: () => boolean): Promise<void> => { | |
| 56 | const p = Bun.spawn(["bash", AGENT], { | |
| 57 | env: { | |
| 58 | PATH: process.env["PATH"] ?? "", | |
| 59 | FORGE_URL: `http://127.0.0.1:${String(forge.port)}`, | |
| 60 | FORGE_GAME_TOKEN: "fgb_abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG", | |
| 61 | FORGE_GAME_DIR: join(root, "game"), | |
| 62 | FORGE_GAME_DOCKER: fake, | |
| 63 | FORGE_GAME_DIAG_S: "0", | |
| 64 | FORGE_GAME_VOLUME_CHECK: "0", | |
| 65 | FORGE_GAME_ONCE: until === undefined ? "1" : "0", | |
| 66 | }, | |
| 67 | stdout: "ignore", | |
| 68 | stderr: Bun.file(join(root, "log")), | |
| 69 | }); | |
| 70 | try { | |
| 71 | if (until === undefined) await p.exited; | |
| 72 | else await pollUntil(until, "the agent to get there", 30_000); | |
| 73 | } finally { | |
| 74 | p.kill(); | |
| 75 | await p.exited; | |
| 76 | } | |
| 77 | }; | |
| 78 | /** A request, as old as `age` seconds. */ | |
| 79 | const asks = (n: number, age: number, text?: string): void => { | |
| 80 | const f = join(desk, "outbox", `${uuid(n)}.json`); | |
| 81 | writeFileSync(f, text ?? JSON.stringify({ id: uuid(n), at: 1764700000000, uuid: uuid(n), name: "Alice", server: "hub", words: `"$(id)" \\ 'x' é ${n}` }, null, 2)); | |
| 82 | utimesSync(f, Date.now() / 1000 - age, Date.now() / 1000 - age); | |
| 83 | }; | |
| 84 | return { root, desk, bodies, run, asks, log: () => readFileSync(join(root, "log"), "utf8"), outbox: () => readdirSync(join(desk, "outbox")).sort(), inbox: () => readdirSync(join(desk, "inbox")).sort() }; | |
| 85 | } | |
| 86 | const ok = (more: object = {}): Response => Response.json({ composeSha: "", commands: [], ...more }); | |
| 87 | /** An answer that names every request the body carried. */ | |
| 88 | const took = (ask: { id: string }[] | undefined): Response => ok(ask === undefined ? {} : { asked: ask.map((a) => a.id) }); | |
| 89 | const deskTest = test.skipIf(lacks.length > 0); | |
| 90 | ||
| 91 | deskTest("requests go as data, oldest first, and are deleted only once an answer names them", async () => { | |
| 92 | // No answer, then an older forge's (no `asked`), then one that took only the older of the two. | |
| 93 | const b = made((n, ask) => (n === 1 ? new Response("", { status: 502 }) : n === 2 ? ok() : n === 3 ? ok({ asked: [uuid(2), uuid(9), "../x"] }) : took(ask))); | |
| 94 | b.asks(1, 10); | |
| 95 | b.asks(2, 20); | |
| 96 | b.asks(9, 5, "{}"); | |
| 97 | writeFileSync(join(b.desk, "outbox", "half.tmp"), "{"); | |
| 98 | const both = [`${uuid(1)}.json`, `${uuid(2)}.json`, "half.tmp"]; | |
| 99 | await b.run(); | |
| 100 | expect(b.bodies[0]!["ask"].map((a: { id: string }) => a.id)).toEqual([uuid(2), uuid(1)]); | |
| 101 | expect(b.bodies[0]!["ask"][1]).toEqual({ id: uuid(1), at: 1764700000000, uuid: uuid(1), name: "Alice", server: "hub", words: `"$(id)" \\ 'x' é 1` }); | |
| 102 | expect(b.outbox()).toEqual(both); | |
| 103 | await b.run(); | |
| 104 | expect(b.bodies[1]!["ask"]).toHaveLength(2); | |
| 105 | expect(b.outbox()).toEqual(both); | |
| 106 | await b.run(); | |
| 107 | expect(b.outbox()).toEqual([`${uuid(1)}.json`, "half.tmp"]); | |
| 108 | await b.run(); | |
| 109 | expect(b.bodies[3]!["ask"].map((a: { id: string }) => a.id)).toEqual([uuid(1)]); | |
| 110 | expect(b.outbox()).toEqual(["half.tmp"]); | |
| 111 | await b.run(); | |
| 112 | expect(b.bodies[4]!["ask"]).toBeUndefined(); | |
| 113 | }, 60_000); | |
| 114 | ||
| 115 | deskTest("what the forge could never name is dropped, said without its content, and holds no place", async () => { | |
| 116 | const b = made(() => ok()); | |
| 117 | const bad = ["not json", "[1]", "{}", JSON.stringify({ id: uuid(1) }), JSON.stringify({ id: 5 }), "x".repeat(2_049), ""]; | |
| 118 | for (let n = 0; n < 25; n++) b.asks(100 + n, 500 - n, bad[n % bad.length]!); | |
| 119 | b.asks(1, 10); | |
| 120 | await b.run(); | |
| 121 | await b.run(); | |
| 122 | expect(b.outbox()).toEqual([`${uuid(1)}.json`]); | |
| 123 | expect(b.bodies[1]!["ask"].map((a: { id: string }) => a.id)).toEqual([uuid(1)]); | |
| 124 | expect(b.log()).toContain("dropped 5 files"); | |
| 125 | expect(b.log()).not.toContain("not json"); | |
| 126 | }, 60_000); | |
| 127 | ||
| 128 | deskTest("a request whose reading was cut short is neither sent nor dropped", async () => { | |
| 129 | const b = made((_n, ask) => took(ask)); | |
| 130 | b.asks(1, 10); | |
| 131 | writeFileSync(join(b.root, "cut"), ""); | |
| 132 | await b.run(); | |
| 133 | expect(b.bodies[0]!["ask"]).toBeUndefined(); | |
| 134 | expect(b.outbox()).toEqual([`${uuid(1)}.json`]); | |
| 135 | }, 60_000); | |
| 136 | ||
| 137 | deskTest("at most 20 go, and a link is never read", async () => { | |
| 138 | const b = made((_n, ask) => took(ask)); | |
| 139 | for (let n = 1; n <= 23; n++) b.asks(n, 100 - n); | |
| 140 | writeFileSync(join(b.root, "secret"), '{"id":"secret"}'); | |
| 141 | symlinkSync(join(b.root, "secret"), join(b.desk, "outbox", `${uuid(40)}.json`)); | |
| 142 | await b.run(); | |
| 143 | expect(b.bodies[0]!["ask"].map((a: { id: string }) => a.id)).toEqual(Array.from({ length: 20 }, (_, i) => uuid(i + 1))); | |
| 144 | await b.run(); | |
| 145 | expect(b.bodies[1]!["ask"]).toHaveLength(3); | |
| 146 | expect(JSON.stringify(b.bodies)).not.toContain("secret"); | |
| 147 | expect(b.outbox()).toEqual([`${uuid(40)}.json`]); | |
| 148 | expect(existsSync(join(b.root, "secret"))).toBe(true); | |
| 149 | }, 60_000); | |
| 150 | ||
| 151 | deskTest("a box with no velocity service is never asked, and its quick poll does not say it reads told", async () => { | |
| 152 | const b = made(() => ok({ told: [{ id: uuid(1), uuid: uuid(1), text: "hi", confirm: false }] }), "hub"); | |
| 153 | b.asks(1, 10); | |
| 154 | await b.run(() => b.bodies.some((x) => x["light"] === true)); | |
| 155 | expect(existsSync(join(b.root, "execs"))).toBe(false); | |
| 156 | expect(b.bodies.find((x) => x["light"] === true)!["told"]).toBeUndefined(); | |
| 157 | expect(b.outbox()).toHaveLength(1); | |
| 158 | }, 60_000); | |
| 159 | ||
| 160 | deskTest("answers are put whole in the inbox, readable, under the agent's own names; none while 64 wait", async () => { | |
| 161 | const told = [{ id: "../../x", uuid: uuid(1), text: `Restart "hub"? \\n é $(id)`, confirm: true }, { id: "r2", uuid: uuid(2), text: "y".repeat(17_000), confirm: false }]; | |
| 162 | const b = made(() => ok({ told })); | |
| 163 | await b.run(); | |
| 164 | expect(b.inbox()).toHaveLength(1); | |
| 165 | const f = join(b.desk, "inbox", b.inbox()[0]!); | |
| 166 | expect(b.inbox()[0]).toMatch(/^[0-9-]+\.json$/); | |
| 167 | expect(JSON.parse(readFileSync(f, "utf8"))).toEqual(told[0]); | |
| 168 | for (let i = 0; i < 63; i++) writeFileSync(join(b.desk, "inbox", `w${i}.json`), "{}"); | |
| 169 | await b.run(); | |
| 170 | expect(b.inbox()).toHaveLength(64); | |
| 171 | }, 60_000); | |
| 172 | ||
| 173 | deskTest("an answer that comes with the quick poll is put in the inbox too", async () => { | |
| 174 | const told = [{ id: "r1", uuid: uuid(1), text: "hi", confirm: false }]; | |
| 175 | const b = made((n) => (n === 2 ? Response.json({ commands: [], told }) : ok())); | |
| 176 | await b.run(() => b.inbox().some((f) => f.endsWith(".json"))); | |
| 177 | expect(b.bodies[1]).toMatchObject({ light: true, told: true }); | |
| 178 | expect(JSON.parse(readFileSync(join(b.desk, "inbox", b.inbox()[0]!), "utf8"))).toEqual(told[0]); | |
| 179 | }, 60_000); | |
| 180 | ||
| 181 | deskTest("a folder that is a link is left alone", async () => { | |
| 182 | const b = made(() => ok({ told: [{ id: "r1", uuid: uuid(1), text: "hi", confirm: false }] })); | |
| 183 | const away = join(b.root, "away"); | |
| 184 | mkdirSync(away); | |
| 185 | writeFileSync(join(away, `${uuid(1)}.json`), '{"id":"r1"}'); | |
| 186 | await rm(join(b.desk, "inbox"), { recursive: true }); | |
| 187 | await rm(join(b.desk, "outbox"), { recursive: true }); | |
| 188 | symlinkSync(away, join(b.desk, "inbox")); | |
| 189 | symlinkSync(away, join(b.desk, "outbox")); | |
| 190 | await b.run(); | |
| 191 | expect(b.bodies[0]!["ask"]).toBeUndefined(); | |
| 192 | expect(readdirSync(away)).toEqual([`${uuid(1)}.json`]); | |
| 193 | }, 60_000); |
test/game-token.test.ts +5 −3
| @@ -73,11 +73,13 @@ function made(meddle: (n: number, answer: Response) => Response | Promise<Respon | ||
| 73 | 73 | stdout: "ignore", |
| 74 | 74 | stderr: "ignore", |
| 75 | 75 | }); |
| 76 | if (until !== undefined) { | |
| 77 | await pollUntil(until, "the agent to get there", 30_000); | |
| 76 | try { | |
| 77 | if (until !== undefined) await pollUntil(until, "the agent to get there", 30_000); | |
| 78 | else await p.exited; | |
| 79 | } finally { | |
| 78 | 80 | p.kill(); |
| 81 | await p.exited; | |
| 79 | 82 | } |
| 80 | await p.exited; | |
| 81 | 83 | }; |
| 82 | 84 | const knows = (token: string): boolean => boxByToken(app.db, token) !== undefined; |
| 83 | 85 | return { app, id, boot, dir, env, bodies, kept, run, knows }; |
new above
offline