/ sdks / sandbox / javascript / tests / manager.test.mjs
manager.test.mjs
 1  import assert from "node:assert/strict";
 2  import test from "node:test";
 3  
 4  import { ConnectionConfig, SandboxManager } from "../dist/index.js";
 5  
 6  function createSandboxesStub() {
 7    const calls = [];
 8    const sandboxes = {
 9      async listSandboxes(filter) {
10        calls.push({ method: "listSandboxes", args: [filter] });
11        return { items: [{ id: "sbx-1" }] };
12      },
13      async getSandbox(sandboxId) {
14        calls.push({ method: "getSandbox", args: [sandboxId] });
15        return { id: sandboxId };
16      },
17      async deleteSandbox(sandboxId) {
18        calls.push({ method: "deleteSandbox", args: [sandboxId] });
19      },
20      async pauseSandbox(sandboxId) {
21        calls.push({ method: "pauseSandbox", args: [sandboxId] });
22      },
23      async resumeSandbox(sandboxId) {
24        calls.push({ method: "resumeSandbox", args: [sandboxId] });
25      },
26      async renewSandboxExpiration(sandboxId, body) {
27        calls.push({ method: "renewSandboxExpiration", args: [sandboxId, body] });
28      },
29    };
30    return { sandboxes, calls };
31  }
32  
33  test("SandboxManager delegates lifecycle operations and closes its transport", async () => {
34    const { sandboxes, calls } = createSandboxesStub();
35    const connectionConfig = new ConnectionConfig({ domain: "http://127.0.0.1:8080" });
36    let closeCalls = 0;
37    connectionConfig.closeTransport = async () => {
38      closeCalls += 1;
39    };
40    connectionConfig.withTransportIfMissing = () => connectionConfig;
41  
42    const manager = SandboxManager.create({
43      connectionConfig,
44      adapterFactory: {
45        createLifecycleStack() {
46          return { sandboxes };
47        },
48      },
49    });
50  
51    const list = await manager.listSandboxInfos({
52      states: ["Running"],
53      metadata: { team: "sdk" },
54      page: 2,
55      pageSize: 5,
56    });
57    assert.equal(list.items[0].id, "sbx-1");
58  
59    const info = await manager.getSandboxInfo("sbx-42");
60    assert.equal(info.id, "sbx-42");
61  
62    await manager.pauseSandbox("sbx-42");
63    await manager.resumeSandbox("sbx-42");
64    await manager.killSandbox("sbx-42");
65    await manager.renewSandbox("sbx-42", 30);
66    await manager.close();
67  
68    assert.deepEqual(
69      calls.map((entry) => entry.method),
70      [
71        "listSandboxes",
72        "getSandbox",
73        "pauseSandbox",
74        "resumeSandbox",
75        "deleteSandbox",
76        "renewSandboxExpiration",
77      ],
78    );
79    assert.deepEqual(calls[0].args[0], {
80      states: ["Running"],
81      metadata: { team: "sdk" },
82      page: 2,
83      pageSize: 5,
84    });
85    assert.equal(calls[5].args[0], "sbx-42");
86    assert.ok(typeof calls[5].args[1].expiresAt === "string");
87    assert.ok(Number.isFinite(Date.parse(calls[5].args[1].expiresAt)));
88    assert.equal(closeCalls, 1);
89  });