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
63 changes: 63 additions & 0 deletions app/slides/slidesData.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { describe, it, expect } from "vitest"
import { compareSlides, type SlideData } from "./slidesData"

function makeSlide(slug: string, timestamp: string, title: string): SlideData {
return { slug, metadata: { title, description: "", author: "", timestamp } }
}

describe("compareSlides", () => {
it("sorts newer timestamps first", () => {
const older = makeSlide("a", "2024-01-01T00:00:00Z", "A")
const newer = makeSlide("b", "2025-06-15T00:00:00Z", "B")
expect(compareSlides(newer, older)).toBeLessThan(0)
expect(compareSlides(older, newer)).toBeGreaterThan(0)
})

it("uses title DESC as tiebreaker for equal timestamps", () => {
const slideA = makeSlide("a", "2024-01-01T00:00:00Z", "Alpha")
const slideZ = makeSlide("z", "2024-01-01T00:00:00Z", "Zulu")
expect(compareSlides(slideZ, slideA)).toBeLessThan(0)
expect(compareSlides(slideA, slideZ)).toBeGreaterThan(0)
})

it("returns 0 for identical timestamp and title", () => {
const s1 = makeSlide("a", "2024-01-01T00:00:00Z", "Same")
const s2 = makeSlide("b", "2024-01-01T00:00:00Z", "Same")
expect(compareSlides(s1, s2)).toBe(0)
})

it("pushes slides with missing timestamp to the end", () => {
const valid = makeSlide("a", "2024-01-01T00:00:00Z", "A")
const noTs = makeSlide("b", "", "B")
expect(compareSlides(valid, noTs)).toBeLessThan(0)
expect(compareSlides(noTs, valid)).toBeGreaterThan(0)
})

it("pushes slides with invalid timestamp to the end", () => {
const valid = makeSlide("a", "2024-01-01T00:00:00Z", "A")
const invalid = makeSlide("b", "not-a-date", "B")
expect(compareSlides(valid, invalid)).toBeLessThan(0)
expect(compareSlides(invalid, valid)).toBeGreaterThan(0)
})

it("sorts two invalid-timestamp slides by title DESC", () => {
const alpha = makeSlide("a", "", "Alpha")
const zulu = makeSlide("z", "", "Zulu")
expect(compareSlides(zulu, alpha)).toBeLessThan(0)
expect(compareSlides(alpha, zulu)).toBeGreaterThan(0)
})

it("sorts a full array correctly", () => {
const slides = [
makeSlide("old", "2023-01-01T00:00:00Z", "Old Talk"),
makeSlide("no-ts", "", "No Timestamp"),
makeSlide("new", "2025-06-15T00:00:00Z", "New Talk"),
makeSlide("same-a", "2024-06-01T00:00:00Z", "Alpha"),
makeSlide("same-z", "2024-06-01T00:00:00Z", "Zulu")
]

const sorted = [...slides].sort(compareSlides)
const slugOrder = sorted.map((s) => s.slug)
expect(slugOrder).toEqual(["new", "same-z", "same-a", "old", "no-ts"])
})
})
20 changes: 19 additions & 1 deletion app/slides/slidesData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,25 @@ export function getSlideData(slug: string): SlideData | null {
}
}

export function compareSlides(a: SlideData, b: SlideData): number {
const aTime = Date.parse(a.metadata?.timestamp)
const bTime = Date.parse(b.metadata?.timestamp)
const aValid = !isNaN(aTime)
const bValid = !isNaN(bTime)

if (aValid && !bValid) return -1
if (!aValid && bValid) return 1
if (!aValid && !bValid) return (b.metadata?.title ?? "").localeCompare(a.metadata?.title ?? "")

if (aTime !== bTime) return bTime - aTime

return (b.metadata?.title ?? "").localeCompare(a.metadata?.title ?? "")
}

export function getAllSlides(): SlideData[] {
const slugs = getAllSlideSlugs()
return slugs.map((slug) => getSlideData(slug)).filter((data): data is SlideData => data !== null)
return slugs
.map((slug) => getSlideData(slug))
.filter((data): data is SlideData => data !== null)
.sort(compareSlides)
}
Loading