feat: add dashboard command
This commit is contained in:
118
src/commands/dashboard.test.ts
Normal file
118
src/commands/dashboard.test.ts
Normal file
@@ -0,0 +1,118 @@
|
||||
import { describe, expect, it, vi, beforeEach } from "vitest";
|
||||
|
||||
import { dashboardCommand } from "./dashboard.js";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
readConfigFileSnapshot: vi.fn(),
|
||||
resolveGatewayPort: vi.fn(),
|
||||
resolveControlUiLinks: vi.fn(),
|
||||
detectBrowserOpenSupport: vi.fn(),
|
||||
openUrl: vi.fn(),
|
||||
formatControlUiSshHint: vi.fn(),
|
||||
copyToClipboard: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("../config/config.js", () => ({
|
||||
readConfigFileSnapshot: mocks.readConfigFileSnapshot,
|
||||
resolveGatewayPort: mocks.resolveGatewayPort,
|
||||
}));
|
||||
|
||||
vi.mock("./onboard-helpers.js", () => ({
|
||||
resolveControlUiLinks: mocks.resolveControlUiLinks,
|
||||
detectBrowserOpenSupport: mocks.detectBrowserOpenSupport,
|
||||
openUrl: mocks.openUrl,
|
||||
formatControlUiSshHint: mocks.formatControlUiSshHint,
|
||||
copyToClipboard: mocks.copyToClipboard,
|
||||
}));
|
||||
|
||||
const runtime = {
|
||||
log: vi.fn(),
|
||||
error: vi.fn(),
|
||||
exit: vi.fn(),
|
||||
};
|
||||
|
||||
function resetRuntime() {
|
||||
runtime.log.mockClear();
|
||||
runtime.error.mockClear();
|
||||
runtime.exit.mockClear();
|
||||
}
|
||||
|
||||
function mockSnapshot(token = "abc") {
|
||||
mocks.readConfigFileSnapshot.mockResolvedValue({
|
||||
path: "/tmp/clawdbot.json",
|
||||
exists: true,
|
||||
raw: "{}",
|
||||
parsed: {},
|
||||
valid: true,
|
||||
config: { gateway: { auth: { token } } },
|
||||
issues: [],
|
||||
legacyIssues: [],
|
||||
});
|
||||
mocks.resolveGatewayPort.mockReturnValue(18789);
|
||||
mocks.resolveControlUiLinks.mockReturnValue({
|
||||
httpUrl: "http://127.0.0.1:18789/",
|
||||
wsUrl: "ws://127.0.0.1:18789",
|
||||
});
|
||||
}
|
||||
|
||||
describe("dashboardCommand", () => {
|
||||
beforeEach(() => {
|
||||
resetRuntime();
|
||||
mocks.readConfigFileSnapshot.mockReset();
|
||||
mocks.resolveGatewayPort.mockReset();
|
||||
mocks.resolveControlUiLinks.mockReset();
|
||||
mocks.detectBrowserOpenSupport.mockReset();
|
||||
mocks.openUrl.mockReset();
|
||||
mocks.formatControlUiSshHint.mockReset();
|
||||
mocks.copyToClipboard.mockReset();
|
||||
});
|
||||
|
||||
it("opens and copies the dashboard link by default", async () => {
|
||||
mockSnapshot("abc123");
|
||||
mocks.copyToClipboard.mockResolvedValue(true);
|
||||
mocks.detectBrowserOpenSupport.mockResolvedValue({ ok: true });
|
||||
mocks.openUrl.mockResolvedValue(true);
|
||||
|
||||
await dashboardCommand(runtime);
|
||||
|
||||
expect(mocks.resolveControlUiLinks).toHaveBeenCalledWith({
|
||||
port: 18789,
|
||||
bind: "loopback",
|
||||
basePath: undefined,
|
||||
});
|
||||
expect(mocks.copyToClipboard).toHaveBeenCalledWith(
|
||||
"http://127.0.0.1:18789/?token=abc123",
|
||||
);
|
||||
expect(mocks.openUrl).toHaveBeenCalledWith(
|
||||
"http://127.0.0.1:18789/?token=abc123",
|
||||
);
|
||||
expect(runtime.log).toHaveBeenCalledWith(
|
||||
"Opened in your browser. Keep that tab to control Clawdbot.",
|
||||
);
|
||||
});
|
||||
|
||||
it("prints SSH hint when browser cannot open", async () => {
|
||||
mockSnapshot("shhhh");
|
||||
mocks.copyToClipboard.mockResolvedValue(false);
|
||||
mocks.detectBrowserOpenSupport.mockResolvedValue({ ok: false, reason: "ssh" });
|
||||
mocks.formatControlUiSshHint.mockReturnValue("ssh hint");
|
||||
|
||||
await dashboardCommand(runtime);
|
||||
|
||||
expect(mocks.openUrl).not.toHaveBeenCalled();
|
||||
expect(runtime.log).toHaveBeenCalledWith("ssh hint");
|
||||
});
|
||||
|
||||
it("respects --no-open and skips browser attempts", async () => {
|
||||
mockSnapshot();
|
||||
mocks.copyToClipboard.mockResolvedValue(true);
|
||||
|
||||
await dashboardCommand(runtime, { noOpen: true });
|
||||
|
||||
expect(mocks.detectBrowserOpenSupport).not.toHaveBeenCalled();
|
||||
expect(mocks.openUrl).not.toHaveBeenCalled();
|
||||
expect(runtime.log).toHaveBeenCalledWith(
|
||||
"Browser launch disabled (--no-open). Use the URL above.",
|
||||
);
|
||||
});
|
||||
});
|
||||
61
src/commands/dashboard.ts
Normal file
61
src/commands/dashboard.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { resolveGatewayPort, readConfigFileSnapshot } from "../config/config.js";
|
||||
import { defaultRuntime } from "../runtime.js";
|
||||
import type { RuntimeEnv } from "../runtime.js";
|
||||
import {
|
||||
copyToClipboard,
|
||||
detectBrowserOpenSupport,
|
||||
formatControlUiSshHint,
|
||||
openUrl,
|
||||
resolveControlUiLinks,
|
||||
} from "./onboard-helpers.js";
|
||||
|
||||
type DashboardOptions = {
|
||||
noOpen?: boolean;
|
||||
};
|
||||
|
||||
export async function dashboardCommand(
|
||||
runtime: RuntimeEnv = defaultRuntime,
|
||||
options: DashboardOptions = {},
|
||||
) {
|
||||
const snapshot = await readConfigFileSnapshot();
|
||||
const cfg = snapshot.valid ? snapshot.config : {};
|
||||
const port = resolveGatewayPort(cfg);
|
||||
const bind = cfg.gateway?.bind ?? "loopback";
|
||||
const basePath = cfg.gateway?.controlUi?.basePath;
|
||||
const token =
|
||||
cfg.gateway?.auth?.token ?? process.env.CLAWDBOT_GATEWAY_TOKEN ?? "";
|
||||
|
||||
const links = resolveControlUiLinks({ port, bind, basePath });
|
||||
const authedUrl = token
|
||||
? `${links.httpUrl}?token=${encodeURIComponent(token)}`
|
||||
: links.httpUrl;
|
||||
|
||||
runtime.log(`Dashboard URL: ${authedUrl}`);
|
||||
|
||||
const copied = await copyToClipboard(authedUrl).catch(() => false);
|
||||
runtime.log(copied ? "Copied to clipboard." : "Copy to clipboard unavailable.");
|
||||
|
||||
let opened = false;
|
||||
let hint: string | undefined;
|
||||
if (!options.noOpen) {
|
||||
const browserSupport = await detectBrowserOpenSupport();
|
||||
if (browserSupport.ok) {
|
||||
opened = await openUrl(authedUrl);
|
||||
}
|
||||
if (!opened) {
|
||||
hint = formatControlUiSshHint({
|
||||
port,
|
||||
basePath,
|
||||
token: token || undefined,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
hint = "Browser launch disabled (--no-open). Use the URL above.";
|
||||
}
|
||||
|
||||
if (opened) {
|
||||
runtime.log("Opened in your browser. Keep that tab to control Clawdbot.");
|
||||
} else if (hint) {
|
||||
runtime.log(hint);
|
||||
}
|
||||
}
|
||||
@@ -233,6 +233,28 @@ export async function openUrl(url: string): Promise<boolean> {
|
||||
}
|
||||
}
|
||||
|
||||
export async function copyToClipboard(value: string): Promise<boolean> {
|
||||
const attempts: Array<{ argv: string[] }> = [
|
||||
{ argv: ["pbcopy"] },
|
||||
{ argv: ["xclip", "-selection", "clipboard"] },
|
||||
{ argv: ["wl-copy"] },
|
||||
{ argv: ["clip.exe"] }, // WSL / Windows
|
||||
{ argv: ["powershell", "-NoProfile", "-Command", "Set-Clipboard"] },
|
||||
];
|
||||
for (const attempt of attempts) {
|
||||
try {
|
||||
const result = await runCommandWithTimeout(attempt.argv, {
|
||||
timeoutMs: 3_000,
|
||||
input: value,
|
||||
});
|
||||
if (result.code === 0 && !result.killed) return true;
|
||||
} catch {
|
||||
// keep trying the next fallback
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export async function ensureWorkspaceAndSessions(
|
||||
workspaceDir: string,
|
||||
runtime: RuntimeEnv,
|
||||
|
||||
Reference in New Issue
Block a user