Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
213 changes: 111 additions & 102 deletions src/shared/components/NoAuthAccountCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -300,117 +300,126 @@ export default function NoAuthAccountCard({
)}

{!loading && allAccountIds.length > 0 && (
<div className="space-y-1 relative">
<div
data-testid="noauth-account-grid"
className="grid max-h-72 grid-cols-1 gap-1.5 overflow-y-auto pr-1 sm:grid-cols-2 lg:grid-cols-3"
>
{allAccountIds.map((id, i) => {
const proxy = getProxyForFingerprint(accountProxies, id);
return (
<div key={id} className="relative">
<div className="group flex items-center justify-between p-3 rounded-lg hover:bg-black/[0.02] dark:hover:bg-white/[0.02] transition-colors">
<div className="flex items-center gap-2 flex-1 min-w-0">
<span className="inline-flex items-center justify-center w-6 h-6 rounded-full bg-bg text-text-muted text-xs font-medium">
{i + 1}
</span>
<span className="font-mono text-xs text-text-muted truncate">
{id.slice(0, 12)}...
</span>
</div>
<div className="flex items-center gap-2">
<button
onClick={() => openProxyConfig(id)}
className="inline-flex items-center gap-1 px-1.5 py-0.5 rounded text-xs font-medium transition-colors"
title={
proxy ? `${proxy.type}://${proxy.host}:${proxy.port}` : "Configure proxy"
}
>
<span
className={`material-symbols-outlined text-[14px] ${proxy ? "text-blue-400" : "text-text-muted"}`}
>
{proxy ? "shield" : "shield"}
</span>
<span className={proxy ? "text-blue-400" : "text-text-muted"}>
{proxy ? `${proxy.type}://${proxy.host}` : "Proxy"}
</span>
</button>
<button
onClick={() => handleRemoveAccount(id)}
className="p-1 hover:bg-red-500/10 rounded text-text-muted hover:text-red-500 transition-colors opacity-0 group-hover:opacity-100"
>
<span className="material-symbols-outlined text-[16px]">delete</span>
</button>
</div>
</div>

{proxyAccountId === id && (
<div
ref={popoverRef}
className="absolute right-0 top-full z-50 mt-1 w-80 rounded-lg border border-black/10 dark:border-white/10 bg-surface shadow-lg p-4"
<div
key={id}
data-account-id={id}
className="group flex items-center gap-2 rounded-lg border border-border bg-bg/40 px-2.5 py-2 transition-colors hover:bg-black/[0.03] dark:hover:bg-white/[0.03]"
>
<span className="inline-flex h-5 w-5 shrink-0 items-center justify-center rounded-full bg-bg text-[10px] font-medium text-text-muted">
{i + 1}
</span>
<span className="min-w-0 flex-1 truncate font-mono text-xs text-text-muted">
{id.slice(0, 10)}…
</span>
<button
type="button"
onClick={() => openProxyConfig(id)}
className={`inline-flex h-6 w-6 shrink-0 items-center justify-center rounded transition-colors hover:bg-black/5 dark:hover:bg-white/5 ${proxy ? "text-blue-400" : "text-text-muted"}`}
title={
proxy
? `Proxy: ${proxy.type}://${proxy.host}:${proxy.port}`
: "Configure proxy"
}
aria-label={proxy ? `Proxy configured: ${proxy.host}` : "Configure proxy"}
>
<span
className="material-symbols-outlined text-[16px]"
style={proxy ? { fontVariationSettings: "'FILL' 1" } : undefined}
>
<p className="text-sm font-medium mb-3">Proxy for Account {i + 1}</p>
<div className="space-y-3">
<div className="flex gap-2">
<select
value={proxyType}
onChange={(e) => setProxyType(e.target.value)}
className="rounded-md border border-black/10 dark:border-white/10 bg-bg px-2.5 py-1.5 text-xs flex-shrink-0"
>
{PROXY_TYPES.map((t) => (
<option key={t.value} value={t.value}>
{t.label}
</option>
))}
</select>
<input
type="text"
value={proxyHost}
onChange={(e) => setProxyHost(e.target.value)}
placeholder="Host"
className="flex-1 rounded-md border border-black/10 dark:border-white/10 bg-bg px-2.5 py-1.5 text-xs"
/>
<input
type="text"
value={proxyPort}
onChange={(e) => setProxyPort(e.target.value)}
placeholder="Port"
className="w-16 rounded-md border border-black/10 dark:border-white/10 bg-bg px-2.5 py-1.5 text-xs"
/>
</div>
<input
type="text"
value={proxyUsername}
onChange={(e) => setProxyUsername(e.target.value)}
placeholder="Username (optional)"
className="w-full rounded-md border border-black/10 dark:border-white/10 bg-bg px-2.5 py-1.5 text-xs"
/>
<input
type="password"
value={proxyPassword}
onChange={(e) => setProxyPassword(e.target.value)}
placeholder="Password (optional)"
className="w-full rounded-md border border-black/10 dark:border-white/10 bg-bg px-2.5 py-1.5 text-xs"
/>
<div className="flex justify-end gap-2 pt-1">
<button
onClick={() => setProxyAccountId(null)}
className="rounded-md px-3 py-1.5 text-xs text-text-muted hover:text-text-main hover:bg-black/5 dark:hover:bg-white/5 transition-colors"
>
Cancel
</button>
<button
onClick={handleSaveProxy}
disabled={savingProxy}
className="rounded-md bg-primary/10 px-3 py-1.5 text-xs text-primary hover:bg-primary/20 disabled:opacity-50 transition-colors"
>
{savingProxy ? "Saving..." : "Save"}
</button>
</div>
</div>
</div>
)}
shield
</span>
</button>
<button
type="button"
onClick={() => handleRemoveAccount(id)}
className="shrink-0 rounded p-1 text-text-muted opacity-0 transition-colors hover:bg-red-500/10 hover:text-red-500 group-hover:opacity-100"
aria-label="Remove account"
>
<span className="material-symbols-outlined text-[16px]">delete</span>
</button>
</div>
);
})}
</div>
)}

