58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
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.devices.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()
|
|
})
|
|
})
|
|
|