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
28 changes: 18 additions & 10 deletions tests/routes/things/create.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import { getTestServer } from "tests/fixtures/get-test-server"
import { test, expect } from "bun:test"

test("create a thing", async () => {
test("create a thing persists and lists deterministically", async () => {
const { ky } = await getTestServer()

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

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

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

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

test("delete a thing removes it from the list", async () => {
const { ky } = await getTestServer()

await ky
.post("things/create", {
json: { name: "ToDelete", description: "Will be removed" },
})
.json<{ ok: boolean }>()

await ky
.post("things/create", {
json: { name: "ToKeep", description: "Stays" },
})
.json<{ ok: boolean }>()

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

const target = before.things.find((t) => t.name === "ToDelete")
expect(target).toBeDefined()

const deleteRes = await ky
.post("things/delete", {
body: new URLSearchParams({ thing_id: target!.thing_id }),
})
.json<{ ok: boolean }>()
expect(deleteRes).toEqual({ ok: true })

const after = await ky.get("things/list").json<{
things: { thing_id: string; name: string; description: string }[]
}>()
expect(after.things).toHaveLength(1)
expect(after.things[0].name).toBe("ToKeep")
expect(
after.things.find((t) => t.thing_id === target!.thing_id),
).toBeUndefined()
})

test("delete is a no-op when thing_id does not exist", async () => {
const { ky } = await getTestServer()

await ky
.post("things/create", {
json: { name: "Solo", description: "only one" },
})
.json<{ ok: boolean }>()

const res = await ky
.post("things/delete", {
body: new URLSearchParams({ thing_id: "does-not-exist" }),
})
.json<{ ok: boolean }>()
expect(res).toEqual({ ok: true })

const after = await ky
.get("things/list")
.json<{ things: { thing_id: string; name: string }[] }>()
expect(after.things).toHaveLength(1)
expect(after.things[0].name).toBe("Solo")
})
35 changes: 35 additions & 0 deletions tests/routes/things/list.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { getTestServer } from "tests/fixtures/get-test-server"
import { test, expect } from "bun:test"

test("list returns an empty array when no things exist", 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 returns things in insertion order with incrementing thing_id", async () => {
const { ky } = await getTestServer()

const names = ["Alpha", "Bravo", "Charlie"]
for (const name of names) {
await ky
.post("things/create", {
json: { name, description: `${name} desc` },
})
.json<{ ok: boolean }>()
}

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

expect(data.things.map((t) => t.name)).toEqual(names)
// thing_id is generated from an incrementing counter
const ids = data.things.map((t) => Number(t.thing_id))
expect(ids).toEqual([...ids].sort((a, b) => a - b))
expect(new Set(ids).size).toBe(ids.length)
})
Loading