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 { getSshKey, putKeys } from "./sshkeys";
5
6vi.mock("../lib/sdk", () => ({ configureSdk: vi.fn() }));
7vi.mock("consola", () => ({
8 default: { log: vi.fn(), success: vi.fn(), error: vi.fn(), info: vi.fn() },
9}));
10vi.mock("@pocketenv/sdk", () => ({
11 Sandbox: { get: vi.fn(), configure: vi.fn() },
12}));
13vi.mock("@inquirer/prompts", () => ({
14 editor: vi.fn().mockResolvedValue(
15 "-----BEGIN OPENSSH PRIVATE KEY-----\nfake\n-----END OPENSSH PRIVATE KEY-----",
16 ),
17 input: vi.fn().mockResolvedValue("ssh-ed25519 AAAAC3NzaC1lZDI1NTE5 user@host"),
18}));
19vi.mock("node:fs/promises", () => ({
20 default: { readFile: vi.fn().mockResolvedValue("file-key-content") },
21}));
22
23describe("sshkeys commands", () => {
24 beforeEach(() => {
25 vi.clearAllMocks();
26 });
27
28 describe("getSshKey", () => {
29 it("logs SSH keys for a sandbox", async () => {
30 const mockSandbox = {
31 sshKeys: {
32 get: vi.fn().mockResolvedValue({
33 privateKey: "-----BEGIN OPENSSH PRIVATE KEY-----\\nfakekey\\n-----END",
34 publicKey: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5 user@host",
35 }),
36 },
37 };
38 vi.mocked(Sandbox.get).mockResolvedValue(mockSandbox as any);
39
40 await getSshKey("my-sandbox");
41
42 expect(mockSandbox.sshKeys.get).toHaveBeenCalledOnce();
43 expect(consola.log).toHaveBeenCalledWith(
44 expect.stringContaining("Private Key"),
45 );
46 expect(consola.log).toHaveBeenCalledWith(
47 expect.stringContaining("Public Key"),
48 );
49 });
50
51 it("logs info when no SSH keys are found", async () => {
52 const mockSandbox = {
53 sshKeys: {
54 get: vi.fn().mockRejectedValue(new Error("Not found")),
55 },
56 };
57 vi.mocked(Sandbox.get).mockResolvedValue(mockSandbox as any);
58
59 await getSshKey("my-sandbox");
60
61 expect(consola.info).toHaveBeenCalledWith(
62 expect.stringContaining("No SSH keys found"),
63 );
64 });
65 });
66
67 describe("putKeys", () => {
68 it("generates and saves SSH keys when generate option is true", async () => {
69 const mockSandbox = {
70 sshKeys: {
71 generate: vi.fn().mockResolvedValue({
72 privateKey: "-----BEGIN OPENSSH PRIVATE KEY-----\ngenerated\n-----END OPENSSH PRIVATE KEY-----",
73 publicKey: "ssh-ed25519 AAAA generated-pub",
74 }),
75 put: vi.fn().mockResolvedValue(undefined),
76 },
77 };
78 vi.mocked(Sandbox.get).mockResolvedValue(mockSandbox as any);
79
80 await putKeys("my-sandbox", { generate: true });
81
82 expect(mockSandbox.sshKeys.generate).toHaveBeenCalledOnce();
83 expect(mockSandbox.sshKeys.put).toHaveBeenCalledWith(
84 "ssh-ed25519 AAAA generated-pub",
85 "-----BEGIN OPENSSH PRIVATE KEY-----\ngenerated\n-----END OPENSSH PRIVATE KEY-----",
86 );
87 expect(consola.success).toHaveBeenCalledWith("SSH keys saved successfully!");
88 });
89
90 it("logs error when saving SSH keys fails", async () => {
91 const mockSandbox = {
92 sshKeys: {
93 generate: vi.fn().mockResolvedValue({
94 privateKey: "-----BEGIN OPENSSH PRIVATE KEY-----\ngenerated\n-----END OPENSSH PRIVATE KEY-----",
95 publicKey: "ssh-ed25519 AAAA generated-pub",
96 }),
97 put: vi.fn().mockRejectedValue(new Error("Save failed")),
98 },
99 };
100 vi.mocked(Sandbox.get).mockResolvedValue(mockSandbox as any);
101
102 await putKeys("my-sandbox", { generate: true });
103
104 expect(consola.error).toHaveBeenCalledWith("Failed to save SSH keys");
105 });
106 });
107});