124 lines
3.5 KiB
TypeScript
124 lines
3.5 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest"
|
|
|
|
const { mockDb } = vi.hoisted(() => ({
|
|
mockDb: {
|
|
artworkGallery: { upsert: vi.fn() },
|
|
artworkAlbum: { upsert: vi.fn() },
|
|
artworkCategory: { upsert: vi.fn() },
|
|
artworkTag: { upsert: vi.fn() },
|
|
artworkRendition: { upsert: vi.fn() },
|
|
mediaAsset: { create: vi.fn(), findUnique: vi.fn(), update: vi.fn(), delete: vi.fn() },
|
|
artwork: { create: vi.fn() },
|
|
gallery: { create: vi.fn() },
|
|
album: { create: vi.fn() },
|
|
category: { create: vi.fn() },
|
|
tag: { create: vi.fn() },
|
|
},
|
|
}))
|
|
|
|
vi.mock("./client", () => ({
|
|
db: mockDb,
|
|
}))
|
|
|
|
import {
|
|
attachArtworkRendition,
|
|
createArtwork,
|
|
createMediaAsset,
|
|
deleteMediaAsset,
|
|
getMediaAssetById,
|
|
linkArtworkToGrouping,
|
|
updateMediaAsset,
|
|
} from "./media-foundation"
|
|
|
|
describe("media foundation service", () => {
|
|
beforeEach(() => {
|
|
for (const value of Object.values(mockDb)) {
|
|
if ("upsert" in value) {
|
|
value.upsert.mockReset()
|
|
}
|
|
if ("create" in value) {
|
|
value.create.mockReset()
|
|
}
|
|
if ("findUnique" in value) {
|
|
value.findUnique.mockReset()
|
|
}
|
|
if ("update" in value) {
|
|
value.update.mockReset()
|
|
}
|
|
if ("delete" in value) {
|
|
value.delete.mockReset()
|
|
}
|
|
}
|
|
})
|
|
|
|
it("routes grouping links to the correct link table", async () => {
|
|
mockDb.artworkAlbum.upsert.mockResolvedValue({ id: "link" })
|
|
|
|
await linkArtworkToGrouping({
|
|
artworkId: "f40f4bcc-7148-45d7-a19d-856f7146a47e",
|
|
groupType: "album",
|
|
groupId: "f4e094df-0edf-4d5a-8b7b-c51f09cae95e",
|
|
})
|
|
|
|
expect(mockDb.artworkAlbum.upsert).toHaveBeenCalledTimes(1)
|
|
expect(mockDb.artworkGallery.upsert).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it("upserts rendition by artwork and slot", async () => {
|
|
mockDb.artworkRendition.upsert.mockResolvedValue({ id: "rendition" })
|
|
|
|
await attachArtworkRendition({
|
|
artworkId: "f40f4bcc-7148-45d7-a19d-856f7146a47e",
|
|
mediaAssetId: "f4e094df-0edf-4d5a-8b7b-c51f09cae95e",
|
|
slot: "thumbnail",
|
|
isPrimary: true,
|
|
})
|
|
|
|
expect(mockDb.artworkRendition.upsert).toHaveBeenCalledTimes(1)
|
|
expect(mockDb.artworkRendition.upsert.mock.calls[0]?.[0]).toMatchObject({
|
|
where: {
|
|
artworkId_slot: {
|
|
artworkId: "f40f4bcc-7148-45d7-a19d-856f7146a47e",
|
|
slot: "thumbnail",
|
|
},
|
|
},
|
|
})
|
|
})
|
|
|
|
it("parses and forwards media and artwork creation payloads", async () => {
|
|
mockDb.mediaAsset.create.mockResolvedValue({ id: "asset" })
|
|
mockDb.artwork.create.mockResolvedValue({ id: "artwork" })
|
|
|
|
await createMediaAsset({
|
|
type: "generic",
|
|
title: "Asset",
|
|
tags: [],
|
|
})
|
|
await createArtwork({
|
|
title: "Artwork",
|
|
slug: "artwork",
|
|
})
|
|
|
|
expect(mockDb.mediaAsset.create).toHaveBeenCalledTimes(1)
|
|
expect(mockDb.artwork.create).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it("handles media asset read/update/delete operations", async () => {
|
|
mockDb.mediaAsset.findUnique.mockResolvedValue({ id: "asset-1" })
|
|
mockDb.mediaAsset.update.mockResolvedValue({ id: "asset-1", title: "Updated" })
|
|
mockDb.mediaAsset.delete.mockResolvedValue({ id: "asset-1" })
|
|
|
|
await getMediaAssetById("asset-1")
|
|
await updateMediaAsset({
|
|
id: "c58f3aca-f958-4079-b2df-c9edf3a5fb0a",
|
|
title: "Updated",
|
|
tags: ["a", "b"],
|
|
})
|
|
await deleteMediaAsset("asset-1")
|
|
|
|
expect(mockDb.mediaAsset.findUnique).toHaveBeenCalledTimes(1)
|
|
expect(mockDb.mediaAsset.update).toHaveBeenCalledTimes(1)
|
|
expect(mockDb.mediaAsset.delete).toHaveBeenCalledTimes(1)
|
|
})
|
|
})
|