Skip to content

Commit

Permalink
add reset events command
Browse files Browse the repository at this point in the history
  • Loading branch information
seveibar committed Dec 30, 2024
1 parent 2e8df75 commit b6b4225
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/db/db-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,11 @@ const initializer = combine(databaseSchema.parse({}), (set, get) => ({
if (!since) return state.events
return state.events.filter((e) => e.created_at > since)
},

resetEvents: () => {
set((state) => ({
...state,
events: []
}))
},
}))
12 changes: 12 additions & 0 deletions routes/events/reset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { withRouteSpec } from "lib/middleware/with-winter-spec"
import { z } from "zod"

export default withRouteSpec({
methods: ["POST"],
jsonResponse: z.object({
ok: z.boolean(),
}),
})((req, ctx) => {
ctx.db.resetEvents()
return ctx.json({ ok: true })
})
26 changes: 26 additions & 0 deletions tests/routes/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,29 @@ test("custom events", async () => {
expect(createdEvent.ip_address).toBe("192.168.1.1")
expect(createdEvent.success).toBe(true)
})

test("reset events", async () => {
const { axios } = await getTestServer()

// Create some events
await axios.post("/events/create", {
event_type: "USER_LOGIN",
user_id: "123"
})
await axios.post("/events/create", {
event_type: "USER_LOGIN",
user_id: "456"
})

// Verify events exist
let listRes = await axios.get("/events/list")
expect(listRes.data.event_list).toHaveLength(2)

// Reset events
const resetRes = await axios.post("/events/reset")
expect(resetRes.data.success).toBe(true)

Check failure on line 49 in tests/routes/events.test.ts

View workflow job for this annotation

GitHub Actions / test

error: expect(received).toBe(expected)

Expected: true Received: undefined at /home/runner/work/file-server/file-server/tests/routes/events.test.ts:49:33

// Verify events are cleared
listRes = await axios.get("/events/list")
expect(listRes.data.event_list).toHaveLength(0)
})

0 comments on commit b6b4225

Please sign in to comment.