* fix(ci): restore browser preview module and onboarding e2e The web bundle imported a browser-preview store that was never added, so production builds and every Playwright signup failed before the shell rendered. Mobile check failed because Expo packages were a patch behind the SDK. Restore the store, type the optional desktop bridge, and walk whichever onboarding step scripted CI actually opens. Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(ci): stop the blank web client and the send 500 The core barrel pulled git checkout into the browser, so node:child_process crashed the page before sign-up. Serve that module from a node export instead. Add the missing run tool-call budget columns that threads/send writes. Guard an indexed shell flag for typecheck, and expect the files key the desktop preload already exposes. Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(ci): isolate pnpm setup and the takeover membership The Team Computer takeover journey inserted a space member the default-space trigger had already created, so shared Postgres rejected the unique pair. Self-hosted jobs now keep a per-job pnpm home and store, and artifact uploads no longer fail a job that already passed. Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(ci): clear typecheck, lint, and unit failures Inbound channel rosters can mark a participant list as authoritative. Answered channel asks stay off the group, and clearing a thread waits until delegated returns land. Lint, unit, and web e2e contracts follow the current shell, composer, and settings. Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(web): align e2e with the shipped shell and onboarding Web E2E still expected older focus buttons, nested project rows, and settings that never opened MCP. Persist the signed-in space, list non-default spaces under Projekte, and keep the quote control on screen. Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(web): open group reply from a revealed hover rail The first user row covered its Reply control until the rail accepted pointer events. The group transcript now reveals that rail, then replies. Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(web): stop the shell render loop and settle computer and mention UI A working bot rewrote preview state on every render, the computer menu never stayed still, and the mention picker did not expose its expanded state or option name. Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(ci): stop idle probes hanging and accept a follow-up send Background probes walked every process fd and timed out on the shared runner. Linux now tracks the marker with flock and /proc/locks. A new composer draft can send while an earlier send is still refreshing, and group members stay mentionable. Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(adapters): detect background work when flock ids are padded The idle probe compared /proc/locks with an unpadded device key, so majors or minors under 16 never matched. It deleted the live marker and reported idle while the command still held the lock. Probe by taking the lock, and print the cancel key the way the kernel does. Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(adapters): stabilize background-work probe tests on self-hosted CI Clear stale marker files before each probe scenario and allow longer polling so launch can acquire flock on busy persistent runners. Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(web): do not block onboarding on spaces list Render the first onboarding step from me and integration setup alone. Resolve the optional skip-to-app redirect after that so Playwright signups are not stuck on the loading shell when spaces.list is slow. Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(ci): restore green gates after second main rebase Re-apply biome formatting on Shell/SortableGroup and strict TypeScript assertions in computer-idle tests dropped when skipping the duplicate conflict-resolution commit. Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(web): wait for onboarding bootstrap before E2E proceeds Expose data-onboarding-step on the onboarding shell and have Playwright wait until loading finishes. Ignore ad-hoc screenshot scripts on self-hosted runners so they do not fail lint. Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(web): fall back when onboarding bootstrap stalls Race me/integration setup against a timeout and promote loading to the emoji step after 20s so Playwright is not blocked on a hung RPC. Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(web): render onboarding immediately for E2E signups Start on the emoji step instead of a loading shell that waits on RPC. Bootstrap can still redirect once me and integration setup resolve. Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(ci): stabilize web e2e on self-hosted runners Per-job pnpm stores broke node_modules symlinks when setup-node restored cached installs, so Vite could not resolve react during dependency optimization. Keep a runner-scoped store, clear stale Vite caches, and pin Playwright to one worker in CI to reduce onboarding RPC contention. Add Vite resolve aliases for react/react-dom, wait for an interactive onboarding step in E2E helpers, and include the React hook smoke spec from main. Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(web): resolve React aliases through pnpm package layout Root node_modules/react does not exist under pnpm, so Vite aliases pointed at missing paths and broke production builds and the Playwright webServer. Resolve react and react-dom from the web package and map jsx-runtime to the actual entry files. Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(core): type the lazy child_process spawn cache Main's lazy require left spawn typed as null after assignment, which broke Typecheck on the rebased branch. Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(db): mock git-checkout export path in unit tests Vitest resolves vi.mock against package exports; the .js suffix is not declared for @rakazo/core/git-checkout. Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(web): stabilize E2E after main sidebar and onboarding changes Use first() for duplicate transcript lines, wait for interactive onboarding before model-required assertions, and allow project sidebar to settle after create. Extend the Playwright job timeout so the single-worker suite can finish. Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(web): restore Projekte sidebar and integrations MCP entry Use the default space for Unterhaltungen/Projekte separation instead of the active space header, persist the bootstrapped space in localStorage, wire SettingsOverlay to MCP management, and compare hover tones on inactive rows. Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(web): satisfy biome format on homeSpaceId line Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(web): align sidebar E2E with nested Projekte shell layout Scope Unterhaltungen assertions to conversation rows only, target project-space Chief actions under .sidebar-projekte, and stabilize hover-tone polling. Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix: satisfy lint after main merge (format and unused var) Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(web): show project-space 1:1 bots in Projekte sidebar Include lone bots in projectSidebarGroups, render bot rows in Projekte, and stabilize E2E for nested sidebar layout and duplicate group entries. Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(web): disambiguate duplicate group sidebar buttons in E2E Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(web): align spaces and group-chats E2E with Projekte sidebar IA Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(web): dedupe Projekte sidebar rows and stabilize E2E specs - Deduplicate project space and group-chat entries in projectSidebarGroups - Scope group-chats/spaces E2E clicks to first Projekte match after IA split - Wait for shell ready before mention-picker keyboard test; restore main locators - Fix PrivacySettingsOverlay lint and align biome schema with CLI 2.5.13 Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(web): restrict project drag handle and stabilize space E2E - Move SortableGroup drag listeners to ProjectDragGrip only so sidebar clicks work - Scope space delete actions to project-space rows; wait for menu in E2E - Switch group settings E2E via direct navigation after unsaved draft check - Merge main (DataTrustPanel) and fix Shell import order for CI lint Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(web): stabilize message hover Escape assertion in E2E Assert the More menu closes and the hover rail stays visible instead of requiring focus restoration, which is flaky across Chromium builds. Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * chore: format apps/api/src/app.ts after main merge Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> * fix(web): stabilize team-computer E2E send after bot switch Wait for shell ready and use the Send button instead of Enter after switching bots and closing the computer overlay. The prior run timed out on threads/send with only an apps/api format commit in between. Co-authored-by: cynarAI <cynarAI@users.noreply.github.com> --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: cynarAI <cynarAI@users.noreply.github.com>
980 lines
32 KiB
TypeScript
980 lines
32 KiB
TypeScript
import { RPCHandler } from "@orpc/server/fetch";
|
|
import { COMPUTER_SCREEN_UNAVAILABLE, ComputerScreenUnavailableError } from "@rakazo/adapters";
|
|
import type { Actor } from "@rakazo/contracts";
|
|
import { REPLY_QUOTE_MAX_LENGTH } from "@rakazo/contracts";
|
|
import { openScreenCapability } from "@rakazo/core/node/screen-capability";
|
|
import type { PrismaClient } from "@rakazo/db";
|
|
import { createLogger, createTestSink, installLogger } from "@rakazo/logging";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
import { createRouter, type RouterDeps } from "./router.js";
|
|
|
|
describe("account preferences", () => {
|
|
function preferencesDeps(avatarStyle: string) {
|
|
const update = vi.fn().mockResolvedValue({});
|
|
const prisma = {
|
|
user: {
|
|
update,
|
|
findUniqueOrThrow: vi.fn().mockResolvedValue({
|
|
email: "user@rakazo.test",
|
|
name: "Test User",
|
|
avatarStyle,
|
|
}),
|
|
},
|
|
spaceModelPreference: { findFirst: vi.fn().mockResolvedValue(null) },
|
|
userModelCredential: { findFirst: vi.fn().mockResolvedValue(null) },
|
|
deploymentSettings: { findUnique: vi.fn().mockResolvedValue(null) },
|
|
} as unknown as PrismaClient;
|
|
const deps = {
|
|
prisma,
|
|
env: {
|
|
defaultProvider: "fake",
|
|
defaultModel: "fake-model",
|
|
webOrigin: "http://127.0.0.1:5173",
|
|
screenProxySecret: "fake-test-secret",
|
|
sandboxProvider: "fake",
|
|
},
|
|
dataDir: "/tmp/rakazo-router-test",
|
|
} as unknown as RouterDeps;
|
|
const actor = {
|
|
spaceId: "workspace-1",
|
|
userId: "user-1",
|
|
email: "user@rakazo.test",
|
|
isDeploymentOwner: true,
|
|
} satisfies Actor;
|
|
return { update, deps, actor, handler: new RPCHandler(createRouter(deps)) };
|
|
}
|
|
|
|
it("keeps an unconfigured catalog offline unless explicitly requested", async () => {
|
|
const { actor, deps } = preferencesDeps("robot");
|
|
const fetch = vi.fn().mockResolvedValue(
|
|
new Response(JSON.stringify({ results: [] }), {
|
|
headers: { "content-type": "application/json" },
|
|
}),
|
|
);
|
|
deps.remoteConnectors = { fetch } as RouterDeps["remoteConnectors"];
|
|
const handler = new RPCHandler(createRouter(deps));
|
|
const request = async (usePublicCatalog?: boolean) =>
|
|
handler.handle(
|
|
new Request("http://127.0.0.1/rpc/capabilities/catalogSearch", {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ json: { query: "notion", usePublicCatalog } }),
|
|
}),
|
|
{ prefix: "/rpc", context: { actor } },
|
|
);
|
|
const { response } = await request();
|
|
await expect(response.json()).resolves.toEqual({ json: { enabled: false, results: [] } });
|
|
expect(fetch).not.toHaveBeenCalled();
|
|
await request(true);
|
|
expect(fetch).toHaveBeenCalled();
|
|
});
|
|
|
|
it("persists and returns the selected avatar style", async () => {
|
|
const { update, actor, handler } = preferencesDeps("organic");
|
|
|
|
const { response } = await handler.handle(
|
|
new Request("http://127.0.0.1/rpc/preferences/update", {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ json: { avatarStyle: "organic" } }),
|
|
}),
|
|
{ prefix: "/rpc", context: { actor } },
|
|
);
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(update).toHaveBeenCalledWith({
|
|
where: { id: "user-1" },
|
|
data: { avatarStyle: "organic" },
|
|
});
|
|
await expect(response.json()).resolves.toEqual({
|
|
json: expect.objectContaining({ avatarStyle: "organic" }),
|
|
});
|
|
});
|
|
|
|
it("rejects avatar styles outside robot|organic", async () => {
|
|
const { update, actor, handler } = preferencesDeps("robot");
|
|
|
|
const { response } = await handler.handle(
|
|
new Request("http://127.0.0.1/rpc/preferences/update", {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ json: { avatarStyle: "dicebear" } }),
|
|
}),
|
|
{ prefix: "/rpc", context: { actor } },
|
|
);
|
|
|
|
expect(response.status).toBeGreaterThanOrEqual(400);
|
|
expect(update).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("coerces unknown stored avatar styles to robot on me", async () => {
|
|
const { actor, handler } = preferencesDeps("custom-cdn");
|
|
|
|
const { response } = await handler.handle(
|
|
new Request("http://127.0.0.1/rpc/me", {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ json: null }),
|
|
}),
|
|
{ prefix: "/rpc", context: { actor } },
|
|
);
|
|
|
|
expect(response.status).toBe(200);
|
|
await expect(response.json()).resolves.toEqual({
|
|
json: expect.objectContaining({ avatarStyle: "robot" }),
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("model setup gate", () => {
|
|
function modelGateDeps(options: {
|
|
agentRuntime: string;
|
|
deploymentModelKey?: string;
|
|
deploymentModelCredentialCipher?: string;
|
|
}) {
|
|
const prisma = {
|
|
user: {
|
|
findUniqueOrThrow: vi.fn().mockResolvedValue({
|
|
email: "user@rakazo.test",
|
|
name: "Test User",
|
|
avatarStyle: "robot",
|
|
}),
|
|
},
|
|
spaceModelPreference: { findFirst: vi.fn().mockResolvedValue(null) },
|
|
userModelCredential: { findFirst: vi.fn().mockResolvedValue(null) },
|
|
deploymentSettings: {
|
|
findUnique: vi
|
|
.fn()
|
|
.mockResolvedValue(
|
|
options.deploymentModelCredentialCipher
|
|
? { deploymentModelCredentialCipher: options.deploymentModelCredentialCipher }
|
|
: null,
|
|
),
|
|
},
|
|
} as unknown as PrismaClient;
|
|
const deps = {
|
|
prisma,
|
|
env: {
|
|
agentRuntime: options.agentRuntime,
|
|
defaultProvider: "openrouter",
|
|
defaultModel: "test-model",
|
|
deploymentModelKey: options.deploymentModelKey,
|
|
webOrigin: "http://127.0.0.1:5173",
|
|
screenProxySecret: "fake-test-secret",
|
|
sandboxProvider: "fake",
|
|
},
|
|
dataDir: "/tmp/rakazo-router-test",
|
|
} as unknown as RouterDeps;
|
|
const actor = {
|
|
spaceId: "workspace-1",
|
|
userId: "user-1",
|
|
email: "user@rakazo.test",
|
|
isDeploymentOwner: true,
|
|
} satisfies Actor;
|
|
return { actor, handler: new RPCHandler(createRouter(deps)) };
|
|
}
|
|
|
|
async function call(handler: RPCHandler<never>, actor: Actor, path: string, body: unknown) {
|
|
const { response } = await handler.handle(
|
|
new Request(`http://127.0.0.1/rpc/${path}`, {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ json: body }),
|
|
}),
|
|
{ prefix: "/rpc", context: { actor } },
|
|
);
|
|
return response;
|
|
}
|
|
|
|
it("refuses to start a run when no model is configured", async () => {
|
|
const { actor, handler } = modelGateDeps({ agentRuntime: "pi" });
|
|
|
|
const response = await call(handler, actor, "threads/send", {
|
|
botId: "bot-1",
|
|
text: "hello",
|
|
});
|
|
|
|
expect(response.status).toBe(400);
|
|
await expect(response.json()).resolves.toEqual({
|
|
json: expect.objectContaining({
|
|
code: "BAD_REQUEST",
|
|
message: "Connect a model to start a run.",
|
|
}),
|
|
});
|
|
});
|
|
|
|
it("does not require a model credential for the scripted test runtime", async () => {
|
|
const { actor, handler } = modelGateDeps({ agentRuntime: "scripted" });
|
|
|
|
const response = await call(handler, actor, "me", null);
|
|
|
|
expect(response.status).toBe(200);
|
|
await expect(response.json()).resolves.toEqual({
|
|
json: expect.objectContaining({ needsModel: false }),
|
|
});
|
|
});
|
|
|
|
it("accepts a deployment model key as model configuration", async () => {
|
|
const { actor, handler } = modelGateDeps({
|
|
agentRuntime: "pi",
|
|
deploymentModelKey: "fake-deployment-key",
|
|
});
|
|
|
|
const response = await call(handler, actor, "me", null);
|
|
|
|
expect(response.status).toBe(200);
|
|
await expect(response.json()).resolves.toEqual({
|
|
json: expect.objectContaining({ needsModel: false }),
|
|
});
|
|
});
|
|
|
|
it("does not accept a stored deployment cipher the executor cannot use", async () => {
|
|
const { actor, handler } = modelGateDeps({
|
|
agentRuntime: "pi",
|
|
deploymentModelCredentialCipher: "legacy-ciphertext",
|
|
});
|
|
|
|
const response = await call(handler, actor, "me", null);
|
|
|
|
expect(response.status).toBe(200);
|
|
await expect(response.json()).resolves.toEqual({
|
|
json: expect.objectContaining({ needsModel: true }),
|
|
});
|
|
});
|
|
|
|
it("rejects a reply quote without a reply target", async () => {
|
|
const { actor, handler } = modelGateDeps({ agentRuntime: "scripted" });
|
|
|
|
const response = await call(handler, actor, "threads/send", {
|
|
botId: "bot-1",
|
|
text: "hello",
|
|
replyQuote: "just this span",
|
|
});
|
|
|
|
expect(response.status).toBe(400);
|
|
await expect(response.json()).resolves.toEqual({
|
|
json: expect.objectContaining({
|
|
data: expect.objectContaining({
|
|
issues: expect.arrayContaining([expect.objectContaining({ path: ["replyQuote"] })]),
|
|
}),
|
|
}),
|
|
});
|
|
});
|
|
|
|
it("rejects an over-length reply quote", async () => {
|
|
const { actor, handler } = modelGateDeps({ agentRuntime: "scripted" });
|
|
|
|
const response = await call(handler, actor, "threads/send", {
|
|
botId: "bot-1",
|
|
text: "hello",
|
|
replyToMessageId: "parent-1",
|
|
replyQuote: "x".repeat(REPLY_QUOTE_MAX_LENGTH + 1),
|
|
});
|
|
|
|
expect(response.status).toBe(400);
|
|
await expect(response.json()).resolves.toEqual({
|
|
json: expect.objectContaining({
|
|
data: expect.objectContaining({
|
|
issues: expect.arrayContaining([expect.objectContaining({ path: ["replyQuote"] })]),
|
|
}),
|
|
}),
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("thread answer delivery", () => {
|
|
it("accepts a durable answer when the immediate worker wake fails", async () => {
|
|
const answerRunInput = vi.fn().mockResolvedValue(true);
|
|
const enqueue = vi.fn().mockRejectedValue(new Error("job broker unavailable"));
|
|
const sink = createTestSink();
|
|
installLogger(createLogger({ service: "rakazo-api", sinks: [sink] }));
|
|
const prisma = {
|
|
bot: {
|
|
findFirst: vi.fn().mockResolvedValue({
|
|
id: "bot-1",
|
|
thread: { id: "thread-1" },
|
|
computer: null,
|
|
}),
|
|
},
|
|
} as unknown as PrismaClient;
|
|
const deps = {
|
|
prisma,
|
|
events: { answerRunInput },
|
|
jobs: { enqueue },
|
|
env: {
|
|
defaultProvider: "fake",
|
|
defaultModel: "fake-model",
|
|
webOrigin: "http://127.0.0.1:5173",
|
|
screenProxySecret: "fake-test-secret",
|
|
sandboxProvider: "fake",
|
|
},
|
|
dataDir: "/tmp/rakazo-router-test",
|
|
} as unknown as RouterDeps;
|
|
const actor = {
|
|
spaceId: "workspace-1",
|
|
userId: "user-1",
|
|
email: "user@rakazo.test",
|
|
isDeploymentOwner: true,
|
|
} satisfies Actor;
|
|
const handler = new RPCHandler(createRouter(deps));
|
|
|
|
const { matched, response } = await handler.handle(
|
|
new Request("http://127.0.0.1/rpc/threads/answer", {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({
|
|
json: {
|
|
botId: "bot-1",
|
|
runId: "run-1",
|
|
messageId: "message-1",
|
|
answer: "Paris",
|
|
},
|
|
}),
|
|
}),
|
|
{ prefix: "/rpc", context: { actor } },
|
|
);
|
|
|
|
expect(matched).toBe(true);
|
|
expect(response.status).toBe(200);
|
|
await expect(response.json()).resolves.toEqual({ json: { ok: true } });
|
|
expect(answerRunInput).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
spaceId: "workspace-1",
|
|
threadId: "thread-1",
|
|
runId: "run-1",
|
|
}),
|
|
);
|
|
expect(enqueue).toHaveBeenCalledOnce();
|
|
expect(sink.events.some((event) => event.message === "thread answer enqueue")).toBe(true);
|
|
installLogger(createLogger({ service: "rakazo-api", level: "off", sinks: [] }));
|
|
});
|
|
});
|
|
|
|
describe("MCP server deletion", () => {
|
|
it("does not fail when a concurrent credential rotation already removed the old secret", async () => {
|
|
const deleteServer = vi.fn().mockResolvedValue({ id: "server-1" });
|
|
const deleteSecrets = vi.fn().mockResolvedValue({ count: 0 });
|
|
const prisma = {
|
|
mcpServer: {
|
|
findFirst: vi.fn().mockResolvedValue({ id: "server-1", secretId: "old-secret" }),
|
|
delete: deleteServer,
|
|
},
|
|
secret: { deleteMany: deleteSecrets },
|
|
$transaction: vi.fn((operations: Promise<unknown>[]) => Promise.all(operations)),
|
|
} as unknown as PrismaClient;
|
|
const deps = {
|
|
prisma,
|
|
env: {
|
|
defaultProvider: "fake",
|
|
defaultModel: "fake-model",
|
|
webOrigin: "http://127.0.0.1:5173",
|
|
screenProxySecret: "fake-test-secret",
|
|
sandboxProvider: "fake",
|
|
},
|
|
dataDir: "/tmp/rakazo-router-test",
|
|
} as unknown as RouterDeps;
|
|
const actor = {
|
|
spaceId: "workspace-1",
|
|
userId: "user-1",
|
|
email: "user@rakazo.test",
|
|
isDeploymentOwner: true,
|
|
} satisfies Actor;
|
|
const handler = new RPCHandler(createRouter(deps));
|
|
|
|
const { matched, response } = await handler.handle(
|
|
new Request("http://127.0.0.1/rpc/mcp/servers/remove", {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ json: { id: "server-1" } }),
|
|
}),
|
|
{ prefix: "/rpc", context: { actor } },
|
|
);
|
|
|
|
expect(matched).toBe(true);
|
|
expect(response.status).toBe(200);
|
|
await expect(response.json()).resolves.toEqual({ json: { ok: true } });
|
|
expect(deleteServer).toHaveBeenCalledWith({ where: { id: "server-1" } });
|
|
expect(deleteSecrets).toHaveBeenCalledWith({
|
|
where: {
|
|
id: "old-secret",
|
|
spaceId: "workspace-1",
|
|
userId: "user-1",
|
|
},
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("connections.complete", () => {
|
|
it("forwards an optional code to the managed connector", async () => {
|
|
const complete = vi.fn().mockResolvedValue({ connectionRef: "gmail" });
|
|
const connectionReady = vi.fn().mockResolvedValue(true);
|
|
const update = vi.fn().mockResolvedValue({
|
|
id: "conn-1",
|
|
connectorId: "composio",
|
|
provider: "gmail",
|
|
displayName: "Gmail",
|
|
status: "connected",
|
|
createdAt: new Date("2026-08-26T00:00:00.000Z"),
|
|
});
|
|
const prisma = {
|
|
connection: {
|
|
findFirst: vi.fn().mockResolvedValue({
|
|
id: "conn-1",
|
|
connectorId: "composio",
|
|
provider: "gmail",
|
|
displayName: "Gmail",
|
|
providerRef: "gmail-state",
|
|
status: "pending",
|
|
createdAt: new Date("2026-08-26T00:00:00.000Z"),
|
|
}),
|
|
findMany: vi.fn().mockResolvedValue([]),
|
|
update,
|
|
},
|
|
$transaction: vi.fn(async (fn: (tx: unknown) => Promise<unknown>) => {
|
|
const row = {
|
|
id: "conn-1",
|
|
connectorId: "composio",
|
|
provider: "gmail",
|
|
displayName: "Gmail",
|
|
providerRef: "gmail-state",
|
|
status: "pending",
|
|
createdAt: new Date("2026-08-26T00:00:00.000Z"),
|
|
};
|
|
const tx = {
|
|
$executeRaw: vi.fn().mockResolvedValue(undefined),
|
|
connection: {
|
|
findFirst: vi.fn().mockResolvedValueOnce(row).mockResolvedValueOnce(null),
|
|
findMany: vi.fn().mockResolvedValue([]),
|
|
update: vi
|
|
.fn()
|
|
.mockImplementation(async ({ data }: { data: Record<string, unknown> }) => ({
|
|
...row,
|
|
...data,
|
|
})),
|
|
},
|
|
};
|
|
return fn(tx);
|
|
}),
|
|
} as unknown as PrismaClient;
|
|
const deps = {
|
|
prisma,
|
|
connectors: {
|
|
managed: vi.fn(() => ({ complete, connectionReady })),
|
|
},
|
|
env: {
|
|
defaultProvider: "fake",
|
|
defaultModel: "fake-model",
|
|
webOrigin: "http://127.0.0.1:5173",
|
|
screenProxySecret: "fake-test-secret",
|
|
sandboxProvider: "fake",
|
|
},
|
|
dataDir: "/tmp/rakazo-router-test",
|
|
} as unknown as RouterDeps;
|
|
const actor = {
|
|
spaceId: "workspace-1",
|
|
userId: "user-1",
|
|
email: "user@rakazo.test",
|
|
isDeploymentOwner: true,
|
|
} satisfies Actor;
|
|
const handler = new RPCHandler(createRouter(deps));
|
|
|
|
const { matched, response } = await handler.handle(
|
|
new Request("http://127.0.0.1/rpc/connections/complete", {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({
|
|
json: {
|
|
connectionId: "conn-1",
|
|
code: "123456",
|
|
},
|
|
}),
|
|
}),
|
|
{ prefix: "/rpc", context: { actor } },
|
|
);
|
|
|
|
expect(matched).toBe(true);
|
|
expect(response.status).toBe(200);
|
|
expect(complete).toHaveBeenCalledWith(
|
|
{ state: "gmail-state", code: "123456" },
|
|
expect.objectContaining({ spaceId: "workspace-1", userId: "user-1" }),
|
|
);
|
|
expect(connectionReady).toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("updater owner gate", () => {
|
|
function updaterDeps() {
|
|
const prisma = {
|
|
user: {
|
|
findUniqueOrThrow: vi.fn().mockResolvedValue({
|
|
email: "user@rakazo.test",
|
|
name: "Test User",
|
|
avatarStyle: "robot",
|
|
}),
|
|
},
|
|
spaceModelPreference: { findFirst: vi.fn().mockResolvedValue(null) },
|
|
userModelCredential: { findFirst: vi.fn().mockResolvedValue(null) },
|
|
deploymentSettings: { findUnique: vi.fn().mockResolvedValue(null) },
|
|
} as unknown as PrismaClient;
|
|
const deps = {
|
|
prisma,
|
|
env: {
|
|
defaultProvider: "fake",
|
|
defaultModel: "fake-model",
|
|
webOrigin: "http://127.0.0.1:5173",
|
|
screenProxySecret: "fake-test-secret",
|
|
sandboxProvider: "fake",
|
|
gitSha: "deadbeef",
|
|
updaterUrl: undefined,
|
|
updaterToken: undefined,
|
|
},
|
|
dataDir: "/tmp/rakazo-router-test",
|
|
} as unknown as RouterDeps;
|
|
return { deps, handler: new RPCHandler(createRouter(deps)) };
|
|
}
|
|
|
|
it("forbids non-owners from updater status", async () => {
|
|
const { handler } = updaterDeps();
|
|
const actor = {
|
|
spaceId: "workspace-1",
|
|
userId: "user-2",
|
|
email: "member@rakazo.test",
|
|
isDeploymentOwner: false,
|
|
} satisfies Actor;
|
|
|
|
const { response } = await handler.handle(
|
|
new Request("http://127.0.0.1/rpc/updater/status", {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ json: null }),
|
|
}),
|
|
{ prefix: "/rpc", context: { actor } },
|
|
);
|
|
|
|
expect(response.status).toBe(403);
|
|
});
|
|
|
|
it("lets the deployment owner read status without applying git", async () => {
|
|
const { handler } = updaterDeps();
|
|
const actor = {
|
|
spaceId: "workspace-1",
|
|
userId: "user-1",
|
|
email: "owner@rakazo.test",
|
|
isDeploymentOwner: true,
|
|
} satisfies Actor;
|
|
|
|
const { response } = await handler.handle(
|
|
new Request("http://127.0.0.1/rpc/updater/status", {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ json: null }),
|
|
}),
|
|
{ prefix: "/rpc", context: { actor } },
|
|
);
|
|
|
|
expect(response.status).toBe(200);
|
|
const body = await response.json();
|
|
expect(body.json.supported).toBe(false);
|
|
expect(["source", "compose"]).toContain(body.json.installKind);
|
|
expect(Array.isArray(body.json.manualCommands)).toBe(true);
|
|
});
|
|
|
|
it("refuses apply when the sidecar is not configured", async () => {
|
|
const { handler } = updaterDeps();
|
|
const actor = {
|
|
spaceId: "workspace-1",
|
|
userId: "user-1",
|
|
email: "owner@rakazo.test",
|
|
isDeploymentOwner: true,
|
|
} satisfies Actor;
|
|
|
|
const { response } = await handler.handle(
|
|
new Request("http://127.0.0.1/rpc/updater/apply", {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ json: {} }),
|
|
}),
|
|
{ prefix: "/rpc", context: { actor } },
|
|
);
|
|
|
|
expect(response.status).toBeGreaterThanOrEqual(400);
|
|
const body = await response.json();
|
|
const message = JSON.stringify(body);
|
|
expect(message).toMatch(/sidecar/i);
|
|
expect(message).not.toMatch(/git (fetch|merge|pull)/i);
|
|
});
|
|
});
|
|
|
|
describe("computer screen url", () => {
|
|
const actor = {
|
|
spaceId: "workspace-1",
|
|
userId: "user-1",
|
|
email: "user@rakazo.test",
|
|
isDeploymentOwner: true,
|
|
} satisfies Actor;
|
|
const computerRow = {
|
|
id: "computer-1",
|
|
screenGeneration: 3,
|
|
kind: "e2b",
|
|
scope: "team",
|
|
state: "running",
|
|
providerRef: "sandbox-ref-1",
|
|
homeKey: "home-1",
|
|
controlHolder: "none",
|
|
controlLeaseId: null,
|
|
controlLeaseExpiresAt: null,
|
|
controlBotId: null,
|
|
controlRunId: null,
|
|
};
|
|
|
|
const callScreenUrl = async (connectScreen: () => Promise<unknown>, updateMany = vi.fn()) => {
|
|
const prisma = {
|
|
bot: {
|
|
findFirst: vi.fn().mockResolvedValue({
|
|
id: "bot-1",
|
|
screenGeneration: 2,
|
|
thread: { id: "thread-1" },
|
|
computer: computerRow,
|
|
}),
|
|
},
|
|
computer: { updateMany },
|
|
computerExecutionLease: { findUnique: vi.fn().mockResolvedValue(null) },
|
|
} as unknown as PrismaClient;
|
|
const deps = {
|
|
prisma,
|
|
sandbox: { connectScreen },
|
|
jobs: { enqueue: vi.fn().mockResolvedValue(undefined) },
|
|
env: {
|
|
defaultProvider: "fake",
|
|
defaultModel: "fake-model",
|
|
webOrigin: "http://127.0.0.1:5173",
|
|
screenProxySecret: "fake-test-secret",
|
|
sandboxProvider: "e2b",
|
|
},
|
|
dataDir: "/tmp/rakazo-router-test",
|
|
} as unknown as RouterDeps;
|
|
const handler = new RPCHandler(createRouter(deps));
|
|
const { response } = await handler.handle(
|
|
new Request("http://127.0.0.1/rpc/computer/screenUrl", {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ json: { botId: "bot-1" } }),
|
|
}),
|
|
{ prefix: "/rpc", context: { actor } },
|
|
);
|
|
return { response, updateMany };
|
|
};
|
|
|
|
it("issues lifecycle-bound capabilities for managed-provider screens too", async () => {
|
|
const { response } = await callScreenUrl(async () => ({
|
|
url: "https://screen.example/vnc.html?token=fake-token",
|
|
}));
|
|
expect(response.status).toBe(200);
|
|
const { json } = await response.json();
|
|
const url = new URL(json.url);
|
|
expect(url.origin).toBe("http://127.0.0.1:5173");
|
|
expect(openScreenCapability(url.pathname, "fake-test-secret")).toMatchObject({
|
|
scope: {
|
|
botId: "bot-1",
|
|
computerId: "computer-1",
|
|
botGeneration: 2,
|
|
computerGeneration: 3,
|
|
controlLeaseId: null,
|
|
},
|
|
target: { hostname: "screen.example", interactive: false },
|
|
});
|
|
});
|
|
|
|
it("returns desktop provider screen URLs without sealing them", async () => {
|
|
const { response } = await callScreenUrl(async () => ({
|
|
url: "desktop://screen/computer-1",
|
|
}));
|
|
expect(response.status).toBe(200);
|
|
await expect(response.json()).resolves.toEqual({
|
|
json: { url: "desktop://screen/computer-1?view_only=true" },
|
|
});
|
|
});
|
|
|
|
it("clears the row instead of 500ing when the provider says the sandbox is gone", async () => {
|
|
const { response, updateMany } = await callScreenUrl(() =>
|
|
Promise.reject(
|
|
Object.assign(new Error("Sandbox is probably not running anymore"), {
|
|
name: "SandboxNotFoundError",
|
|
}),
|
|
),
|
|
);
|
|
expect(response.status).toBe(200);
|
|
await expect(response.json()).resolves.toEqual({ json: { url: null } });
|
|
expect(updateMany).toHaveBeenCalledWith({
|
|
where: { id: "computer-1", providerRef: "sandbox-ref-1" },
|
|
data: { state: "stopped", providerRef: null },
|
|
});
|
|
});
|
|
|
|
it("keeps a transport blip an error and leaves the row alone", async () => {
|
|
const { response, updateMany } = await callScreenUrl(() =>
|
|
Promise.reject(Object.assign(new Error("fetch failed"), { code: "ECONNRESET" })),
|
|
);
|
|
expect(response.status).toBe(500);
|
|
expect(updateMany).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("returns a recoverable conflict when the screen is temporarily busy", async () => {
|
|
const { response, updateMany } = await callScreenUrl(() =>
|
|
Promise.reject(new ComputerScreenUnavailableError()),
|
|
);
|
|
expect(response.status).toBe(409);
|
|
await expect(response.json()).resolves.toEqual({
|
|
json: expect.objectContaining({
|
|
code: "CONFLICT",
|
|
message: COMPUTER_SCREEN_UNAVAILABLE,
|
|
}),
|
|
});
|
|
expect(updateMany).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("integration setup authorization", () => {
|
|
it.each([
|
|
{ owner: false, configured: false, needsSetup: false },
|
|
{ owner: true, configured: false, needsSetup: true },
|
|
{ owner: true, configured: true, needsSetup: false },
|
|
])(
|
|
"offers server setup only to an owner without configured providers: %j",
|
|
async ({ owner, configured, needsSetup }) => {
|
|
const lookup = vi.fn(async () => configured);
|
|
const deps = {
|
|
prisma: {},
|
|
env: { webOrigin: "https://example.test" },
|
|
integrationSettings: { configured: lookup },
|
|
} as unknown as RouterDeps;
|
|
const handler = new RPCHandler(createRouter(deps));
|
|
const { response } = await handler.handle(
|
|
new Request("https://example.test/rpc/integrationSetup/get", {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ json: null }),
|
|
}),
|
|
{
|
|
prefix: "/rpc",
|
|
context: {
|
|
actor: {
|
|
userId: "user",
|
|
spaceId: "space",
|
|
email: "user@rakazo.test",
|
|
isDeploymentOwner: owner,
|
|
},
|
|
},
|
|
},
|
|
);
|
|
const result = (await response.json()).json;
|
|
expect(result).toMatchObject({ canConfigure: owner, needsSetup });
|
|
if (!owner) {
|
|
expect(result.providers).toEqual([]);
|
|
expect(lookup).not.toHaveBeenCalled();
|
|
}
|
|
},
|
|
);
|
|
|
|
it("rejects provider credentials from a non-owner before verification or persistence", async () => {
|
|
const save = vi.fn();
|
|
const deps = {
|
|
prisma: {},
|
|
env: { webOrigin: "https://example.test" },
|
|
integrationSettings: { save },
|
|
} as unknown as RouterDeps;
|
|
const handler = new RPCHandler(createRouter(deps));
|
|
const { response } = await handler.handle(
|
|
new Request("https://example.test/rpc/integrationSetup/save", {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ json: { provider: "composio", apiKey: "fake-key" } }),
|
|
}),
|
|
{
|
|
prefix: "/rpc",
|
|
context: {
|
|
actor: {
|
|
userId: "member",
|
|
spaceId: "space",
|
|
email: "member@rakazo.test",
|
|
isDeploymentOwner: false,
|
|
},
|
|
},
|
|
},
|
|
);
|
|
expect(response.status).toBe(403);
|
|
expect(save).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("memory provider config owner gate", () => {
|
|
function memoryProviderDeps(spaceOwner: boolean) {
|
|
const spaceMemoryConfigFindUnique = vi.fn().mockResolvedValue({
|
|
provider: "supermemory",
|
|
settings: { mode: "local", baseUrl: "http://127.0.0.1:8123" },
|
|
defaultMemoryScope: "isolated",
|
|
updatedAt: new Date("2026-08-19T00:00:00.000Z"),
|
|
});
|
|
const spaceMemberFindUnique = vi.fn().mockResolvedValue({
|
|
role: spaceOwner ? "owner" : "member",
|
|
});
|
|
const prisma = {
|
|
spaceMember: { findUnique: spaceMemberFindUnique },
|
|
spaceMemoryConfig: { findUnique: spaceMemoryConfigFindUnique },
|
|
user: {
|
|
findUniqueOrThrow: vi.fn().mockResolvedValue({
|
|
email: "user@rakazo.test",
|
|
name: "Test User",
|
|
avatarStyle: "robot",
|
|
}),
|
|
},
|
|
spaceModelPreference: { findFirst: vi.fn().mockResolvedValue(null) },
|
|
userModelCredential: { findFirst: vi.fn().mockResolvedValue(null) },
|
|
deploymentSettings: { findUnique: vi.fn().mockResolvedValue(null) },
|
|
} as unknown as PrismaClient;
|
|
const deps = {
|
|
prisma,
|
|
env: {
|
|
defaultProvider: "fake",
|
|
defaultModel: "fake-model",
|
|
webOrigin: "http://127.0.0.1:5173",
|
|
screenProxySecret: "fake-test-secret",
|
|
sandboxProvider: "fake",
|
|
},
|
|
dataDir: "/tmp/rakazo-router-test",
|
|
} as unknown as RouterDeps;
|
|
const actor = {
|
|
spaceId: "workspace-1",
|
|
userId: "user-1",
|
|
email: spaceOwner ? "owner@rakazo.test" : "member@rakazo.test",
|
|
isDeploymentOwner: false,
|
|
} satisfies Actor;
|
|
return {
|
|
deps,
|
|
actor,
|
|
handler: new RPCHandler(createRouter(deps)),
|
|
spaceMemberFindUnique,
|
|
spaceMemoryConfigFindUnique,
|
|
};
|
|
}
|
|
|
|
it("forbids non-owners from reading provider configuration", async () => {
|
|
const { handler, actor, spaceMemberFindUnique, spaceMemoryConfigFindUnique } =
|
|
memoryProviderDeps(false);
|
|
|
|
const { response } = await handler.handle(
|
|
new Request("http://127.0.0.1/rpc/memory/providerConfig", {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ json: null }),
|
|
}),
|
|
{ prefix: "/rpc", context: { actor } },
|
|
);
|
|
|
|
expect(response.status).toBe(403);
|
|
expect(spaceMemberFindUnique).toHaveBeenCalled();
|
|
expect(spaceMemoryConfigFindUnique).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("returns provider configuration for Space owners", async () => {
|
|
const { handler, actor, spaceMemoryConfigFindUnique } = memoryProviderDeps(true);
|
|
|
|
const { response } = await handler.handle(
|
|
new Request("http://127.0.0.1/rpc/memory/providerConfig", {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ json: null }),
|
|
}),
|
|
{ prefix: "/rpc", context: { actor } },
|
|
);
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(spaceMemoryConfigFindUnique).toHaveBeenCalled();
|
|
await expect(response.json()).resolves.toEqual({
|
|
json: {
|
|
provider: "supermemory",
|
|
settings: { mode: "local", baseUrl: "http://127.0.0.1:8123" },
|
|
defaultMemoryScope: "isolated",
|
|
updatedAt: "2026-08-19T00:00:00.000Z",
|
|
},
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("interrupted computer reservation release", () => {
|
|
function fixture() {
|
|
const order: string[] = [];
|
|
const computerUpdate = {
|
|
findFirst: vi.fn(async () => ({ id: "update-1", computerId: "computer-1" })),
|
|
updateMany: vi.fn(async () => {
|
|
order.push("operation");
|
|
return { count: 1 };
|
|
}),
|
|
};
|
|
const computer = {
|
|
updateMany: vi.fn(async () => {
|
|
order.push("computer");
|
|
return { count: 1 };
|
|
}),
|
|
};
|
|
const prisma = { computer, computerUpdate, $transaction: vi.fn(async (fn) => fn(prisma)) };
|
|
const handler = new RPCHandler(
|
|
createRouter({ prisma, env: { sandboxProvider: "fake" } } as unknown as RouterDeps),
|
|
);
|
|
const call = async (owner: boolean, workersStopped?: boolean) =>
|
|
handler.handle(
|
|
new Request("http://127.0.0.1/rpc/computer/releaseInterrupted", {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ json: { id: "update-1", workersStopped } }),
|
|
}),
|
|
{
|
|
prefix: "/rpc",
|
|
context: {
|
|
actor: {
|
|
spaceId: "space-1",
|
|
userId: "user-1",
|
|
email: "user@rakazo.test",
|
|
isDeploymentOwner: owner,
|
|
},
|
|
},
|
|
},
|
|
);
|
|
return { prisma, computer, computerUpdate, order, call };
|
|
}
|
|
it.each([
|
|
{ owner: false, stopped: true, status: 403 },
|
|
{ owner: true, stopped: false, status: 400 },
|
|
{ owner: true, stopped: undefined, status: 400 },
|
|
])(
|
|
"requires owner authorization and an explicit stopped-workers assertion: %j",
|
|
async ({ owner, stopped, status }) => {
|
|
const { call, prisma } = fixture();
|
|
const { response } = await call(owner, stopped);
|
|
expect(response.status).toBe(status);
|
|
expect(prisma.$transaction).not.toHaveBeenCalled();
|
|
},
|
|
);
|
|
it("atomically releases only an interrupted reservation in the owner's workspace", async () => {
|
|
const { call, computer, computerUpdate, order } = fixture();
|
|
const { response } = await call(true, true);
|
|
expect(response.status).toBe(200);
|
|
expect(computerUpdate.findFirst).toHaveBeenCalledWith({
|
|
where: {
|
|
id: "update-1",
|
|
status: "interrupted",
|
|
computer: { spaceId: "space-1", bots: { some: { userId: "user-1", archivedAt: null } } },
|
|
},
|
|
});
|
|
expect(computerUpdate.updateMany).toHaveBeenCalledWith({
|
|
where: { id: "update-1", status: "interrupted" },
|
|
data: { status: "failed" },
|
|
});
|
|
expect(computer.updateMany).toHaveBeenCalledWith({
|
|
where: { id: "computer-1", maintenanceId: "update-1" },
|
|
data: { maintenanceId: null, state: "error" },
|
|
});
|
|
expect(order).toEqual(["operation", "computer"]);
|
|
});
|
|
});
|