sistema-de-chamados/tests/api-admin-devices-rename.test.ts
2025-11-27 08:38:11 -03:00

72 lines
2.1 KiB
TypeScript

import { describe, expect, it, beforeEach, vi } from "vitest"
import { POST } from "@/app/api/admin/devices/rename/route"
import { assertAuthenticatedSession } from "@/lib/auth-server"
vi.mock("@/lib/auth-server", () => ({
assertAuthenticatedSession: vi.fn(),
}))
const mutationMock = vi.fn()
vi.mock("convex/browser", () => ({
ConvexHttpClient: vi.fn(() => ({ mutation: mutationMock })),
}))
vi.mock("@/convex/_generated/api", () => ({
api: {
users: { ensureUser: "users:ensureUser" },
devices: {
heartbeat: "devices:heartbeat",
upsertInventory: "devices:upsertInventory",
findByAuthEmail: "devices:findByAuthEmail",
remove: "devices:remove",
},
},
}))
describe("POST /api/admin/devices/rename", () => {
beforeEach(() => {
vi.resetAllMocks()
process.env.NEXT_PUBLIC_CONVEX_URL = "https://convex.example.test"
vi.mocked(assertAuthenticatedSession).mockResolvedValue({
user: {
id: "session-user",
email: "agent@example.com",
name: "Agent",
role: "AGENT",
tenantId: "tenant-atlas",
avatarUrl: null,
},
session: { id: "sess", expiresAt: Date.now() + 1000 },
})
mutationMock.mockImplementation((name: string) => {
if (name === "users:ensureUser") {
return Promise.resolve({ _id: "user-123" })
}
return Promise.resolve({ ok: true })
})
})
it("envia somente machineId, actorId e hostname para machines:rename", async () => {
const body = {
machineId: "machine-123",
hostname: "novo-host",
}
const req = new Request("http://localhost/api/admin/devices/rename", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
})
const res = await POST(req)
expect(res.status).toBe(200)
const renameCall = mutationMock.mock.calls.find(([name]) => name === "machines:rename")
expect(renameCall).toBeDefined()
expect(renameCall?.[1]).toEqual({
machineId: "machine-123",
actorId: "user-123",
hostname: "novo-host",
})
})
})