ajustes nos teste, adições e remoções

This commit is contained in:
Esdras Renan 2025-10-16 19:29:52 -03:00
parent 68ace0a858
commit 1ce402cdd7
4 changed files with 270 additions and 0 deletions

View file

@ -0,0 +1,58 @@
import { describe, expect, it, beforeEach, vi } from "vitest"
import { api } from "@/convex/_generated/api"
const mutationMock = vi.fn()
vi.mock("@/server/convex-client", () => ({
createConvexClient: () => ({
mutation: mutationMock,
}),
ConvexConfigurationError: class extends Error {},
}))
describe("POST /api/machines/heartbeat", () => {
beforeEach(() => {
mutationMock.mockReset()
})
it("accepts a valid payload and forwards it to Convex", async () => {
const payload = {
machineToken: "token-123",
status: "online",
metrics: { cpu: 42 },
}
mutationMock.mockResolvedValue({ ok: true })
const { POST } = await import("./route")
const response = await POST(
new Request("http://localhost/api/machines/heartbeat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
})
)
expect(response.status).toBe(200)
const body = await response.json()
expect(body).toEqual({ ok: true })
expect(mutationMock).toHaveBeenCalledWith(api.machines.heartbeat, payload)
})
it("rejects an invalid payload", async () => {
const { POST } = await import("./route")
const response = await POST(
new Request("http://localhost/api/machines/heartbeat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({}),
})
)
expect(response.status).toBe(400)
const body = await response.json()
expect(body).toHaveProperty("error", "Payload inválido")
expect(mutationMock).not.toHaveBeenCalled()
})
})