Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

test(community): relocate bot route tests to /community; preserve join hint (phase 5a)

Simoned6be175d563c
diff --git a/src/web/src/app/api/community/agent/ack/route.test.ts b/src/web/src/app/api/community/ack/route.test.ts
similarity index 96%
rename from src/web/src/app/api/community/agent/ack/route.test.ts
rename to src/web/src/app/api/community/ack/route.test.ts
index 185a874c5..c5737f30f 100644
--- a/src/web/src/app/api/community/agent/ack/route.test.ts
+++ b/src/web/src/app/api/community/ack/route.test.ts
@@ -5,6 +5,15 @@ vi.mock("@opennextjs/cloudflare", () => ({
getCloudflareContext: vi.fn(async () => ({ env: { DB: {} } })),
}))
vi.mock("@/lib/db", () => ({ getDb: vi.fn(() => ({})) }))
+// Unified actor: a request with no `crk_` bearer falls through to the human
+// withAuth path. Mock Better-Auth to resolve "no session" so a no-auth request
+// yields the human-path 401 ("unauthorized") — the real unified-actor contract —
+// instead of a 503 from unmocked session validation.
+vi.mock("@/lib/auth", () => ({
+ createAuth: vi.fn(() => ({
+ api: { getSession: vi.fn(async () => ({ headers: new Headers(), response: null })) },
+ })),
+}))
const mockFindActiveAgentRunnerKeyByBearer = vi.fn()
const mockGetUserInternal = vi.fn()
diff --git a/src/web/src/app/api/community/agent/listServers/route.test.ts b/src/web/src/app/api/community/agent/listServers/route.test.ts
deleted file mode 100644
index ff71dcc14..000000000
--- a/src/web/src/app/api/community/agent/listServers/route.test.ts
+++ /dev/null
@@ -1,74 +0,0 @@
-import { describe, it, expect, vi, beforeEach } from "vitest"
-import { NextRequest } from "next/server"
-
-vi.mock("@opennextjs/cloudflare", () => ({
- getCloudflareContext: vi.fn(async () => ({ env: { DB: {} } })),
-}))
-vi.mock("@/lib/db", () => ({ getDb: vi.fn(() => ({})) }))
-
-const mockFindActiveAgentRunnerKeyByBearer = vi.fn()
-const mockGetUserInternal = vi.fn()
-const mockGetBotBinding = vi.fn()
-const mockListUserServers = vi.fn()
-
-vi.mock("@alook/shared", async () => {
- const actual = await vi.importActual<typeof import("@alook/shared")>("@alook/shared")
- return {
- ...actual,
- queries: {
- ...actual.queries,
- communityMachine: { findActiveAgentRunnerKeyByBearer: (...a: unknown[]) => mockFindActiveAgentRunnerKeyByBearer(...a) },
- user: { getUserInternal: (...a: unknown[]) => mockGetUserInternal(...a) },
- communityBot: { getBotBinding: (...a: unknown[]) => mockGetBotBinding(...a) },
- communityServer: { listUserServers: (...a: unknown[]) => mockListUserServers(...a) },
- },
- }
-})
-
-import { POST } from "./route"
-
-function req(headers: Record<string, string> = {}): NextRequest {
- return new NextRequest("http://localhost/api/community/agent/listServers", {
- method: "POST",
- headers: { "content-type": "application/json", ...headers },
- body: "{}",
- })
-}
-
-describe("POST /api/community/agent/listServers", () => {
- beforeEach(() => {
- vi.clearAllMocks()
- mockFindActiveAgentRunnerKeyByBearer.mockResolvedValue({ userId: "owner_1", machineId: "m_1", agentId: "bot_1" })
- mockGetUserInternal.mockResolvedValue({ isBot: true, deletedAt: null })
- mockGetBotBinding.mockResolvedValue({ machineId: "m_1", runtime: "claude" })
- })
-
- it("401 without Authorization", async () => {
- const res = await POST(new NextRequest("http://localhost/api/community/agent/listServers", { method: "POST" }))
- expect(res.status).toBe(401)
- expect(mockListUserServers).not.toHaveBeenCalled()
- })
-
- it("200 maps rows to { id, name } only, dropping other fields", async () => {
- mockListUserServers.mockResolvedValue([
- { id: "srv_1", name: "Studio", ownerId: "u_owner", secretStuff: "nope" },
- { id: "srv_2", name: "Lab" },
- ])
- const res = await POST(req({ Authorization: "Bearer crk_abc" }))
- expect(res.status).toBe(200)
- expect(await res.json()).toEqual({
- servers: [
- { id: "srv_1", name: "Studio" },
- { id: "srv_2", name: "Lab" },
- ],
- })
- expect(mockListUserServers).toHaveBeenCalledWith(expect.anything(), "bot_1")
- })
-
- it("200 with an empty list when the bot is in no servers", async () => {
- mockListUserServers.mockResolvedValue([])
- const res = await POST(req({ Authorization: "Bearer crk_abc" }))
- expect(res.status).toBe(200)
- expect(await res.json()).toEqual({ servers: [] })
- })
-})
diff --git a/src/web/src/app/api/community/agent/attachmentDownload/route.test.ts b/src/web/src/app/api/community/attachmentDownload/route.test.ts
similarity index 94%
rename from src/web/src/app/api/community/agent/attachmentDownload/route.test.ts
rename to src/web/src/app/api/community/attachmentDownload/route.test.ts
index 48c411ee4..7c87f2def 100644
--- a/src/web/src/app/api/community/agent/attachmentDownload/route.test.ts
+++ b/src/web/src/app/api/community/attachmentDownload/route.test.ts
@@ -8,6 +8,15 @@ vi.mock("@opennextjs/cloudflare", () => ({
})),
}))
vi.mock("@/lib/db", () => ({ getDb: vi.fn(() => ({})) }))
+// Unified actor: a request with no `crk_` bearer falls through to the human
+// withAuth path. Mock Better-Auth to resolve "no session" so a no-auth request
+// yields the human-path 401 ("unauthorized") — the real unified-actor contract —
+// instead of a 503 from unmocked session validation.
+vi.mock("@/lib/auth", () => ({
+ createAuth: vi.fn(() => ({
+ api: { getSession: vi.fn(async () => ({ headers: new Headers(), response: null })) },
+ })),
+}))
const mockFindActiveAgentRunnerKeyByBearer = vi.fn()
const mockGetUserInternal = vi.fn()
diff --git a/src/web/src/app/api/community/agent/attachmentUpload/route.test.ts b/src/web/src/app/api/community/attachmentUpload/route.test.ts
similarity index 94%
rename from src/web/src/app/api/community/agent/attachmentUpload/route.test.ts
rename to src/web/src/app/api/community/attachmentUpload/route.test.ts
index a2d311baf..325b12a25 100644
--- a/src/web/src/app/api/community/agent/attachmentUpload/route.test.ts
+++ b/src/web/src/app/api/community/attachmentUpload/route.test.ts
@@ -8,6 +8,15 @@ vi.mock("@opennextjs/cloudflare", () => ({
})),
}))
vi.mock("@/lib/db", () => ({ getDb: vi.fn(() => ({})) }))
+// Unified actor: a request with no `crk_` bearer falls through to the human
+// withAuth path. Mock Better-Auth to resolve "no session" so a no-auth request
+// yields the human-path 401 ("unauthorized") — the real unified-actor contract —
+// instead of a 503 from unmocked session validation.
+vi.mock("@/lib/auth", () => ({
+ createAuth: vi.fn(() => ({
+ api: { getSession: vi.fn(async () => ({ headers: new Headers(), response: null })) },
+ })),
+}))
const mockFindActiveAgentRunnerKeyByBearer = vi.fn()
const mockGetUserInternal = vi.fn()
diff --git a/src/web/src/app/api/community/agent/channelMember/route.test.ts b/src/web/src/app/api/community/channelMember/route.test.ts
similarity index 95%
rename from src/web/src/app/api/community/agent/channelMember/route.test.ts
rename to src/web/src/app/api/community/channelMember/route.test.ts
index efb4f919e..4edaaf314 100644
--- a/src/web/src/app/api/community/agent/channelMember/route.test.ts
+++ b/src/web/src/app/api/community/channelMember/route.test.ts
@@ -5,6 +5,15 @@ vi.mock("@opennextjs/cloudflare", () => ({
getCloudflareContext: vi.fn(async () => ({ env: { DB: {} } })),
}))
vi.mock("@/lib/db", () => ({ getDb: vi.fn(() => ({})) }))
+// Unified actor: a request with no `crk_` bearer falls through to the human
+// withAuth path. Mock Better-Auth to resolve "no session" so a no-auth request
+// yields the human-path 401 ("unauthorized") — the real unified-actor contract —
+// instead of a 503 from unmocked session validation.
+vi.mock("@/lib/auth", () => ({
+ createAuth: vi.fn(() => ({
+ api: { getSession: vi.fn(async () => ({ headers: new Headers(), response: null })) },
+ })),
+}))
const mockFindActiveAgentRunnerKeyByBearer = vi.fn()
const mockGetUserInternal = vi.fn()
diff --git a/src/web/src/app/api/community/agent/friendRequest/route.test.ts b/src/web/src/app/api/community/friendRequest/route.test.ts
similarity index 95%
rename from src/web/src/app/api/community/agent/friendRequest/route.test.ts
rename to src/web/src/app/api/community/friendRequest/route.test.ts
index 34ab2ccfe..6660b66e2 100644
--- a/src/web/src/app/api/community/agent/friendRequest/route.test.ts
+++ b/src/web/src/app/api/community/friendRequest/route.test.ts
@@ -5,6 +5,14 @@ vi.mock("@opennextjs/cloudflare", () => ({
getCloudflareContext: vi.fn(async () => ({ env: { DB: {} } })),
}))
vi.mock("@/lib/db", () => ({ getDb: vi.fn(() => ({})) }))
+// Unified actor: a no-`crk_` request falls through to the human withAuth path.
+// Mock Better-Auth to "no session" so a no-auth request yields the human-path
+// 401 instead of a 503 from unmocked session validation.
+vi.mock("@/lib/auth", () => ({
+ createAuth: vi.fn(() => ({
+ api: { getSession: vi.fn(async () => ({ headers: new Headers(), response: null })) },
+ })),
+}))
const mockFindActiveAgentRunnerKeyByBearer = vi.fn()
const mockGetUserInternal = vi.fn()
diff --git a/src/web/src/app/api/community/agent/inboxPull/route.test.ts b/src/web/src/app/api/community/inboxPull/route.test.ts
similarity index 92%
rename from src/web/src/app/api/community/agent/inboxPull/route.test.ts
rename to src/web/src/app/api/community/inboxPull/route.test.ts
index a5875141d..c6897d0d5 100644
--- a/src/web/src/app/api/community/agent/inboxPull/route.test.ts
+++ b/src/web/src/app/api/community/inboxPull/route.test.ts
@@ -5,6 +5,15 @@ vi.mock("@opennextjs/cloudflare", () => ({
getCloudflareContext: vi.fn(async () => ({ env: { DB: {} } })),
}))
vi.mock("@/lib/db", () => ({ getDb: vi.fn(() => ({})) }))
+// Unified actor: a request with no `crk_` bearer falls through to the human
+// withAuth path. Mock Better-Auth to resolve "no session" so a no-auth request
+// yields the human-path 401 ("unauthorized") — the real unified-actor contract —
+// instead of a 503 from unmocked session validation.
+vi.mock("@/lib/auth", () => ({
+ createAuth: vi.fn(() => ({
+ api: { getSession: vi.fn(async () => ({ headers: new Headers(), response: null })) },
+ })),
+}))
const mockFindActiveAgentRunnerKeyByBearer = vi.fn()
const mockGetUserInternal = vi.fn()
diff --git a/src/web/src/app/api/community/agent/inboxSnapshot/route.test.ts b/src/web/src/app/api/community/inboxSnapshot/route.test.ts
similarity index 87%
rename from src/web/src/app/api/community/agent/inboxSnapshot/route.test.ts
rename to src/web/src/app/api/community/inboxSnapshot/route.test.ts
index 07c84994e..35bfa5d06 100644
--- a/src/web/src/app/api/community/agent/inboxSnapshot/route.test.ts
+++ b/src/web/src/app/api/community/inboxSnapshot/route.test.ts
@@ -5,6 +5,15 @@ vi.mock("@opennextjs/cloudflare", () => ({
getCloudflareContext: vi.fn(async () => ({ env: { DB: {} } })),
}))
vi.mock("@/lib/db", () => ({ getDb: vi.fn(() => ({})) }))
+// Unified actor: a request with no `crk_` bearer falls through to the human
+// withAuth path. Mock Better-Auth to resolve "no session" so a no-auth request
+// yields the human-path 401 ("unauthorized") — the real unified-actor contract —
+// instead of a 503 from unmocked session validation.
+vi.mock("@/lib/auth", () => ({
+ createAuth: vi.fn(() => ({
+ api: { getSession: vi.fn(async () => ({ headers: new Headers(), response: null })) },
+ })),
+}))
const mockFindActiveAgentRunnerKeyByBearer = vi.fn()
const mockGetUserInternal = vi.fn()
diff --git a/src/web/src/app/api/community/agent/joinServer/route.test.ts b/src/web/src/app/api/community/invites/[token]/join/route.bot.test.ts
similarity index 71%
rename from src/web/src/app/api/community/agent/joinServer/route.test.ts
rename to src/web/src/app/api/community/invites/[token]/join/route.bot.test.ts
index b86eca01f..d9a4cbd59 100644
--- a/src/web/src/app/api/community/agent/joinServer/route.test.ts
+++ b/src/web/src/app/api/community/invites/[token]/join/route.bot.test.ts
@@ -6,6 +6,16 @@ vi.mock("@opennextjs/cloudflare", () => ({
}))
vi.mock("@/lib/db", () => ({ getDb: vi.fn(() => ({})) }))
+// Under the unified actor, a request with NO `crk_` bearer falls through to the
+// human withAuth path. Mock Better-Auth to resolve "no session" so a no-auth
+// request yields the human-path 401 ("unauthorized") — the real unified-actor
+// contract — instead of a 503 from unmocked session validation.
+vi.mock("@/lib/auth", () => ({
+ createAuth: vi.fn(() => ({
+ api: { getSession: vi.fn(async () => ({ headers: new Headers(), response: null })) },
+ })),
+}))
+
const mockFindActiveAgentRunnerKeyByBearer = vi.fn()
const mockGetUserInternal = vi.fn()
const mockGetBotBinding = vi.fn()
@@ -49,15 +59,22 @@ vi.mock("@/lib/community/audit", async () => {
import { POST } from "./route"
import { isUniqueConstraintError } from "@alook/shared"
-function req(body: unknown, headers: Record<string, string> = {}): NextRequest {
- return new NextRequest("http://localhost/api/community/agent/joinServer", {
+// Bot-path coverage for the UNIFIED join route (folded from /agent/joinServer,
+// plan §9 phase 4/5). The token is a ROUTE PARAM now (`[token]`), not a body
+// field — the invite body carried it on the old flat /agent route; here it
+// rides `ctx.params.token`. A `crk_` bearer drives withCommunityActor's bot
+// path (same resolveBotActor query mocks as before). The human path is covered
+// by the sibling route.test.ts.
+const TOKEN = "tok_abc"
+function req(token: string, headers: Record<string, string> = {}): NextRequest {
+ return new NextRequest(`http://localhost/api/community/invites/${token}/join`, {
method: "POST",
headers: { "content-type": "application/json", ...headers },
- body: typeof body === "string" ? body : JSON.stringify(body),
})
}
+const params = (token: string) => ({ params: Promise.resolve({ token }) })
-describe("POST /api/community/agent/joinServer", () => {
+describe("POST /api/community/invites/[token]/join — bot path", () => {
beforeEach(() => {
vi.clearAllMocks()
mockFindActiveAgentRunnerKeyByBearer.mockResolvedValue({ userId: "owner_1", machineId: "m_1", agentId: "bot_1" })
@@ -66,14 +83,14 @@ describe("POST /api/community/agent/joinServer", () => {
})
it("401 without Authorization", async () => {
- const res = await POST(req({ invite: "tok_abc" }))
+ const res = await POST(req(TOKEN), params(TOKEN))
expect(res.status).toBe(401)
expect(mockGetInviteByToken).not.toHaveBeenCalled()
})
it("400 'Invalid or expired invite' for an unknown token", async () => {
mockGetInviteByToken.mockResolvedValue(null)
- const res = await POST(req({ invite: "tok_unknown" }, { Authorization: "Bearer crk_abc" }))
+ const res = await POST(req("tok_unknown", { Authorization: "Bearer crk_abc" }), params("tok_unknown"))
expect(res.status).toBe(400)
expect((await res.json()).error).toBe("Invalid or expired invite")
expect(mockUseInvite).not.toHaveBeenCalled()
@@ -81,7 +98,7 @@ describe("POST /api/community/agent/joinServer", () => {
it("400 'Invalid or expired invite' (NOT a 403) when invite.createdBy is null", async () => {
mockGetInviteByToken.mockResolvedValue({ id: "inv_1", serverId: "srv_1", createdBy: null })
- const res = await POST(req({ invite: "tok_abc" }, { Authorization: "Bearer crk_abc" }))
+ const res = await POST(req(TOKEN, { Authorization: "Bearer crk_abc" }), params(TOKEN))
expect(res.status).toBe(400)
expect((await res.json()).error).toBe("Invalid or expired invite")
expect(mockUseInvite).not.toHaveBeenCalled()
@@ -89,7 +106,7 @@ describe("POST /api/community/agent/joinServer", () => {
it("403 with hint when invite.createdBy is a real, different user id than ctx.ownerUserId", async () => {
mockGetInviteByToken.mockResolvedValue({ id: "inv_1", serverId: "srv_1", createdBy: "stranger_1" })
- const res = await POST(req({ invite: "tok_abc" }, { Authorization: "Bearer crk_abc" }))
+ const res = await POST(req(TOKEN, { Authorization: "Bearer crk_abc" }), params(TOKEN))
expect(res.status).toBe(403)
const body = await res.json()
expect(body.error).toContain("not created by your owner")
@@ -100,14 +117,14 @@ describe("POST /api/community/agent/joinServer", () => {
it("400 'Already a member' on a unique-constraint re-join", async () => {
mockGetInviteByToken.mockResolvedValue({ id: "inv_1", serverId: "srv_1", createdBy: "owner_1" })
mockUseInvite.mockRejectedValue(new Error("UNIQUE constraint failed"))
- const res = await POST(req({ invite: "tok_abc" }, { Authorization: "Bearer crk_abc" }))
+ const res = await POST(req(TOKEN, { Authorization: "Bearer crk_abc" }), params(TOKEN))
// Guard the test against the mocked isUniqueConstraintError predicate diverging silently.
expect(isUniqueConstraintError(new Error("UNIQUE constraint failed"))).toBe(true)
expect(res.status).toBe(400)
expect((await res.json()).error).toBe("Already a member")
})
- it("200 {server:{id,name}} on success; fanOutToServerMembers excludes the bot; logAudit uses BOT_JOINED_VIA_INVITE + botUserId", async () => {
+ it("200 superset {member, serverId, server} on success; fanOutToServerMembers excludes the bot; logAudit uses BOT_JOINED_VIA_INVITE + botUserId", async () => {
mockGetInviteByToken.mockResolvedValue({ id: "inv_1", serverId: "srv_1", createdBy: "owner_1" })
mockUseInvite.mockResolvedValue({
invite: { id: "inv_1", serverId: "srv_1" },
@@ -115,9 +132,13 @@ describe("POST /api/community/agent/joinServer", () => {
})
mockGetServer.mockResolvedValue({ id: "srv_1", name: "Design Studio" })
- const res = await POST(req({ invite: "tok_abc" }, { Authorization: "Bearer crk_abc" }))
+ const res = await POST(req(TOKEN, { Authorization: "Bearer crk_abc" }), params(TOKEN))
expect(res.status).toBe(200)
- expect(await res.json()).toEqual({ server: { id: "srv_1", name: "Design Studio" } })
+ // Superset response (Fork C): the bot's daemon projects `server` down to
+ // the old lean {server:{id,name}} shape; serverId/member ride along.
+ const body = await res.json()
+ expect(body.server).toEqual({ id: "srv_1", name: "Design Studio" })
+ expect(body.serverId).toBe("srv_1")
expect(mockFanOutToServerMembers).toHaveBeenCalledWith(
"srv_1",
diff --git a/src/web/src/app/api/community/invites/[token]/join/route.ts b/src/web/src/app/api/community/invites/[token]/join/route.ts
index 98b71ea76..e3920e893 100644
--- a/src/web/src/app/api/community/invites/[token]/join/route.ts
+++ b/src/web/src/app/api/community/invites/[token]/join/route.ts
@@ -38,7 +38,14 @@ export const POST = withCommunityActor(async (_req, ctx) => {
return writeError("Invalid or expired invite", 400)
}
if (invite.createdBy !== actor.ownerUserId) {
- return writeError("This invite was not created by your owner — refusing to join.", 403)
+ // Preserve the bot CLI's actionable hint (the /agent/joinServer contract).
+ return writeJSON(
+ {
+ error: "This invite was not created by your owner — refusing to join.",
+ hint: "Ask your owner to send an invite link they created themselves.",
+ },
+ 403,
+ )
}
}
diff --git a/src/web/src/app/api/community/agent/listChannels/route.test.ts b/src/web/src/app/api/community/listChannels/route.test.ts
similarity index 94%
rename from src/web/src/app/api/community/agent/listChannels/route.test.ts
rename to src/web/src/app/api/community/listChannels/route.test.ts
index 381cf494e..9817adaa0 100644
--- a/src/web/src/app/api/community/agent/listChannels/route.test.ts
+++ b/src/web/src/app/api/community/listChannels/route.test.ts
@@ -5,6 +5,15 @@ vi.mock("@opennextjs/cloudflare", () => ({
getCloudflareContext: vi.fn(async () => ({ env: { DB: {} } })),
}))
vi.mock("@/lib/db", () => ({ getDb: vi.fn(() => ({})) }))
+// Unified actor: a request with no `crk_` bearer falls through to the human
+// withAuth path. Mock Better-Auth to resolve "no session" so a no-auth request
+// yields the human-path 401 ("unauthorized") — the real unified-actor contract —
+// instead of a 503 from unmocked session validation.
+vi.mock("@/lib/auth", () => ({
+ createAuth: vi.fn(() => ({
+ api: { getSession: vi.fn(async () => ({ headers: new Headers(), response: null })) },
+ })),
+}))
const mockFindActiveAgentRunnerKeyByBearer = vi.fn()
const mockGetUserInternal = vi.fn()
diff --git a/src/web/src/app/api/community/agent/listFriends/route.test.ts b/src/web/src/app/api/community/listFriends/route.test.ts
similarity index 91%
rename from src/web/src/app/api/community/agent/listFriends/route.test.ts
rename to src/web/src/app/api/community/listFriends/route.test.ts
index 2d82d57f3..32cdaeff3 100644
--- a/src/web/src/app/api/community/agent/listFriends/route.test.ts
+++ b/src/web/src/app/api/community/listFriends/route.test.ts
@@ -5,6 +5,14 @@ vi.mock("@opennextjs/cloudflare", () => ({
getCloudflareContext: vi.fn(async () => ({ env: { DB: {} } })),
}))
vi.mock("@/lib/db", () => ({ getDb: vi.fn(() => ({})) }))
+// Unified actor: a no-`crk_` request falls through to the human withAuth path.
+// Mock Better-Auth to "no session" so a no-auth request yields the human-path
+// 401 instead of a 503 from unmocked session validation.
+vi.mock("@/lib/auth", () => ({
+ createAuth: vi.fn(() => ({
+ api: { getSession: vi.fn(async () => ({ headers: new Headers(), response: null })) },
+ })),
+}))
const mockFindActiveAgentRunnerKeyByBearer = vi.fn()
const mockGetUserInternal = vi.fn()
diff --git a/src/web/src/app/api/community/agent/listMembers/route.test.ts b/src/web/src/app/api/community/listMembers/route.test.ts
similarity index 90%
rename from src/web/src/app/api/community/agent/listMembers/route.test.ts
rename to src/web/src/app/api/community/listMembers/route.test.ts
index e02fa7345..f3aaa6f89 100644
--- a/src/web/src/app/api/community/agent/listMembers/route.test.ts
+++ b/src/web/src/app/api/community/listMembers/route.test.ts
@@ -5,6 +5,15 @@ vi.mock("@opennextjs/cloudflare", () => ({
getCloudflareContext: vi.fn(async () => ({ env: { DB: {} } })),
}))
vi.mock("@/lib/db", () => ({ getDb: vi.fn(() => ({})) }))
+// Unified actor: a request with no `crk_` bearer falls through to the human
+// withAuth path. Mock Better-Auth to resolve "no session" so a no-auth request
+// yields the human-path 401 ("unauthorized") — the real unified-actor contract —
+// instead of a 503 from unmocked session validation.
+vi.mock("@/lib/auth", () => ({
+ createAuth: vi.fn(() => ({
+ api: { getSession: vi.fn(async () => ({ headers: new Headers(), response: null })) },
+ })),
+}))
const mockFindActiveAgentRunnerKeyByBearer = vi.fn()
const mockGetUserInternal = vi.fn()
diff --git a/src/web/src/app/api/community/agent/reactAdd/route.test.ts b/src/web/src/app/api/community/reactAdd/route.test.ts
similarity index 95%
rename from src/web/src/app/api/community/agent/reactAdd/route.test.ts
rename to src/web/src/app/api/community/reactAdd/route.test.ts
index 096207122..15da3e449 100644
--- a/src/web/src/app/api/community/agent/reactAdd/route.test.ts
+++ b/src/web/src/app/api/community/reactAdd/route.test.ts
@@ -5,6 +5,15 @@ vi.mock("@opennextjs/cloudflare", () => ({
getCloudflareContext: vi.fn(async () => ({ env: { DB: {} } })),
}))
vi.mock("@/lib/db", () => ({ getDb: vi.fn(() => ({})) }))
+// Unified actor: a request with no `crk_` bearer falls through to the human
+// withAuth path. Mock Better-Auth to resolve "no session" so a no-auth request
+// yields the human-path 401 ("unauthorized") — the real unified-actor contract —
+// instead of a 503 from unmocked session validation.
+vi.mock("@/lib/auth", () => ({
+ createAuth: vi.fn(() => ({
+ api: { getSession: vi.fn(async () => ({ headers: new Headers(), response: null })) },
+ })),
+}))
const mockFindActiveAgentRunnerKeyByBearer = vi.fn()
const mockGetUserInternal = vi.fn()
diff --git a/src/web/src/app/api/community/agent/read/route.test.ts b/src/web/src/app/api/community/read/route.test.ts
similarity index 90%
rename from src/web/src/app/api/community/agent/read/route.test.ts
rename to src/web/src/app/api/community/read/route.test.ts
index 6c9e6a5c9..6a8174a0e 100644
--- a/src/web/src/app/api/community/agent/read/route.test.ts
+++ b/src/web/src/app/api/community/read/route.test.ts
@@ -5,6 +5,15 @@ vi.mock("@opennextjs/cloudflare", () => ({
getCloudflareContext: vi.fn(async () => ({ env: { DB: {} } })),
}))
vi.mock("@/lib/db", () => ({ getDb: vi.fn(() => ({})) }))
+// Unified actor: a request with no `crk_` bearer falls through to the human
+// withAuth path. Mock Better-Auth to resolve "no session" so a no-auth request
+// yields the human-path 401 ("unauthorized") — the real unified-actor contract —
+// instead of a 503 from unmocked session validation.
+vi.mock("@/lib/auth", () => ({
+ createAuth: vi.fn(() => ({
+ api: { getSession: vi.fn(async () => ({ headers: new Headers(), response: null })) },
+ })),
+}))
const mockFindActiveAgentRunnerKeyByBearer = vi.fn()
const mockGetUserInternal = vi.fn()
diff --git a/src/web/src/app/api/community/agent/resolve/route.test.ts b/src/web/src/app/api/community/resolve/route.test.ts
similarity index 94%
rename from src/web/src/app/api/community/agent/resolve/route.test.ts
rename to src/web/src/app/api/community/resolve/route.test.ts
index ef8ee29e4..2791b2b6c 100644
--- a/src/web/src/app/api/community/agent/resolve/route.test.ts
+++ b/src/web/src/app/api/community/resolve/route.test.ts
@@ -5,6 +5,15 @@ vi.mock("@opennextjs/cloudflare", () => ({
getCloudflareContext: vi.fn(async () => ({ env: { DB: {} } })),
}))
vi.mock("@/lib/db", () => ({ getDb: vi.fn(() => ({})) }))
+// Unified actor: a request with no `crk_` bearer falls through to the human
+// withAuth path. Mock Better-Auth to resolve "no session" so a no-auth request
+// yields the human-path 401 ("unauthorized") — the real unified-actor contract —
+// instead of a 503 from unmocked session validation.
+vi.mock("@/lib/auth", () => ({
+ createAuth: vi.fn(() => ({
+ api: { getSession: vi.fn(async () => ({ headers: new Headers(), response: null })) },
+ })),
+}))
const mockFindActiveAgentRunnerKeyByBearer = vi.fn()
const mockGetUserInternal = vi.fn()
diff --git a/src/web/src/app/api/community/agent/send/route.test.ts b/src/web/src/app/api/community/send/route.test.ts
similarity index 97%
rename from src/web/src/app/api/community/agent/send/route.test.ts
rename to src/web/src/app/api/community/send/route.test.ts
index cd0c46ab0..9b8a7e44e 100644
--- a/src/web/src/app/api/community/agent/send/route.test.ts
+++ b/src/web/src/app/api/community/send/route.test.ts
@@ -5,6 +5,15 @@ vi.mock("@opennextjs/cloudflare", () => ({
getCloudflareContext: vi.fn(async () => ({ env: { DB: {} } })),
}))
vi.mock("@/lib/db", () => ({ getDb: vi.fn(() => ({})) }))
+// Unified actor: a request with no `crk_` bearer falls through to the human
+// withAuth path. Mock Better-Auth to resolve "no session" so a no-auth request
+// yields the human-path 401 ("unauthorized") — the real unified-actor contract —
+// instead of a 503 from unmocked session validation.
+vi.mock("@/lib/auth", () => ({
+ createAuth: vi.fn(() => ({
+ api: { getSession: vi.fn(async () => ({ headers: new Headers(), response: null })) },
+ })),
+}))
const mockFindPendingAttachmentsForBot = vi.fn(async () => [] as unknown[])
const mockFindActiveAgentRunnerKeyByBearer = vi.fn()
diff --git a/src/web/src/app/api/community/servers/route.bot.test.ts b/src/web/src/app/api/community/servers/route.bot.test.ts
new file mode 100644
index 000000000..331748a92
--- /dev/null
+++ b/src/web/src/app/api/community/servers/route.bot.test.ts
@@ -0,0 +1,95 @@
+import { describe, it, expect, vi, beforeEach } from "vitest"
+import { NextRequest } from "next/server"
+
+// Bot-path coverage for the UNIFIED server-list route (listServers folded onto
+// GET /servers, plan §4/§9 phase 4-5). A `crk_` bearer drives
+// withCommunityActor's bot path; GET lists via listUserServers(actor.userId).
+// The route returns the SUPERSET (full rows + icon) — the daemon/CLI projects
+// each row down to the lean {id,name} shape (Fork C). That daemon-side lean
+// projection is asserted by the daemon projection test, NOT here; here we lock
+// the ROUTE contract: bot auth works, scoping is by the bot's own membership,
+// and the full rows (incl. id+name) are returned.
+vi.mock("@opennextjs/cloudflare", () => ({
+ getCloudflareContext: vi.fn(async () => ({ env: { DB: {} } })),
+}))
+vi.mock("@/lib/db", () => ({ getDb: vi.fn(() => ({})) }))
+
+// No-`crk_` request falls through to the human withAuth path; mock Better-Auth
+// to "no session" so a no-auth request yields the human-path 401 rather than a
+// 503 from unmocked session validation.
+vi.mock("@/lib/auth", () => ({
+ createAuth: vi.fn(() => ({
+ api: { getSession: vi.fn(async () => ({ headers: new Headers(), response: null })) },
+ })),
+}))
+
+const mockFindActiveAgentRunnerKeyByBearer = vi.fn()
+const mockGetUserInternal = vi.fn()
+const mockGetBotBinding = vi.fn()
+const mockListUserServers = vi.fn()
+
+vi.mock("@alook/shared", async () => {
+ const actual = await vi.importActual<typeof import("@alook/shared")>("@alook/shared")
+ return {
+ ...actual,
+ queries: {
+ ...actual.queries,
+ communityMachine: { findActiveAgentRunnerKeyByBearer: (...a: unknown[]) => mockFindActiveAgentRunnerKeyByBearer(...a) },
+ user: { getUserInternal: (...a: unknown[]) => mockGetUserInternal(...a) },
+ communityBot: { getBotBinding: (...a: unknown[]) => mockGetBotBinding(...a) },
+ communityServer: { listUserServers: (...a: unknown[]) => mockListUserServers(...a) },
+ },
+ }
+})
+
+// serverIconUrl reads the row; keep it a passthrough so we assert on ids/names.
+vi.mock("@/lib/community/storage", () => ({
+ serverIconUrl: vi.fn(() => null),
+}))
+
+import { GET } from "./route"
+
+function req(headers: Record<string, string> = {}): NextRequest {
+ return new NextRequest("http://localhost/api/community/servers", {
+ method: "GET",
+ headers: { ...headers },
+ })
+}
+
+describe("GET /api/community/servers — bot path (folded listServers)", () => {
+ beforeEach(() => {
+ vi.clearAllMocks()
+ mockFindActiveAgentRunnerKeyByBearer.mockResolvedValue({ userId: "owner_1", machineId: "m_1", agentId: "bot_1" })
+ mockGetUserInternal.mockResolvedValue({ isBot: true, deletedAt: null })
+ mockGetBotBinding.mockResolvedValue({ machineId: "m_1", runtime: "claude" })
+ })
+
+ it("401 without Authorization (no crk_ → human path, no session)", async () => {
+ const res = await GET(req())
+ expect(res.status).toBe(401)
+ expect(mockListUserServers).not.toHaveBeenCalled()
+ })
+
+ it("200 lists servers scoped to the bot's own membership", async () => {
+ mockListUserServers.mockResolvedValue([
+ { id: "srv_1", name: "Studio", ownerId: "u_owner" },
+ { id: "srv_2", name: "Lab" },
+ ])
+ const res = await GET(req({ Authorization: "Bearer crk_abc" }))
+ expect(res.status).toBe(200)
+ const body = (await res.json()) as { servers: Array<{ id: string; name: string }> }
+ expect(body.servers.map((s) => ({ id: s.id, name: s.name }))).toEqual([
+ { id: "srv_1", name: "Studio" },
+ { id: "srv_2", name: "Lab" },
+ ])
+ // Scoping red line: the bot sees only ITS OWN servers (actor.userId=bot_1).
+ expect(mockListUserServers).toHaveBeenCalledWith(expect.anything(), "bot_1")
+ })
+
+ it("200 with an empty list when the bot is in no servers", async () => {
+ mockListUserServers.mockResolvedValue([])
+ const res = await GET(req({ Authorization: "Bearer crk_abc" }))
+ expect(res.status).toBe(200)
+ expect((await res.json()).servers).toEqual([])
+ })
+})