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 login from "./login";
3
4vi.mock("../client", () => ({
5 client: { post: vi.fn() },
6}));
7vi.mock("open", () => ({
8 default: vi.fn().mockResolvedValue(undefined),
9}));
10vi.mock("node:fs/promises", () => ({
11 default: {
12 mkdir: vi.fn().mockResolvedValue(undefined),
13 writeFile: vi.fn().mockResolvedValue(undefined),
14 },
15}));
16
17import { client } from "../client";
18import open from "open";
19
20describe("login", () => {
21 let consoleErrorSpy: ReturnType<typeof vi.spyOn>;
22 let consoleLogSpy: ReturnType<typeof vi.spyOn>;
23
24 beforeEach(() => {
25 vi.clearAllMocks();
26 consoleErrorSpy = vi
27 .spyOn(console, "error")
28 .mockImplementation(() => undefined);
29 consoleLogSpy = vi
30 .spyOn(console, "log")
31 .mockImplementation(() => undefined);
32 });
33
34 it("logs an error and closes server when redirect URL lacks authorize", async () => {
35 vi.mocked(client.post).mockResolvedValue({
36 data: "https://example.com/callback",
37 });
38
39 await login("test.bsky.social");
40
41 expect(client.post).toHaveBeenCalledWith("/login", {
42 handle: "test.bsky.social",
43 cli: true,
44 });
45 expect(consoleErrorSpy).toHaveBeenCalledWith(
46 "Failed to login, please check your handle and try again.",
47 );
48 expect(open).not.toHaveBeenCalled();
49 });
50
51 it("opens the browser when redirect URL contains authorize", async () => {
52 vi.mocked(client.post).mockResolvedValue({
53 data: "https://bsky.app/oauth/authorize?request_uri=abc",
54 });
55
56 await login("test.bsky.social");
57
58 expect(open).toHaveBeenCalledWith(
59 "https://bsky.app/oauth/authorize?request_uri=abc",
60 );
61 expect(consoleLogSpy).toHaveBeenCalledWith(
62 expect.stringContaining("https://bsky.app/oauth/authorize"),
63 );
64 });
65});