{proxyAccountId && (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/40 p-4">
<div
ref={popoverRef}
className="w-80 max-w-full rounded-lg border border-black/10 bg-surface p-4 shadow-lg dark:border-white/10"
>
<p className="mb-3 text-sm font-medium">
Proxy for Account {allAccountIds.indexOf(proxyAccountId) + 1}
</p>
<div className="space-y-3">
<div className="flex gap-2">
<select
value={proxyType}
onChange={(e) => setProxyType(e.target.value)}
className="flex-shrink-0 rounded-md border border-black/10 bg-bg px-2.5 py-1.5 text-xs dark:border-white/10"
>
{PROXY_TYPES.map((t) => (
<option key={t.value} value={t.value}>
{t.label}
</option>
))}
</select>
<input
type="text"
value={proxyHost}
onChange={(e) => setProxyHost(e.target.value)}
placeholder="Host"
className="flex-1 rounded-md border border-black/10 bg-bg px-2.5 py-1.5 text-xs dark:border-white/10"
/>
<input
type="text"
value={proxyPort}
onChange={(e) => setProxyPort(e.target.value)}
placeholder="Port"
className="w-16 rounded-md border border-black/10 bg-bg px-2.5 py-1.5 text-xs dark:border-white/10"
/>
</div>
<input
type="text"
value={proxyUsername}
onChange={(e) => setProxyUsername(e.target.value)}
placeholder="Username (optional)"
className="w-full rounded-md border border-black/10 bg-bg px-2.5 py-1.5 text-xs dark:border-white/10"
/>
<input
type="password"
value={proxyPassword}
onChange={(e) => setProxyPassword(e.target.value)}
placeholder="Password (optional)"
className="w-full rounded-md border border-black/10 bg-bg px-2.5 py-1.5 text-xs dark:border-white/10"
/>
<div className="flex justify-end gap-2 pt-1">
<button
onClick={() => setProxyAccountId(null)}
className="rounded-md px-3 py-1.5 text-xs text-text-muted transition-colors hover:bg-black/5 hover:text-text-main dark:hover:bg-white/5"
>
Cancel
</button>
<button
onClick={handleSaveProxy}
disabled={savingProxy}
className="rounded-md bg-primary/10 px-3 py-1.5 text-xs text-primary transition-colors hover:bg-primary/20 disabled:opacity-50"
>
{savingProxy ? "Saving..." : "Save"}
</button>
</div>
</div>
</div>
</div>
)}
</div>
</Card>
);
Expand Down
145 changes: 145 additions & 0 deletions tests/unit/ui/noauth-account-card.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
// @vitest-environment jsdom
import React, { act } from "react";
import { createRoot } from "react-dom/client";
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";

// ── Import component (no module-level mocks needed) ──────────────────────────

const { default: NoAuthAccountCard } = await import(
"../../../src/shared/components/NoAuthAccountCard"
);

// ── Helpers ──────────────────────────────────────────────────────────────────

const PROVIDER_ID = "mimocode";

function makeFingerprints(n: number): string[] {
// Deterministic, distinguishable, > 10 chars each so the truncation is exercised.
return Array.from({ length: n }, (_, i) => `fp${String(i).padStart(2, "0")}deadbeefcafe`);
}

function setupFetch(fingerprints: string[]) {
const connections =
fingerprints.length > 0
? [
{
id: "conn-1",
provider: PROVIDER_ID,
providerSpecificData: { fingerprints, accountProxies: [] },
},
]
: [];
const mockFetch = vi.fn((url: string) => {
if (String(url).includes("/api/providers")) {
return Promise.resolve({
ok: true,
json: () => Promise.resolve({ connections }),
} as Response);
}
return Promise.resolve({ ok: true, json: () => Promise.resolve({}) } as Response);
});
vi.stubGlobal("fetch", mockFetch);
return mockFetch;
}

const containers: Array<{ root: ReturnType<typeof createRoot>; el: HTMLDivElement }> = [];

function renderCard() {
const el = document.createElement("div");
document.body.appendChild(el);
const root = createRoot(el);
let counter = 0;
act(() => {
root.render(
<NoAuthAccountCard
providerId={PROVIDER_ID}
providerName="MiMoCode"
generateAccountId={() => `gen-${counter++}`}
/>
);
});
containers.push({ root, el });
return el;
}

async function waitForCondition(fn: () => boolean, timeoutMs = 2000) {
const start = Date.now();
while (!fn()) {
if (Date.now() - start > timeoutMs) throw new Error("waitForCondition timed out");
await new Promise((r) => setTimeout(r, 20));
}
}

const grid = (el: HTMLElement) =>
el.querySelector<HTMLElement>("[data-testid='noauth-account-grid']");

// ── Lifecycle ─────────────────────────────────────────────────────────────────

beforeEach(() => {
vi.clearAllMocks();
});

afterEach(() => {
for (const { root, el } of containers.splice(0)) {
act(() => root.unmount());
el.remove();
}
vi.unstubAllGlobals();
});

// ── Tests ──────────────────────────────────────────────────────────────────────

describe("NoAuthAccountCard compact grid", () => {
it("renders the account list as a multi-column grid container", async () => {
setupFetch(makeFingerprints(15));
const el = renderCard();
await waitForCondition(() => grid(el) !== null);
const container = grid(el)!;
// Must be a responsive grid (not the old single-column stack) so 15 accounts stay compact.
expect(container.className).toContain("grid");
expect(container.className).toMatch(/sm:grid-cols-2|lg:grid-cols-3/);
});

it("renders one chip per account fingerprint", async () => {
setupFetch(makeFingerprints(15));
const el = renderCard();
await waitForCondition(() => grid(el)?.querySelectorAll("[data-account-id]").length === 15);
expect(grid(el)!.querySelectorAll("[data-account-id]").length).toBe(15);
});

it("constrains height with vertical scroll so a long list never grows unbounded", async () => {
setupFetch(makeFingerprints(15));
const el = renderCard();
await waitForCondition(() => grid(el) !== null);
const container = grid(el)!;
expect(container.className).toContain("overflow-y-auto");
expect(container.className).toMatch(/max-h-/);
});

it("keeps a proxy control on every chip (shield icon)", async () => {
setupFetch(makeFingerprints(3));
const el = renderCard();
await waitForCondition(() => grid(el)?.querySelectorAll("[data-account-id]").length === 3);
const chips = Array.from(grid(el)!.querySelectorAll<HTMLElement>("[data-account-id]"));
for (const chip of chips) {
const proxyBtn = chip.querySelector("button[title]");
expect(proxyBtn).toBeTruthy();
expect(proxyBtn!.textContent).toContain("shield");
}
});

it("opens the proxy editor when a chip's shield is clicked", async () => {
setupFetch(makeFingerprints(3));
const el = renderCard();
await waitForCondition(() => grid(el)?.querySelectorAll("[data-account-id]").length === 3);
const firstChip = grid(el)!.querySelector<HTMLElement>("[data-account-id]")!;
const proxyBtn = firstChip.querySelector<HTMLButtonElement>("button[title]")!;
act(() => {
proxyBtn.dispatchEvent(new MouseEvent("click", { bubbles: true }));
});
await waitForCondition(() => el.textContent?.includes("Proxy for Account 1") ?? false);
expect(el.textContent).toContain("Proxy for Account 1");
// The editor exposes a host input (the proxy configuration surface).
expect(el.querySelector("input[placeholder='Host']")).toBeTruthy();
});
});
Loading