Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 19 additions & 9 deletions tests/routes/things/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@ import { test, expect } from "bun:test"
test("create a thing", async () => {
const { ky } = await getTestServer()

ky.post("things/create", {
json: {
const res = await ky
.post("things/create", {
json: {
name: "Thing1",
description: "Thing1 Description",
},
})
.json<{ ok: boolean }>()

expect(res.ok).toBe(true)

const data = await ky.get("things/list").json<{
things: { thing_id: string; name: string; description: string }[]
}>()

expect(data.things).toEqual([
{
thing_id: "0",
name: "Thing1",
description: "Thing1 Description",
},
})

const data = await ky
.get("things/list")
.json<{ things: { name: string; description: string }[] }>()

expect(data.things).toHaveLength(1)
])
})
73 changes: 73 additions & 0 deletions tests/routes/things/delete.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { getTestServer } from "tests/fixtures/get-test-server"
import { test, expect } from "bun:test"

test("delete a thing", async () => {
const { ky } = await getTestServer()

// 1. Create a thing first
const createRes = await ky
.post("things/create", {
json: {
name: "Thing to Delete",
description: "This thing will be deleted",
},
})
.json<{ ok: boolean }>()

expect(createRes.ok).toBe(true)

// 2. List the things to get its generated thing_id
const listBefore = await ky
.get("things/list")
.json<{ things: { thing_id: string; name: string }[] }>()

expect(listBefore.things).toHaveLength(1)
const thingId = listBefore.things[0].thing_id

// 3. Delete the thing using URL-encoded form data
const deleteRes = await ky
.post("things/delete", {
body: new URLSearchParams({ thing_id: thingId }),
})
.json<{ ok: boolean }>()

expect(deleteRes.ok).toBe(true)

// 4. Verify that the list is empty now
const listAfter = await ky
.get("things/list")
.json<{ things: { thing_id: string; name: string }[] }>()

expect(listAfter.things).toHaveLength(0)
})

test("delete is a no-op for unknown thing ids", async () => {
const { ky } = await getTestServer()

await ky.post("things/create", {
json: {
name: "Thing1",
description: "Thing1 Description",
},
})

const deleteResponse = await ky
.post("things/delete", {
body: new URLSearchParams({ thing_id: "missing" }),
})
.json<{ ok: boolean }>()

expect(deleteResponse).toEqual({ ok: true })

const data = await ky.get("things/list").json<{
things: { thing_id: string; name: string; description: string }[]
}>()

expect(data.things).toEqual([
{
thing_id: "0",
name: "Thing1",
description: "Thing1 Description",
},
])
})
46 changes: 46 additions & 0 deletions tests/routes/things/list.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { expect, test } from "bun:test"
import { getTestServer } from "tests/fixtures/get-test-server"

test("list starts empty", async () => {
const { ky } = await getTestServer()

const data = await ky.get("things/list").json<{
things: { thing_id: string; name: string; description: string }[]
}>()

expect(data.things).toEqual([])
})

test("list preserves insertion order and generated ids", async () => {
const { ky } = await getTestServer()

await ky.post("things/create", {
json: {
name: "Thing1",
description: "Thing1 Description",
},
})
await ky.post("things/create", {
json: {
name: "Thing2",
description: "Thing2 Description",
},
})

const data = await ky.get("things/list").json<{
things: { thing_id: string; name: string; description: string }[]
}>()

expect(data.things).toEqual([
{
thing_id: "0",
name: "Thing1",
description: "Thing1 Description",
},
{
thing_id: "1",
name: "Thing2",
description: "Thing2 Description",
},
])
})
Loading