Skip to content
Myr

box-sdk.test.ts

This file

Path

apps/sandbox/test/box-sdk.test.ts

144 lines · 3.9 KiB · TypeScript

Last changed by

Merge build/comments-ops into mainoscabrielbefore adoptione4c407e

History of this file

Take it

The walk stopped after 200 commits: lines older than 678c8b8 point at it.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 1–144why, one step back
1import { describe, expect, it } from "@effect/vitest";
2import { untouchable } from "@repo/testkit/untouchable";3import { Effect, Fiber } from "effect";4import { TestClock } from "effect/testing";56import { Box, BoxFailed } from "../src/box.ts";7import { SdkBox } from "../src/box/sdk.ts";89/** `text` as the Container API hands output back: a plain `ArrayBuffer`. */10const bytes = (text: string) => {11 const encoded = new TextEncoder().encode(text);12 const buffer = new ArrayBuffer(encoded.length);1314 new Uint8Array(buffer).set(encoded);1516 return buffer;17};1819const fakeContainer = (running: boolean) => {20 const started: ContainerStartupOptions[] = [];21 const commands: { argv: string[]; options: ContainerExecOptions }[] = [];2223 const container = untouchable<Container>("container", {24 exec: (argv, options = {}) => {25 commands.push({ argv, options });2627 return Promise.resolve(28 untouchable<ExecProcess>("process", {29 output: () =>30 Promise.resolve({31 exitCode: 0,32 stderr: bytes(""),33 stdout: bytes("git version 2.47.3\n"),34 }),35 })36 );37 },38 running,39 start: (options) => {40 if (options !== undefined) {41 started.push(options);42 }43 },44 });4546 return { commands, container, started };47};4849const start = { enableInternet: false, image: "check" };5051const hungContainer = () => {52 const killed: (number | undefined)[] = [];5354 const container = untouchable<Container>("container", {55 exec: () =>56 Promise.resolve(57 untouchable<ExecProcess>("process", {58 kill: (signal) => {59 killed.push(signal);60 },61 // A race of nothing never settles: the command runs until it is killed.62 output: () => Promise.race<ExecOutput>([]),63 })64 ),65 running: true,66 });6768 return { container, killed };69};7071describe("Box over the Sandbox SDK", () => {72 it.effect(73 "runs a command in the container and reads its output as text",74 () =>75 Effect.gen(function* () {76 const fake = fakeContainer(true);7778 const executed = yield* Box.use(({ exec }) =>79 exec(["git", "--version"], { cwd: "/workspace", env: { HOME: "/" } })80 ).pipe(Effect.provide(SdkBox.layer(fake.container, start)));8182 expect(executed).toStrictEqual({83 exitCode: 0,84 stderr: "",85 stdout: "git version 2.47.3\n",86 });87 expect(88 fake.commands.map(({ argv, options }) => ({89 argv,90 cwd: options.cwd,91 env: options.env,92 }))93 ).toStrictEqual([94 { argv: ["git", "--version"], cwd: "/workspace", env: { HOME: "/" } },95 ]);96 expect(fake.started).toStrictEqual([]);97 })98 );99100 it.effect(101 "starts a stopped container with its image, offline, before the first command",102 () =>103 Effect.gen(function* () {104 const fake = fakeContainer(false);105106 yield* Box.use(({ exec }) => exec(["true"])).pipe(107 Effect.provide(SdkBox.layer(fake.container, start))108 );109110 expect(fake.started).toStrictEqual([111 { enableInternet: false, image: "check" },112 ]);113 })114 );115116 it.effect(117 "kills a command that runs past its time limit, so it stops beside the lanes after it",118 () =>119 Effect.gen(function* () {120 const hung = hungContainer();121122 const running = yield* Box.use(({ exec }) =>123 exec(["bun", "test"], { timeoutMs: 1000 })124 ).pipe(125 Effect.provide(SdkBox.layer(hung.container, start)),126 Effect.flip,127 Effect.forkChild128 );129130 yield* TestClock.adjust("2 seconds");131132 expect({133 failed: yield* Fiber.join(running),134 killed: hung.killed.length,135 }).toStrictEqual({136 failed: new BoxFailed({137 operation: "exec",138 reason: "timed out after 1000 ms",139 }),140 killed: 1,141 });142 })143 );144});