|
| 1 | +import { |
| 2 | + afterAll, |
| 3 | + beforeAll, |
| 4 | + beforeEach, |
| 5 | + describe, |
| 6 | + expect, |
| 7 | + test, |
| 8 | +} from "vitest"; |
| 9 | +import { __setAgentAssetsApiKeyOverrideForTests } from "@/middleware/agentAuth"; |
| 10 | +import { createJwtToken } from "@/utils/jwt"; |
| 11 | +import { prisma } from "@/utils/prisma"; |
| 12 | +import { |
| 13 | + agentKeyHeaders, |
| 14 | + startAgentTemplatesServer, |
| 15 | + validAgentAssetsApiKey, |
| 16 | +} from "./agent-templates.cross.helpers"; |
| 17 | + |
| 18 | +// The featured gallery is the homepage's, not the template owner's. These pin |
| 19 | +// the whole path shut: an ordinary signed-in user could otherwise mint a |
| 20 | +// self-featured template, publish it (owners may publish their own), and appear |
| 21 | +// in the gallery convos.org renders. They couldn't pick a slot — that was |
| 22 | +// already gated — but being in the gallery at all is the front door. |
| 23 | + |
| 24 | +// A plain punter: not the admin account, no API key. |
| 25 | +const USER = "00000000-0000-4000-8000-eeeeeeee0001"; |
| 26 | + |
| 27 | +let baseURL: string; |
| 28 | +let close: () => Promise<void>; |
| 29 | + |
| 30 | +const userHeaders = async () => ({ |
| 31 | + "Content-Type": "application/json", |
| 32 | + "X-Convos-AuthToken": await createJwtToken({ |
| 33 | + deviceId: "featuring-authz-device", |
| 34 | + accountId: USER, |
| 35 | + }), |
| 36 | +}); |
| 37 | + |
| 38 | +const create = async ( |
| 39 | + headers: Record<string, string>, |
| 40 | + body: Record<string, unknown>, |
| 41 | +) => { |
| 42 | + const response = await fetch(`${baseURL}/api/v2/agent-templates`, { |
| 43 | + method: "POST", |
| 44 | + headers, |
| 45 | + body: JSON.stringify(body), |
| 46 | + }); |
| 47 | + return { response, body: (await response.json()) as Record<string, unknown> }; |
| 48 | +}; |
| 49 | + |
| 50 | +const patch = async ( |
| 51 | + headers: Record<string, string>, |
| 52 | + id: string, |
| 53 | + body: Record<string, unknown>, |
| 54 | +) => { |
| 55 | + const response = await fetch(`${baseURL}/api/v2/agent-templates/${id}`, { |
| 56 | + method: "PATCH", |
| 57 | + headers, |
| 58 | + body: JSON.stringify(body), |
| 59 | + }); |
| 60 | + return { response, body: (await response.json()) as Record<string, unknown> }; |
| 61 | +}; |
| 62 | + |
| 63 | +const featuredOf = async (id: string) => |
| 64 | + ( |
| 65 | + await prisma.agentTemplate.findUniqueOrThrow({ |
| 66 | + where: { id }, |
| 67 | + select: { featured: true }, |
| 68 | + }) |
| 69 | + ).featured; |
| 70 | + |
| 71 | +describe("Featuring is the dashboard's, not the owner's", () => { |
| 72 | + beforeAll(async () => { |
| 73 | + __setAgentAssetsApiKeyOverrideForTests(validAgentAssetsApiKey); |
| 74 | + await prisma.account.upsert({ |
| 75 | + where: { id: USER }, |
| 76 | + create: { id: USER }, |
| 77 | + update: {}, |
| 78 | + }); |
| 79 | + const server = await startAgentTemplatesServer(4098); |
| 80 | + baseURL = server.baseURL; |
| 81 | + close = server.close; |
| 82 | + }); |
| 83 | + |
| 84 | + afterAll(async () => { |
| 85 | + await prisma.agentTemplate.deleteMany({ where: { ownerAccountId: USER } }); |
| 86 | + await close(); |
| 87 | + __setAgentAssetsApiKeyOverrideForTests(undefined); |
| 88 | + }); |
| 89 | + |
| 90 | + beforeEach(async () => { |
| 91 | + await prisma.agentTemplate.deleteMany({ where: { ownerAccountId: USER } }); |
| 92 | + }); |
| 93 | + |
| 94 | + test("a user can't create a template that is already featured", async () => { |
| 95 | + const headers = await userHeaders(); |
| 96 | + const res = await create(headers, { |
| 97 | + agentName: "Self Featured", |
| 98 | + prompt: "p", |
| 99 | + slug: "authz-self-featured", |
| 100 | + featured: true, |
| 101 | + }); |
| 102 | + expect(res.response.status).toBe(403); |
| 103 | + |
| 104 | + // And nothing was written. |
| 105 | + const rows = await prisma.agentTemplate.findMany({ |
| 106 | + where: { ownerAccountId: USER }, |
| 107 | + }); |
| 108 | + expect(rows).toEqual([]); |
| 109 | + }); |
| 110 | + |
| 111 | + test("a user can't feature their own template afterwards", async () => { |
| 112 | + const headers = await userHeaders(); |
| 113 | + const created = await create(headers, { |
| 114 | + agentName: "Ordinary", |
| 115 | + prompt: "p", |
| 116 | + slug: "authz-ordinary", |
| 117 | + }); |
| 118 | + const id = created.body.id as string; |
| 119 | + expect(await featuredOf(id)).toBe(false); |
| 120 | + |
| 121 | + const res = await patch(headers, id, { featured: true }); |
| 122 | + expect(res.response.status).toBe(403); |
| 123 | + expect(await featuredOf(id)).toBe(false); |
| 124 | + }); |
| 125 | + |
| 126 | + // The builder sends `featured: false` on every create, and withdrawing a |
| 127 | + // template from the homepage can only ever remove something from it. |
| 128 | + test("a user may still create with featured:false, and unfeature their own", async () => { |
| 129 | + const headers = await userHeaders(); |
| 130 | + const created = await create(headers, { |
| 131 | + agentName: "Opts Out", |
| 132 | + prompt: "p", |
| 133 | + slug: "authz-opts-out", |
| 134 | + featured: false, |
| 135 | + }); |
| 136 | + expect(created.response.status).toBe(201); |
| 137 | + const id = created.body.id as string; |
| 138 | + |
| 139 | + // The dashboard features it... |
| 140 | + const featured = await patch(agentKeyHeaders(), id, { featured: true }); |
| 141 | + expect(featured.response.status).toBe(200); |
| 142 | + expect(await featuredOf(id)).toBe(true); |
| 143 | + |
| 144 | + // ...and the owner can take it back out. |
| 145 | + const withdrawn = await patch(headers, id, { featured: false }); |
| 146 | + expect(withdrawn.response.status).toBe(200); |
| 147 | + expect(await featuredOf(id)).toBe(false); |
| 148 | + }); |
| 149 | + |
| 150 | + test("the dashboard still features templates", async () => { |
| 151 | + const created = await create(agentKeyHeaders(), { |
| 152 | + agentName: "Curated", |
| 153 | + prompt: "p", |
| 154 | + slug: "authz-curated", |
| 155 | + featured: true, |
| 156 | + ownerAccountId: USER, |
| 157 | + }); |
| 158 | + expect(created.response.status).toBe(201); |
| 159 | + expect(await featuredOf(created.body.id as string)).toBe(true); |
| 160 | + }); |
| 161 | +}); |
0 commit comments