the universal sandbox runtime for agents and humans.
pocketenv.io
sandbox
openclaw
agent
claude-code
vercel-sandbox
deno-sandbox
cloudflare-sandbox
atproto
sprites
daytona
1import { vi, describe, it, expect, beforeEach } from "vitest";
2import consola from "consola";
3import { Sandbox } from "@pocketenv/sdk";
4import { exposePort } from "./expose";
5
6vi.mock("../lib/sdk", () => ({ configureSdk: vi.fn() }));
7vi.mock("../theme", () => ({
8 c: {
9 primary: (s: string | number) => String(s),
10 secondary: (s: string | number) => String(s),
11 },
12}));
13vi.mock("consola", () => ({
14 default: { log: vi.fn(), success: vi.fn(), error: vi.fn() },
15}));
16vi.mock("@pocketenv/sdk", () => ({
17 Sandbox: { get: vi.fn(), configure: vi.fn() },
18}));
19
20describe("exposePort", () => {
21 const mockExit = vi
22 .spyOn(process, "exit")
23 .mockImplementation(() => undefined as never);
24
25 beforeEach(() => {
26 vi.clearAllMocks();
27 });
28
29 it("exposes a port and logs success with preview URL", async () => {
30 const mockSandbox = {
31 expose: vi
32 .fn()
33 .mockResolvedValue({ previewUrl: "https://preview.example.com" }),
34 };
35 vi.mocked(Sandbox.get).mockResolvedValue(mockSandbox as any);
36
37 await exposePort("my-sandbox", 3000, "My app");
38
39 expect(Sandbox.get).toHaveBeenCalledWith("my-sandbox");
40 expect(mockSandbox.expose).toHaveBeenCalledWith(3000, "My app");
41 expect(consola.success).toHaveBeenCalledTimes(2);
42 expect(consola.success).toHaveBeenCalledWith(
43 expect.stringContaining("3000"),
44 );
45 expect(consola.success).toHaveBeenCalledWith(
46 expect.stringContaining("https://preview.example.com"),
47 );
48 });
49
50 it("exposes a port without preview URL", async () => {
51 const mockSandbox = {
52 expose: vi.fn().mockResolvedValue({ previewUrl: null }),
53 };
54 vi.mocked(Sandbox.get).mockResolvedValue(mockSandbox as any);
55
56 await exposePort("my-sandbox", 8080);
57
58 expect(consola.success).toHaveBeenCalledOnce();
59 });
60
61 it("logs error and exits on failure", async () => {
62 vi.mocked(Sandbox.get).mockRejectedValue(new Error("API error"));
63
64 await exposePort("my-sandbox", 3000);
65
66 expect(consola.error).toHaveBeenCalledWith(
67 "Failed to expose port:",
68 expect.any(Error),
69 );
70 expect(mockExit).toHaveBeenCalledWith(1);
71 });
72});