88 lines
2.7 KiB
TypeScript
88 lines
2.7 KiB
TypeScript
import { describe, it, expect } from "vitest"
|
|
import { buildVoyageTree } from "../../app/utils/buildVoyageTree"
|
|
|
|
const info = { title: "Trip to Mars", lutins: [] } as any
|
|
|
|
const parent = {
|
|
id: 1,
|
|
title: "Departure",
|
|
parent: null,
|
|
startDate: "2026-06-13T08:00:00",
|
|
endDate: "2026-06-13T10:00:00",
|
|
location: { name: "Paris" },
|
|
} as any
|
|
|
|
const child = {
|
|
id: 2,
|
|
title: "Layover",
|
|
parent: 1,
|
|
startDate: "2026-06-13T09:00:00",
|
|
endDate: "2026-06-13T09:30:00",
|
|
} as any
|
|
|
|
const undated = {
|
|
id: 3,
|
|
title: "Maybe later",
|
|
parent: null,
|
|
startDate: null,
|
|
} as any
|
|
|
|
const trip = { id: 10, mode: "train", eventStart: 1 } as any
|
|
|
|
describe("buildVoyageTree", () => {
|
|
it("builds the synthetic voyage root", () => {
|
|
const root = buildVoyageTree(info, [parent, child], [trip])
|
|
expect(root.root).toBe(true)
|
|
expect(root.id).toBe("__voyage")
|
|
expect(root.event.id).toBe(-1)
|
|
expect(root.event.title).toBe("Trip to Mars")
|
|
})
|
|
|
|
it("nests children under their parent event", () => {
|
|
const root = buildVoyageTree(info, [parent, child], [trip])
|
|
expect(root.children).toHaveLength(1)
|
|
const top = root.children[0]!
|
|
expect(top.id).toBe("1")
|
|
expect(top.children).toHaveLength(1)
|
|
expect(top.children[0]!.id).toBe("2")
|
|
expect(top.children[0]!.event.title).toBe("Layover")
|
|
})
|
|
|
|
it("references the backing event rather than copying it", () => {
|
|
const root = buildVoyageTree(info, [parent, child], [trip])
|
|
expect(root.children[0]!.event).toBe(parent)
|
|
})
|
|
|
|
it("attaches the trip mode to the matching parent node", () => {
|
|
const root = buildVoyageTree(info, [parent, child], [trip])
|
|
const top = root.children[0]!
|
|
expect(top.trip).toEqual({ mode: "train" })
|
|
expect(top.children[0]!.trip).toBeNull()
|
|
})
|
|
|
|
it("exposes location and dates through the event", () => {
|
|
const root = buildVoyageTree(info, [parent, child], [trip])
|
|
const top = root.children[0]!
|
|
expect(top.event.location?.name).toBe("Paris")
|
|
expect(top.event.startDate).toBe("2026-06-13T08:00:00")
|
|
expect(top.event.endDate).toBe("2026-06-13T10:00:00")
|
|
})
|
|
|
|
it("sets dateLabel when there are dated roots", () => {
|
|
const root = buildVoyageTree(info, [parent, child], [trip])
|
|
expect(root.dateLabel).toBeTruthy()
|
|
expect(typeof root.dateLabel).toBe("string")
|
|
})
|
|
|
|
it("leaves dateLabel undefined when no roots are dated", () => {
|
|
const root = buildVoyageTree(info, [undated], [])
|
|
expect(root.dateLabel).toBeUndefined()
|
|
})
|
|
|
|
it("keeps undated events as children with null start", () => {
|
|
const root = buildVoyageTree(info, [parent, child, undated], [])
|
|
const undatedNode = root.children.find((n) => n.id === "3")
|
|
expect(undatedNode).toBeDefined()
|
|
expect(undatedNode!.event.startDate).toBeNull()
|
|
})
|
|
})
|