|
| 1 | +/** |
| 2 | + * Proves the ComputeSDK Tenki provider works against live Tenki: create a |
| 3 | + * sandbox through `compute`, run a command (assert 42), round-trip a file |
| 4 | + * through the native filesystem API, then destroy the sandbox. |
| 5 | + * Token/workspace from env (CI) or ~/.config/tenki/config.yaml (local `tenki login`). |
| 6 | + * Exits non-zero on any failure. |
| 7 | + */ |
| 8 | +import { compute } from "computesdk"; |
| 9 | +import { tenki } from "@computesdk/tenki"; |
| 10 | +import { readFileSync } from "node:fs"; |
| 11 | +import { homedir } from "node:os"; |
| 12 | + |
| 13 | +const cfg = (key) => { |
| 14 | + try { |
| 15 | + const c = readFileSync(`${homedir()}/.config/tenki/config.yaml`, "utf8"); |
| 16 | + return (c.match(new RegExp(`^${key}:\\s*(.+)$`, "m"))?.[1] ?? "").trim(); |
| 17 | + } catch { |
| 18 | + return ""; |
| 19 | + } |
| 20 | +}; |
| 21 | + |
| 22 | +const apiKey = process.env.TENKI_AUTH_TOKEN || process.env.TENKI_API_KEY || cfg("auth_token"); |
| 23 | +const workspaceId = process.env.TENKI_WORKSPACE_ID || cfg("current_workspace_id") || undefined; |
| 24 | +if (!apiKey) { |
| 25 | + console.error("No token. Set TENKI_AUTH_TOKEN, or run `tenki login`."); |
| 26 | + process.exit(1); |
| 27 | +} |
| 28 | + |
| 29 | +compute.setConfig({ provider: tenki({ apiKey, ...(workspaceId ? { workspaceId } : {}) }) }); |
| 30 | + |
| 31 | +let sandbox; |
| 32 | +try { |
| 33 | + sandbox = await compute.sandbox.create(); |
| 34 | + |
| 35 | + // 1) runCommand (goes through `sh -lc`) → assert stdout |
| 36 | + const r = await sandbox.runCommand("python3 -c 'print(6 * 7)'"); |
| 37 | + if (!(r.exitCode === 0 && r.stdout.trim() === "42")) { |
| 38 | + throw new Error(`runCommand: exit ${r.exitCode}, stdout ${JSON.stringify(r.stdout)}, stderr ${JSON.stringify(r.stderr)}`); |
| 39 | + } |
| 40 | + |
| 41 | + // 2) native filesystem API → write, assert exists, read back |
| 42 | + const path = "/home/tenki/verify-note.txt"; |
| 43 | + const content = "written via the ComputeSDK filesystem API\n"; |
| 44 | + await sandbox.filesystem.writeFile(path, content); |
| 45 | + if (!(await sandbox.filesystem.exists(path))) throw new Error("exists() false after writeFile"); |
| 46 | + const back = await sandbox.filesystem.readFile(path); |
| 47 | + if (back !== content) throw new Error(`file round-trip mismatch: ${JSON.stringify(back)}`); |
| 48 | + |
| 49 | + console.log("✓ computesdk-tenki: compute.sandbox.create → runCommand (42) → filesystem round-trip → destroy"); |
| 50 | +} catch (e) { |
| 51 | + console.error("✗ " + (e?.message ?? e)); |
| 52 | + process.exitCode = 1; |
| 53 | +} finally { |
| 54 | + if (sandbox) { |
| 55 | + try { |
| 56 | + await sandbox.destroy(); |
| 57 | + } catch { |
| 58 | + /* self-reaps via idle/lifetime caps */ |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments