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
81 changes: 68 additions & 13 deletions src/__tests__/unit/lib/ai/resolveSphinxToolTarget.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,41 @@ const USER_ID = "user-1";
const WS_A = { workspaceId: "cuid-a", slug: "alpha" };
const WS_B = { workspaceId: "cuid-b", slug: "beta" };

const CONNECTED_ROW_A = { id: "cuid-a", slug: "alpha" };
const CONNECTED_ROW_B = { id: "cuid-b", slug: "beta" };
type ConnectedRow = {
id: string;
slug: string;
name: string;
sphinxChatPubkey: string;
swarm: { name: string } | null;
};

const CONNECTED_ROW_A: ConnectedRow = {
id: "cuid-a",
slug: "alpha",
name: "Alpha",
sphinxChatPubkey: "pubkey-a",
swarm: { name: "swarm38" },
};
const CONNECTED_ROW_B: ConnectedRow = {
id: "cuid-b",
slug: "beta",
name: "Beta",
sphinxChatPubkey: "pubkey-b",
swarm: { name: "swarm39" },
};

function asTarget(row: ConnectedRow) {
return {
workspaceId: row.id,
workspaceSlug: row.slug,
sphinxChatPubkey: row.sphinxChatPubkey,
workspaceName: row.name,
...(row.swarm?.name ? { swarmDomain: `${row.swarm.name}.sphinx.chat` } : {}),
};
}

const TARGET_A = asTarget(CONNECTED_ROW_A);
const TARGET_B = asTarget(CONNECTED_ROW_B);

// Connection + write-access are folded into the single findMany so the
// resolve step is one round trip regardless of workspace count.
Expand Down Expand Up @@ -57,6 +90,13 @@ function scopedPredicate(ids: string[]) {
},
],
}),
select: {
id: true,
slug: true,
name: true,
sphinxChatPubkey: true,
swarm: { select: { name: true } },
},
});
}

Expand Down Expand Up @@ -107,10 +147,7 @@ describe("resolveSphinxToolTarget", () => {
currentCanvasRef: "",
});

expect(result).toEqual([
{ workspaceId: "cuid-a", workspaceSlug: "alpha" },
{ workspaceId: "cuid-b", workspaceSlug: "beta" },
]);
expect(result).toEqual([TARGET_A, TARGET_B]);
});

it("returns [] when no in-scope workspace is Sphinx-connected", async () => {
Expand Down Expand Up @@ -180,11 +217,29 @@ describe("resolveSphinxToolTarget", () => {
currentCanvasRef: "",
});

expect(result).toEqual([TARGET_A, TARGET_B]);
expect(db.workspace.findMany).toHaveBeenCalledWith(scopedPredicate(["cuid-a", "cuid-b"]));
});

it("omits swarmDomain when the workspace has no swarm row", async () => {
(db.workspace.findMany as ReturnType<typeof vi.fn>).mockResolvedValue([
{ ...CONNECTED_ROW_A, swarm: null },
]);

const result = await resolveSphinxToolTarget({
userId: USER_ID,
workspaceConfigs: [WS_A],
});

expect(result).toEqual([
{ workspaceId: "cuid-a", workspaceSlug: "alpha" },
{ workspaceId: "cuid-b", workspaceSlug: "beta" },
{
workspaceId: "cuid-a",
workspaceSlug: "alpha",
sphinxChatPubkey: "pubkey-a",
workspaceName: "Alpha",
},
]);
expect(db.workspace.findMany).toHaveBeenCalledWith(scopedPredicate(["cuid-a", "cuid-b"]));
expect(result[0]).not.toHaveProperty("swarmDomain");
});

it("drops VIEWER-only workspaces at resolve on org-root scope (DB filters them out)", async () => {
Expand All @@ -198,7 +253,7 @@ describe("resolveSphinxToolTarget", () => {
currentCanvasRef: "",
});

expect(result).toEqual([{ workspaceId: "cuid-a", workspaceSlug: "alpha" }]);
expect(result).toEqual([TARGET_A]);
expect(db.workspace.findMany).toHaveBeenCalledWith(scopedPredicate(["cuid-a", "cuid-b"]));
});

Expand All @@ -211,7 +266,7 @@ describe("resolveSphinxToolTarget", () => {
currentCanvasRef: "",
});

expect(result).toEqual([{ workspaceId: "cuid-a", workspaceSlug: "alpha" }]);
expect(result).toEqual([TARGET_A]);
const call = (db.workspace.findMany as ReturnType<typeof vi.fn>).mock.calls[0][0];
expect(call.where.OR).toContainEqual({ ownerId: USER_ID });
});
Expand Down Expand Up @@ -284,7 +339,7 @@ describe("resolveSphinxToolTarget", () => {
workspaceConfigs: [WS_A],
});

expect(result).toEqual([{ workspaceId: "cuid-a", workspaceSlug: "alpha" }]);
expect(result).toEqual([TARGET_A]);
expect(db.workspace.findMany).toHaveBeenCalledWith(scopedPredicate(["cuid-a"]));
});

Expand All @@ -295,7 +350,7 @@ describe("resolveSphinxToolTarget", () => {
currentCanvasRef: "ws:cuid-a",
});

expect(result).toEqual([{ workspaceId: "cuid-a", workspaceSlug: "alpha" }]);
expect(result).toEqual([TARGET_A]);
expect(db.workspace.findMany).toHaveBeenCalledWith(scopedPredicate(["cuid-a"]));
});
});
61 changes: 42 additions & 19 deletions src/__tests__/unit/lib/ai/runCanvasAgent-sphinx-merge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,39 @@

import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";

type ConnectedRow = {
id: string;
slug: string;
name: string;
sphinxChatPubkey: string;
swarm: { name: string } | null;
};

function connectedRow(
slug: string,
swarmName: string | null = `swarm-${slug}`,
): ConnectedRow {
return {
id: `cuid-${slug}`,
slug,
name: slug,
sphinxChatPubkey: `pubkey-${slug}`,
swarm: swarmName ? { name: swarmName } : null,
};
}

function expectedTarget(slug: string, swarmName: string | null = `swarm-${slug}`) {
return {
workspaceId: `cuid-${slug}`,
workspaceSlug: slug,
sphinxChatPubkey: `pubkey-${slug}`,
workspaceName: slug,
...(swarmName ? { swarmDomain: `${swarmName}.sphinx.chat` } : {}),
};
}

const { mockFindMany } = vi.hoisted(() => ({
mockFindMany: vi.fn<(...args: unknown[]) => Promise<Array<{ id: string; slug: string }>>>(
async () => [],
),
mockFindMany: vi.fn<(...args: unknown[]) => Promise<ConnectedRow[]>>(async () => []),
}));

const { mockValidateWorkspaceAccessById } = vi.hoisted(() => ({
Expand Down Expand Up @@ -177,7 +206,7 @@ describe("runCanvasAgent — send_sphinx_message merge", () => {
beforeEach(() => {
vi.clearAllMocks();
consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {});
mockFindMany.mockResolvedValue([{ id: "cuid-alpha", slug: "alpha" }]);
mockFindMany.mockResolvedValue([connectedRow("alpha")]);
mockValidateWorkspaceAccessById.mockResolvedValue({ canWrite: true });
});

Expand All @@ -190,7 +219,7 @@ describe("runCanvasAgent — send_sphinx_message merge", () => {

expect(buildSphinxTools).toHaveBeenCalledWith({
userId: "user-1",
targets: [{ workspaceId: "cuid-alpha", workspaceSlug: "alpha" }],
targets: [expectedTarget("alpha")],
actorLabel: undefined,
});
expect(toolNames()).toContain("send_sphinx_message");
Expand Down Expand Up @@ -250,10 +279,7 @@ describe("runCanvasAgent — send_sphinx_message merge", () => {
});

it("merges every writable connected conversation workspace on org-root scope (currentCanvasRef === '')", async () => {
mockFindMany.mockResolvedValue([
{ id: "cuid-alpha", slug: "alpha" },
{ id: "cuid-beta", slug: "beta" },
]);
mockFindMany.mockResolvedValue([connectedRow("alpha"), connectedRow("beta")]);

await runCanvasAgent(
opts({
Expand All @@ -264,10 +290,7 @@ describe("runCanvasAgent — send_sphinx_message merge", () => {

expect(buildSphinxTools).toHaveBeenCalledWith({
userId: "user-1",
targets: [
{ workspaceId: "cuid-alpha", workspaceSlug: "alpha" },
{ workspaceId: "cuid-beta", workspaceSlug: "beta" },
],
targets: [expectedTarget("alpha"), expectedTarget("beta")],
actorLabel: undefined,
});
expect(toolNames()).toContain("send_sphinx_message");
Expand All @@ -276,7 +299,7 @@ describe("runCanvasAgent — send_sphinx_message merge", () => {
it("drops VIEWER-only workspaces on org-root scope but still merges the writable one", async () => {
// Write access is part of the findMany predicate, so the VIEWER-only
// workspace (beta) never comes back from the query.
mockFindMany.mockResolvedValue([{ id: "cuid-alpha", slug: "alpha" }]);
mockFindMany.mockResolvedValue([connectedRow("alpha")]);

await runCanvasAgent(
opts({
Expand All @@ -287,7 +310,7 @@ describe("runCanvasAgent — send_sphinx_message merge", () => {

expect(buildSphinxTools).toHaveBeenCalledWith({
userId: "user-1",
targets: [{ workspaceId: "cuid-alpha", workspaceSlug: "alpha" }],
targets: [expectedTarget("alpha")],
actorLabel: undefined,
});
});
Expand All @@ -307,7 +330,7 @@ describe("runCanvasAgent — send_sphinx_message merge", () => {
});

it("scopes findMany to conversation workspace ids on org-root scope", async () => {
mockFindMany.mockResolvedValue([{ id: "cuid-alpha", slug: "alpha" }]);
mockFindMany.mockResolvedValue([connectedRow("alpha")]);

await runCanvasAgent(
opts({
Expand All @@ -326,7 +349,7 @@ describe("runCanvasAgent — send_sphinx_message merge", () => {
});

it("merges for multi-workspace only when currentCanvasRef is ws:<id> of a connected conversation workspace", async () => {
mockFindMany.mockResolvedValue([{ id: "cuid-alpha", slug: "alpha" }]);
mockFindMany.mockResolvedValue([connectedRow("alpha")]);

await runCanvasAgent(
opts({
Expand All @@ -337,7 +360,7 @@ describe("runCanvasAgent — send_sphinx_message merge", () => {

expect(buildSphinxTools).toHaveBeenCalledWith({
userId: "user-1",
targets: [{ workspaceId: "cuid-alpha", workspaceSlug: "alpha" }],
targets: [expectedTarget("alpha")],
actorLabel: undefined,
});
expect(toolNames()).toContain("send_sphinx_message");
Expand All @@ -364,7 +387,7 @@ describe("runCanvasAgent — send_sphinx_message merge", () => {

expect(buildSphinxTools).toHaveBeenCalledWith({
userId: "user-1",
targets: [{ workspaceId: "cuid-alpha", workspaceSlug: "alpha" }],
targets: [expectedTarget("alpha")],
actorLabel: "tom",
});
});
Expand Down
Loading
Loading