import { describe, expect, it } from "vitest"; import { type BookSnapshot, type ReadStatus, toBookHiveFields, toGoodreadsFields, toProgressPercent } from "./mapping.ts"; function bookWith(overrides: Partial): BookSnapshot { return { bookId: 30, title: "Dune", authors: ["Frank Herbert"], isbn13: "9780441172719", goodreadsId: null, status: "reading", startedOn: null, finishedOn: null, rating: null, review: null, ...overrides, }; } // The status mapping table of the approved spec. const EXPECTED_BY_STATUS: [ReadStatus, string, string][] = [ ["want_to_read", "to-read", "buzz.bookhive.defs#wantToRead"], ["reading", "currently-reading", "buzz.bookhive.defs#reading"], ["rereading", "currently-reading", "buzz.bookhive.defs#reading"], ["on_hold", "paused", "buzz.bookhive.defs#reading"], ["read", "read", "buzz.bookhive.defs#finished"], ["skimmed", "read", "buzz.bookhive.defs#finished"], ["abandoned", "did-not-finish", "buzz.bookhive.defs#abandoned"], ]; describe("status mapping", () => { it.each(EXPECTED_BY_STATUS)("maps %s to the Goodreads shelf %s and the BookHive status %s", (status, shelf, bookHiveStatus) => { const book = bookWith({ status }); expect(toGoodreadsFields(book)?.shelf).toBe(shelf); expect(toBookHiveFields(book)?.status).toBe(bookHiveStatus); }); it("gives no fields to either target for an unread book", () => { const book = bookWith({ status: "unread", rating: 4, review: "Great" }); expect(toGoodreadsFields(book)).toBeNull(); expect(toBookHiveFields(book)).toBeNull(); }); }); describe("field mapping", () => { it("sends 4 stars to Goodreads and 8 half stars to BookHive", () => { const book = bookWith({ rating: 4 }); expect(toGoodreadsFields(book)?.rating).toBe(4); expect(toBookHiveFields(book)?.stars).toBe(8); }); it("sends no rating when the book has none", () => { const book = bookWith({ rating: null }); expect(toGoodreadsFields(book)?.rating).toBeNull(); expect(toBookHiveFields(book)?.stars).toBeNull(); }); it("sends the start date to BookHive only", () => { const book = bookWith({ status: "read", startedOn: "2026-10-01", finishedOn: "2026-10-19" }); expect(toBookHiveFields(book)).toMatchObject({ startedAt: "2026-10-01T00:00:00.000Z", finishedAt: "2026-10-19T00:00:00.000Z" }); expect(toGoodreadsFields(book)).toEqual({ shelf: "read", rating: null, readAt: "2026-10-19", review: null }); }); it("sends the review to both targets", () => { const book = bookWith({ review: "Slow start, great ending." }); expect(toGoodreadsFields(book)?.review).toBe("Slow start, great ending."); expect(toBookHiveFields(book)?.review).toBe("Slow start, great ending."); }); }); describe("toProgressPercent", () => { it.each([ [11.226599, 11], [11.5, 12], [0, 0], [100, 100], [-0.4, 0], [100.6, 100], ])("turns %f into %i", (endProgress, expected) => { expect(toProgressPercent(endProgress)).toBe(expected); }); });