Files
PaperClipAI/server/src/services/workspace-runtime-read-model.test.ts
T
DottaandPaperclip b51112798f feat(apps): improve gateway and workspace connection UX (#12340)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - App connections must work in both the operator UI and agent tool
gateway.
> - The first stack layer adds secure remote connections.
> - Operators still need clear setup, test, and recovery states.
> - This pull request adds the gateway behavior and the workspace
connection experience.
> - The benefit is a connection flow that is easier to understand and
recover.

## Linked Issues or Issue Description

Refs #11965

This is stack 2 of 11. It depends on stack 1 and replaces another
reviewable part of #11965.

## What Changed

- Improve remote tool gateway connection behavior.
- Add clearer app setup, test, and recovery states.
- Add focused server and UI tests for the new paths.
- Keep the diff isolated from later identity and catalog work.
- Stabilize DNS-pinned remote HTTP protocol fixtures and the
managed-runtime public-origin fixture for this independently tested
layer.

## Verification

- `pnpm -r typecheck`
- `pnpm --filter @paperclipai/server exec vitest run
src/__tests__/tool-access-service.test.ts` (150 passed)
- `pnpm test:run`
- `pnpm check:token-gates`
- `pnpm build`

## Risks

- Gateway errors now surface through new user-facing states.
- A stale connection can require a new setup attempt.
- The change does not add a database migration.
- The injected HTTP transport and public URL are test-only fixtures;
production DNS pinning and runtime behavior are unchanged.

> I checked `ROADMAP.md`. This stack continues the existing app
connection work from #11965 and does not duplicate another planned item.

## Model Used

OpenAI Codex, GPT-5. The runtime model ID and context window were not
exposed. The model used reasoning, tool use, and code execution.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links
- [x] My branch name describes the change and contains no internal
Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-08-29 12:08:32 -05:00

209 lines
5.9 KiB
TypeScript

import { randomUUID } from "node:crypto";
import { workspaceRuntimeServices } from "@paperclipai/db";
import { describe, expect, it } from "vitest";
import { selectConfiguredRuntimeServiceRows } from "./workspace-runtime-read-model.js";
type WorkspaceRuntimeServiceRow = typeof workspaceRuntimeServices.$inferSelect;
function runtimeServiceRow(
overrides: Partial<WorkspaceRuntimeServiceRow> = {},
): WorkspaceRuntimeServiceRow {
const now = new Date("2026-07-31T10:00:00.000Z");
return {
id: randomUUID(),
companyId: randomUUID(),
projectId: randomUUID(),
projectWorkspaceId: randomUUID(),
executionWorkspaceId: null,
issueId: null,
scopeType: "project_workspace",
scopeId: null,
serviceName: "web",
status: "stopped",
lifecycle: "shared",
reuseKey: randomUUID(),
command: "pnpm dev",
cwd: "/tmp/paperclip",
port: null,
url: null,
provider: "local_process",
providerRef: null,
ownerAgentId: null,
startedByRunId: null,
lastUsedAt: now,
startedAt: now,
stoppedAt: now,
stopPolicy: null,
healthStatus: "unknown",
exposure: null,
exposureHandle: null,
backendUrl: null,
createdAt: now,
updatedAt: now,
...overrides,
};
}
describe("selectConfiguredRuntimeServiceRows", () => {
it("excludes stopped project-workspace history when no services remain configured", () => {
const historicalRows = Array.from({ length: 163 }, (_, index) =>
runtimeServiceRow({
serviceName: `historical-service-${index + 1}`,
command: `pnpm historical:${index + 1}`,
}),
);
expect(selectConfiguredRuntimeServiceRows(historicalRows, null)).toEqual([]);
});
it("selects the newest current row for each configured service and annotates its config index", () => {
const oldWeb = runtimeServiceRow({
serviceName: "web",
command: "pnpm dev",
updatedAt: new Date("2026-07-29T10:00:00.000Z"),
});
const currentWeb = runtimeServiceRow({
serviceName: "web",
command: "pnpm dev",
updatedAt: new Date("2026-07-30T10:00:00.000Z"),
});
const removedWorker = runtimeServiceRow({
serviceName: "worker",
command: "pnpm worker",
updatedAt: new Date("2026-07-31T10:00:00.000Z"),
});
const selected = selectConfiguredRuntimeServiceRows(
[removedWorker, currentWeb, oldWeb],
{ services: [{ name: "web", command: "pnpm dev" }] },
);
expect(selected).toEqual([
expect.objectContaining({
id: currentWeb.id,
serviceName: "web",
configIndex: 0,
}),
]);
});
it("keeps an exposed dev runtime tracked after its bind command is hardened", () => {
const exposedWeb = runtimeServiceRow({
serviceName: "paperclip-dev",
command: "pnpm dev --bind loopback",
exposure: {
provider: "tailscale_https",
state: "ready",
publicUrl: "https://paperclip-dev.example.ts.net:42012",
hostname: "paperclip-dev.example.ts.net",
listeners: [],
brokerRef: "broker-1",
lastError: null,
updatedAt: "2026-08-20T00:00:00.000Z",
},
});
const selected = selectConfiguredRuntimeServiceRows(
[exposedWeb],
{ services: [{ name: "paperclip-dev", command: "pnpm dev --bind lan" }] },
);
expect(selected).toEqual([
expect.objectContaining({ id: exposedWeb.id, configIndex: 0 }),
]);
});
it("matches configured services only to rows with the configured reuse scope", () => {
const staleProjectScopedWorker = runtimeServiceRow({
serviceName: "worker",
command: "pnpm worker",
});
const executionScopedWorker = runtimeServiceRow({
executionWorkspaceId: randomUUID(),
scopeType: "execution_workspace",
scopeId: randomUUID(),
serviceName: "worker",
command: "pnpm worker",
});
const selected = selectConfiguredRuntimeServiceRows(
[staleProjectScopedWorker, executionScopedWorker],
{
services: [
{
name: "worker",
command: "pnpm worker",
reuseScope: "execution_workspace",
},
],
},
);
expect(selected).toEqual([
expect.objectContaining({
id: executionScopedWorker.id,
configIndex: 0,
}),
]);
});
it("can fall back to legacy execution-workspace scope for directly configured services", () => {
const projectScopedHistory = runtimeServiceRow({
serviceName: "worker",
command: "pnpm worker",
});
const legacyExecutionScopedWeb = runtimeServiceRow({
executionWorkspaceId: randomUUID(),
scopeType: "execution_workspace",
scopeId: randomUUID(),
serviceName: "web",
command: "pnpm dev",
});
const selected = selectConfiguredRuntimeServiceRows(
[projectScopedHistory, legacyExecutionScopedWeb],
{ services: [{ name: "web", command: "pnpm dev" }] },
{ fallbackScopeTypes: ["execution_workspace"] },
);
expect(selected).toEqual([
expect.objectContaining({
id: legacyExecutionScopedWeb.id,
configIndex: 0,
}),
]);
});
it("selects the row for the configured port instead of newer history on another port", () => {
const previousPort = runtimeServiceRow({
port: 42013,
updatedAt: new Date("2026-07-31T10:00:00.000Z"),
});
const configuredPort = runtimeServiceRow({
port: 42001,
updatedAt: new Date("2026-07-30T10:00:00.000Z"),
});
const selected = selectConfiguredRuntimeServiceRows(
[previousPort, configuredPort],
{
services: [
{
name: "web",
command: "pnpm dev",
port: { type: "fixed", value: 42001 },
},
],
},
);
expect(selected).toEqual([
expect.objectContaining({
id: configuredPort.id,
port: 42001,
configIndex: 0,
}),
]);
});
});