test(canvas): add ExternalConnectModal pure-helper coverage — 31 cases #847
@ -18,6 +18,109 @@
|
||||
import { useCallback, useState } from "react";
|
||||
import * as Dialog from "@radix-ui/react-dialog";
|
||||
|
||||
// ─── Pure fill helpers ────────────────────────────────────────────────────────
|
||||
// Each snippet is server-stamped with workspace_id + platform_url but leaves
|
||||
// AUTH_TOKEN as a placeholder. These helpers stamp the real token in so the
|
||||
// operator's copy-paste is truly ready-to-run. All are pure string ops.
|
||||
|
||||
export function fillPythonSnippet(
|
||||
snippet: string,
|
||||
authToken: string,
|
||||
): string {
|
||||
return snippet.replace(
|
||||
'AUTH_TOKEN = "<paste from create response>"',
|
||||
`AUTH_TOKEN = "${authToken}"`,
|
||||
);
|
||||
}
|
||||
|
||||
export function fillCurlSnippet(
|
||||
snippet: string,
|
||||
authToken: string,
|
||||
): string {
|
||||
return snippet.replace(
|
||||
'WORKSPACE_AUTH_TOKEN="<paste from create response>"',
|
||||
`WORKSPACE_AUTH_TOKEN="${authToken}"`,
|
||||
);
|
||||
}
|
||||
|
||||
export function fillChannelSnippet(
|
||||
snippet: string | undefined,
|
||||
authToken: string,
|
||||
): string | undefined {
|
||||
return snippet?.replace(
|
||||
'MOLECULE_WORKSPACE_TOKENS=<paste auth_token from create response>',
|
||||
`MOLECULE_WORKSPACE_TOKENS=${authToken}`,
|
||||
);
|
||||
}
|
||||
|
||||
export function fillUniversalMcpSnippet(
|
||||
snippet: string | undefined,
|
||||
authToken: string,
|
||||
): string | undefined {
|
||||
return snippet?.replace(
|
||||
'MOLECULE_WORKSPACE_TOKEN="<paste from create response>"',
|
||||
`MOLECULE_WORKSPACE_TOKEN="${authToken}"`,
|
||||
);
|
||||
}
|
||||
|
||||
export function fillHermesSnippet(
|
||||
snippet: string | undefined,
|
||||
authToken: string,
|
||||
): string | undefined {
|
||||
return snippet?.replace(
|
||||
'MOLECULE_WORKSPACE_TOKEN="<paste from create response>"',
|
||||
`MOLECULE_WORKSPACE_TOKEN="${authToken}"`,
|
||||
);
|
||||
}
|
||||
|
||||
export function fillCodexSnippet(
|
||||
snippet: string | undefined,
|
||||
authToken: string,
|
||||
): string | undefined {
|
||||
return snippet?.replace(
|
||||
'MOLECULE_WORKSPACE_TOKEN = "<paste from create response>"',
|
||||
`MOLECULE_WORKSPACE_TOKEN = "${authToken}"`,
|
||||
);
|
||||
}
|
||||
|
||||
export function fillOpenClawSnippet(
|
||||
snippet: string | undefined,
|
||||
authToken: string,
|
||||
): string | undefined {
|
||||
return snippet?.replace(
|
||||
'WORKSPACE_TOKEN="<paste from create response>"',
|
||||
`WORKSPACE_TOKEN="${authToken}"`,
|
||||
);
|
||||
}
|
||||
|
||||
/** Build the ordered tab list shown in the modal. Each tab only appears when
|
||||
* the platform supplies the corresponding snippet. */
|
||||
export function buildTabOrder(info: ExternalConnectionInfo): Tab[] {
|
||||
const tabs: Tab[] = [];
|
||||
const { filledUniversalMcp, filledChannel, filledHermes, filledCodex, filledOpenClaw } = buildFilledSnippets(info);
|
||||
if (filledUniversalMcp) tabs.push("mcp");
|
||||
tabs.push("python");
|
||||
if (filledChannel) tabs.push("claude");
|
||||
if (filledHermes) tabs.push("hermes");
|
||||
if (filledCodex) tabs.push("codex");
|
||||
if (filledOpenClaw) tabs.push("openclaw");
|
||||
tabs.push("curl", "fields");
|
||||
return tabs;
|
||||
}
|
||||
|
||||
/** Pre-fill all snippets from an info object. Exposed for testing. */
|
||||
export function buildFilledSnippets(info: ExternalConnectionInfo) {
|
||||
return {
|
||||
filledPython: fillPythonSnippet(info.python_snippet, info.auth_token),
|
||||
filledCurl: fillCurlSnippet(info.curl_register_template, info.auth_token),
|
||||
filledChannel: fillChannelSnippet(info.claude_code_channel_snippet, info.auth_token),
|
||||
filledUniversalMcp: fillUniversalMcpSnippet(info.universal_mcp_snippet, info.auth_token),
|
||||
filledHermes: fillHermesSnippet(info.hermes_channel_snippet, info.auth_token),
|
||||
filledCodex: fillCodexSnippet(info.codex_snippet, info.auth_token),
|
||||
filledOpenClaw: fillOpenClawSnippet(info.openclaw_snippet, info.auth_token),
|
||||
};
|
||||
}
|
||||
|
||||
type Tab = "python" | "curl" | "claude" | "mcp" | "hermes" | "codex" | "openclaw" | "fields";
|
||||
|
||||
export interface ExternalConnectionInfo {
|
||||
@ -102,54 +205,7 @@ export function ExternalConnectModal({ info, onClose }: Props) {
|
||||
|
||||
if (!info) return null;
|
||||
|
||||
// Python snippet is stamped server-side with workspace_id +
|
||||
// platform_url but leaves AUTH_TOKEN as a "<paste …>" placeholder
|
||||
// (that's what we're showing in the modal). Fill in the real
|
||||
// token here so the snippet the operator copies is truly ready-to-run.
|
||||
const filledPython = info.python_snippet.replace(
|
||||
'AUTH_TOKEN = "<paste from create response>"',
|
||||
`AUTH_TOKEN = "${info.auth_token}"`,
|
||||
);
|
||||
const filledCurl = info.curl_register_template.replace(
|
||||
'WORKSPACE_AUTH_TOKEN="<paste from create response>"',
|
||||
`WORKSPACE_AUTH_TOKEN="${info.auth_token}"`,
|
||||
);
|
||||
// The channel snippet asks the operator to paste the auth_token into
|
||||
// the .env file's MOLECULE_WORKSPACE_TOKENS field. Stamp it server-side
|
||||
// here so the copy-paste-block is truly ready-to-run.
|
||||
const filledChannel = info.claude_code_channel_snippet?.replace(
|
||||
'MOLECULE_WORKSPACE_TOKENS=<paste auth_token from create response>',
|
||||
`MOLECULE_WORKSPACE_TOKENS=${info.auth_token}`,
|
||||
);
|
||||
// Universal MCP snippet uses MOLECULE_WORKSPACE_TOKEN as the env-var
|
||||
// name passed through to molecule-mcp via `claude mcp add ... -- env
|
||||
// MOLECULE_WORKSPACE_TOKEN=...`. The placeholder must match the
|
||||
// template's literal — pre-2026-04-30 polish this looked for
|
||||
// WORKSPACE_AUTH_TOKEN (carryover from the curl tab), which silently
|
||||
// skipped the substitution and left "<paste from create response>"
|
||||
// visible in the operator's clipboard.
|
||||
const filledUniversalMcp = info.universal_mcp_snippet?.replace(
|
||||
'MOLECULE_WORKSPACE_TOKEN="<paste from create response>"',
|
||||
`MOLECULE_WORKSPACE_TOKEN="${info.auth_token}"`,
|
||||
);
|
||||
// Hermes channel snippet uses MOLECULE_WORKSPACE_TOKEN (same env-var
|
||||
// name as Universal MCP). Stamp the auth_token in so the operator's
|
||||
// copy-paste is fully ready-to-run.
|
||||
const filledHermes = info.hermes_channel_snippet?.replace(
|
||||
'MOLECULE_WORKSPACE_TOKEN="<paste from create response>"',
|
||||
`MOLECULE_WORKSPACE_TOKEN="${info.auth_token}"`,
|
||||
);
|
||||
// Codex + OpenClaw snippets carry the placeholder inside the
|
||||
// generated config block (TOML / JSON respectively). Stamp the
|
||||
// token in so the copy-paste is one less manual edit.
|
||||
const filledCodex = info.codex_snippet?.replace(
|
||||
'MOLECULE_WORKSPACE_TOKEN = "<paste from create response>"',
|
||||
`MOLECULE_WORKSPACE_TOKEN = "${info.auth_token}"`,
|
||||
);
|
||||
const filledOpenClaw = info.openclaw_snippet?.replace(
|
||||
'WORKSPACE_TOKEN="<paste from create response>"',
|
||||
`WORKSPACE_TOKEN="${info.auth_token}"`,
|
||||
);
|
||||
const { filledPython, filledCurl, filledChannel, filledUniversalMcp, filledHermes, filledCodex, filledOpenClaw } = buildFilledSnippets(info);
|
||||
|
||||
return (
|
||||
<Dialog.Root open onOpenChange={(o) => !o && onClose()}>
|
||||
@ -171,27 +227,7 @@ export function ExternalConnectModal({ info, onClose }: Props) {
|
||||
aria-label="Connection snippet format"
|
||||
className="mt-4 flex gap-1 border-b border-line"
|
||||
>
|
||||
{(() => {
|
||||
// Build the tab order dynamically. Claude Code first
|
||||
// (when offered) since it's the simplest setup; Python
|
||||
// SDK second (full register+heartbeat+inbound); Universal
|
||||
// MCP third (any MCP-aware runtime, outbound-only); curl
|
||||
// for one-shot register; Fields for raw values.
|
||||
// Tab order: Universal MCP first (default, runtime-
|
||||
// agnostic primitives), then runtime-specific channel/
|
||||
// SDK tabs, then curl + Fields. Each runtime tab only
|
||||
// appears when the platform supplies the snippet — no
|
||||
// dead "tab missing snippet" UX.
|
||||
const tabs: Tab[] = [];
|
||||
if (filledUniversalMcp) tabs.push("mcp");
|
||||
tabs.push("python");
|
||||
if (filledChannel) tabs.push("claude");
|
||||
if (filledHermes) tabs.push("hermes");
|
||||
if (filledCodex) tabs.push("codex");
|
||||
if (filledOpenClaw) tabs.push("openclaw");
|
||||
tabs.push("curl", "fields");
|
||||
return tabs;
|
||||
})().map((t) => (
|
||||
{buildTabOrder(info).map((t) => (
|
||||
<button
|
||||
key={t}
|
||||
type="button"
|
||||
|
||||
275
canvas/src/components/__tests__/ExternalConnectModal.test.tsx
Normal file
275
canvas/src/components/__tests__/ExternalConnectModal.test.tsx
Normal file
@ -0,0 +1,275 @@
|
||||
'use client';
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import {
|
||||
fillPythonSnippet,
|
||||
fillCurlSnippet,
|
||||
fillChannelSnippet,
|
||||
fillUniversalMcpSnippet,
|
||||
fillHermesSnippet,
|
||||
fillCodexSnippet,
|
||||
fillOpenClawSnippet,
|
||||
buildFilledSnippets,
|
||||
buildTabOrder,
|
||||
ExternalConnectionInfo,
|
||||
} from '../ExternalConnectModal';
|
||||
|
||||
// ─── fillPythonSnippet ───────────────────────────────────────────────────────
|
||||
|
||||
describe('fillPythonSnippet', () => {
|
||||
it('stamps auth_token into the AUTH_TOKEN placeholder', () => {
|
||||
const input =
|
||||
'AUTH_TOKEN = "<paste from create response>"\n' +
|
||||
'PLATFORM_URL = "http://localhost:8080"';
|
||||
const got = fillPythonSnippet(input, 'tok-abc123');
|
||||
expect(got).toContain('AUTH_TOKEN = "tok-abc123"');
|
||||
// Original placeholder is gone
|
||||
expect(got).not.toContain('<paste from create response>');
|
||||
});
|
||||
|
||||
it('leaves other lines untouched', () => {
|
||||
const input = 'PLATFORM_URL = "http://localhost:8080"\nAUTH_TOKEN = "<paste from create response>"';
|
||||
const got = fillPythonSnippet(input, 'tok-xyz');
|
||||
expect(got).toContain('PLATFORM_URL = "http://localhost:8080"');
|
||||
});
|
||||
|
||||
it('handles empty token', () => {
|
||||
const input = 'AUTH_TOKEN = "<paste from create response>"';
|
||||
const got = fillPythonSnippet(input, '');
|
||||
expect(got).toContain('AUTH_TOKEN = ""');
|
||||
});
|
||||
});
|
||||
|
||||
// ─── fillCurlSnippet ─────────────────────────────────────────────────────────
|
||||
|
||||
describe('fillCurlSnippet', () => {
|
||||
it('stamps auth_token into WORKSPACE_AUTH_TOKEN placeholder', () => {
|
||||
const input = 'WORKSPACE_AUTH_TOKEN="<paste from create response>"';
|
||||
const got = fillCurlSnippet(input, 'tok-curl');
|
||||
expect(got).toContain('WORKSPACE_AUTH_TOKEN="tok-curl"');
|
||||
expect(got).not.toContain('<paste from create response>');
|
||||
});
|
||||
});
|
||||
|
||||
// ─── fillChannelSnippet ─────────────────────────────────────────────────────
|
||||
|
||||
describe('fillChannelSnippet', () => {
|
||||
it('stamps token into MOLECULE_WORKSPACE_TOKENS placeholder', () => {
|
||||
const input = 'MOLECULE_WORKSPACE_TOKENS=<paste auth_token from create response>';
|
||||
const got = fillChannelSnippet(input, 'tok-channel');
|
||||
expect(got).toContain('MOLECULE_WORKSPACE_TOKENS=tok-channel');
|
||||
});
|
||||
|
||||
it('returns undefined when snippet is undefined', () => {
|
||||
expect(fillChannelSnippet(undefined, 'tok')).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
// ─── fillUniversalMcpSnippet ───────────────────────────────────────────────
|
||||
|
||||
describe('fillUniversalMcpSnippet', () => {
|
||||
it('stamps token with double-quoted value', () => {
|
||||
const input = 'MOLECULE_WORKSPACE_TOKEN="<paste from create response>"';
|
||||
const got = fillUniversalMcpSnippet(input, 'tok-mcp');
|
||||
expect(got).toContain('MOLECULE_WORKSPACE_TOKEN="tok-mcp"');
|
||||
});
|
||||
|
||||
it('returns undefined when snippet is undefined', () => {
|
||||
expect(fillUniversalMcpSnippet(undefined, 'tok')).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
// ─── fillHermesSnippet ─────────────────────────────────────────────────────
|
||||
|
||||
describe('fillHermesSnippet', () => {
|
||||
it('stamps token with double-quoted value', () => {
|
||||
const input = 'MOLECULE_WORKSPACE_TOKEN="<paste from create response>"';
|
||||
const got = fillHermesSnippet(input, 'tok-hermes');
|
||||
expect(got).toContain('MOLECULE_WORKSPACE_TOKEN="tok-hermes"');
|
||||
});
|
||||
|
||||
it('returns undefined when snippet is undefined', () => {
|
||||
expect(fillHermesSnippet(undefined, 'tok')).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
// ─── fillCodexSnippet ──────────────────────────────────────────────────────
|
||||
|
||||
describe('fillCodexSnippet', () => {
|
||||
it('uses TOML spacing (space around equals)', () => {
|
||||
const input = 'MOLECULE_WORKSPACE_TOKEN = "<paste from create response>"';
|
||||
const got = fillCodexSnippet(input, 'tok-codex');
|
||||
expect(got).toContain('MOLECULE_WORKSPACE_TOKEN = "tok-codex"');
|
||||
expect(got).not.toContain('<paste from create response>');
|
||||
});
|
||||
|
||||
it('returns undefined when snippet is undefined', () => {
|
||||
expect(fillCodexSnippet(undefined, 'tok')).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
// ─── fillOpenClawSnippet ───────────────────────────────────────────────────
|
||||
|
||||
describe('fillOpenClawSnippet', () => {
|
||||
it('stamps token with WORKSPACE_TOKEN key name', () => {
|
||||
const input = 'WORKSPACE_TOKEN="<paste from create response>"';
|
||||
const got = fillOpenClawSnippet(input, 'tok-oc');
|
||||
expect(got).toContain('WORKSPACE_TOKEN="tok-oc"');
|
||||
expect(got).not.toContain('<paste from create response>');
|
||||
});
|
||||
|
||||
it('returns undefined when snippet is undefined', () => {
|
||||
expect(fillOpenClawSnippet(undefined, 'tok')).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
// ─── buildFilledSnippets ────────────────────────────────────────────────────
|
||||
|
||||
describe('buildFilledSnippets', () => {
|
||||
const makeInfo = (overrides: Partial<ExternalConnectionInfo> = {}): ExternalConnectionInfo =>
|
||||
({
|
||||
workspace_id: 'ws-1',
|
||||
platform_url: 'http://localhost:8080',
|
||||
auth_token: 'tok-test',
|
||||
registry_endpoint: 'http://localhost:8080/registry/register',
|
||||
heartbeat_endpoint: 'http://localhost:8080/registry/heartbeat',
|
||||
python_snippet: 'AUTH_TOKEN = "<paste from create response>"',
|
||||
curl_register_template: 'WORKSPACE_AUTH_TOKEN="<paste from create response>"',
|
||||
...overrides,
|
||||
});
|
||||
|
||||
it('fills python snippet', () => {
|
||||
const { filledPython } = buildFilledSnippets(makeInfo());
|
||||
expect(filledPython).toContain('tok-test');
|
||||
});
|
||||
|
||||
it('fills curl snippet', () => {
|
||||
const { filledCurl } = buildFilledSnippets(makeInfo());
|
||||
expect(filledCurl).toContain('tok-test');
|
||||
});
|
||||
|
||||
it('fills claude_code_channel_snippet when present', () => {
|
||||
const info = makeInfo({
|
||||
claude_code_channel_snippet: 'MOLECULE_WORKSPACE_TOKENS=<paste auth_token from create response>',
|
||||
});
|
||||
const { filledChannel } = buildFilledSnippets(info);
|
||||
expect(filledChannel).toContain('tok-test');
|
||||
});
|
||||
|
||||
it('fills universal_mcp_snippet when present', () => {
|
||||
const info = makeInfo({
|
||||
universal_mcp_snippet: 'MOLECULE_WORKSPACE_TOKEN="<paste from create response>"',
|
||||
});
|
||||
const { filledUniversalMcp } = buildFilledSnippets(info);
|
||||
expect(filledUniversalMcp).toContain('tok-test');
|
||||
});
|
||||
|
||||
it('fills hermes_channel_snippet when present', () => {
|
||||
const info = makeInfo({
|
||||
hermes_channel_snippet: 'MOLECULE_WORKSPACE_TOKEN="<paste from create response>"',
|
||||
});
|
||||
const { filledHermes } = buildFilledSnippets(info);
|
||||
expect(filledHermes).toContain('tok-test');
|
||||
});
|
||||
|
||||
it('fills codex_snippet when present', () => {
|
||||
const info = makeInfo({
|
||||
codex_snippet: 'MOLECULE_WORKSPACE_TOKEN = "<paste from create response>"',
|
||||
});
|
||||
const { filledCodex } = buildFilledSnippets(info);
|
||||
expect(filledCodex).toContain('tok-test');
|
||||
});
|
||||
|
||||
it('fills openclaw_snippet when present', () => {
|
||||
const info = makeInfo({
|
||||
openclaw_snippet: 'WORKSPACE_TOKEN="<paste from create response>"',
|
||||
});
|
||||
const { filledOpenClaw } = buildFilledSnippets(info);
|
||||
expect(filledOpenClaw).toContain('tok-test');
|
||||
});
|
||||
});
|
||||
|
||||
// ─── buildTabOrder ──────────────────────────────────────────────────────────
|
||||
|
||||
describe('buildTabOrder', () => {
|
||||
const makeInfo = (overrides: Partial<ExternalConnectionInfo> = {}): ExternalConnectionInfo =>
|
||||
({
|
||||
workspace_id: 'ws-1',
|
||||
platform_url: 'http://localhost:8080',
|
||||
auth_token: 'tok-test',
|
||||
registry_endpoint: 'http://localhost:8080/registry/register',
|
||||
heartbeat_endpoint: 'http://localhost:8080/registry/heartbeat',
|
||||
python_snippet: 'AUTH_TOKEN = "<paste from create response>"',
|
||||
curl_register_template: 'WORKSPACE_AUTH_TOKEN="<paste from create response>"',
|
||||
...overrides,
|
||||
});
|
||||
|
||||
it('python is always present', () => {
|
||||
const tabs = buildTabOrder(makeInfo());
|
||||
expect(tabs).toContain('python');
|
||||
});
|
||||
|
||||
it('curl and fields are always present', () => {
|
||||
const tabs = buildTabOrder(makeInfo());
|
||||
expect(tabs).toContain('curl');
|
||||
expect(tabs).toContain('fields');
|
||||
});
|
||||
|
||||
it('mcp first when universal_mcp_snippet is present', () => {
|
||||
const tabs = buildTabOrder(makeInfo({
|
||||
universal_mcp_snippet: 'MOLECULE_WORKSPACE_TOKEN="<paste from create response>"',
|
||||
}));
|
||||
expect(tabs[0]).toBe('mcp');
|
||||
});
|
||||
|
||||
it('python first when universal_mcp_snippet is absent', () => {
|
||||
const tabs = buildTabOrder(makeInfo());
|
||||
expect(tabs[0]).toBe('python');
|
||||
});
|
||||
|
||||
it('mcp excluded when universal_mcp_snippet is absent', () => {
|
||||
const tabs = buildTabOrder(makeInfo());
|
||||
expect(tabs).not.toContain('mcp');
|
||||
});
|
||||
|
||||
it('includes claude when claude_code_channel_snippet is present', () => {
|
||||
const tabs = buildTabOrder(makeInfo({
|
||||
claude_code_channel_snippet: 'MOLECULE_WORKSPACE_TOKENS=<paste auth_token from create response>',
|
||||
}));
|
||||
expect(tabs).toContain('claude');
|
||||
});
|
||||
|
||||
it('includes hermes when hermes_channel_snippet is present', () => {
|
||||
const tabs = buildTabOrder(makeInfo({
|
||||
hermes_channel_snippet: 'MOLECULE_WORKSPACE_TOKEN="<paste from create response>"',
|
||||
}));
|
||||
expect(tabs).toContain('hermes');
|
||||
});
|
||||
|
||||
it('includes codex when codex_snippet is present', () => {
|
||||
const tabs = buildTabOrder(makeInfo({
|
||||
codex_snippet: 'MOLECULE_WORKSPACE_TOKEN = "<paste from create response>"',
|
||||
}));
|
||||
expect(tabs).toContain('codex');
|
||||
});
|
||||
|
||||
it('includes openclaw when openclaw_snippet is present', () => {
|
||||
const tabs = buildTabOrder(makeInfo({
|
||||
openclaw_snippet: 'WORKSPACE_TOKEN="<paste from create response>"',
|
||||
}));
|
||||
expect(tabs).toContain('openclaw');
|
||||
});
|
||||
|
||||
it('all optional tabs at once: full house', () => {
|
||||
const tabs = buildTabOrder(makeInfo({
|
||||
universal_mcp_snippet: 'MOLECULE_WORKSPACE_TOKEN="<paste from create response>"',
|
||||
claude_code_channel_snippet: 'MOLECULE_WORKSPACE_TOKENS=<paste auth_token from create response>',
|
||||
hermes_channel_snippet: 'MOLECULE_WORKSPACE_TOKEN="<paste from create response>"',
|
||||
codex_snippet: 'MOLECULE_WORKSPACE_TOKEN = "<paste from create response>"',
|
||||
openclaw_snippet: 'WORKSPACE_TOKEN="<paste from create response>"',
|
||||
}));
|
||||
expect(tabs).toEqual([
|
||||
'mcp', 'python', 'claude', 'hermes', 'codex', 'openclaw', 'curl', 'fields',
|
||||
]);
|
||||
});
|
||||
});
|
||||
Loading…
Reference in New Issue
Block a user