-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify.mjs
More file actions
64 lines (57 loc) · 2.21 KB
/
Copy pathverify.mjs
File metadata and controls
64 lines (57 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* Proves this example works: boot a sandbox, write a text file, read it back,
* list the directory, assert the round-trip, dispose.
* Token/workspace from env (CI) or ~/.config/tenki/config.yaml (local `tenki login`).
* Exits non-zero on any failure.
*/
import { TenkiSandbox } from "@tenkicloud/sandbox";
import { readFileSync } from "node:fs";
import { homedir } from "node:os";
const cfg = (key) => {
try {
const c = readFileSync(`${homedir()}/.config/tenki/config.yaml`, "utf8");
return (c.match(new RegExp(`^${key}:\\s*(.+)$`, "m"))?.[1] ?? "").trim();
} catch {
return "";
}
};
const authToken = process.env.TENKI_AUTH_TOKEN || process.env.TENKI_API_KEY || cfg("auth_token");
const workspaceId = process.env.TENKI_WORKSPACE_ID || cfg("current_workspace_id");
if (!authToken) {
console.error("No token. Set TENKI_AUTH_TOKEN, or run `tenki login`.");
process.exit(1);
}
const path = "notes.txt";
const text = "hello from tenki\n";
const tenki = new TenkiSandbox({ authToken });
let sandbox;
try {
sandbox = await tenki.createAndWait({ cpuCores: 1, memoryMb: 1024, workspaceId });
// write -> read back -> assert the bytes survive the round-trip.
await sandbox.writeFile(path, text);
const bytes = await sandbox.readFile(path);
const back = new TextDecoder().decode(bytes);
if (back !== text) {
throw new Error(`round-trip mismatch: wrote ${JSON.stringify(text)}, read ${JSON.stringify(back)}`);
}
// list the dir -> assert the file shows up as a file of the right size.
const entries = await sandbox.list(".");
const found = entries.find((f) => f.path === path);
if (!found) throw new Error(`${path} not in listing: [${entries.map((f) => f.path).join(", ")}]`);
if (found.isDir) throw new Error(`${path} listed as a directory, expected a file`);
if (Number(found.size) !== bytes.length) {
throw new Error(`listed size ${found.size} != ${bytes.length} bytes read`);
}
console.log(`✓ files-in-a-sandbox: write → read "${back.trim()}" → list (${entries.length} entry) → dispose`);
} catch (e) {
console.error("✗ " + (e?.message ?? e));
process.exitCode = 1;
} finally {
if (sandbox) {
try {
await sandbox[Symbol.asyncDispose]();
} catch {
/* self-reaps via idle/lifetime caps */
}
}
}