-
Notifications
You must be signed in to change notification settings - Fork 10
fix(artists): deleteArtist must not hard-delete a shared canonical with song/catalog dependencies #770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
fix(artists): deleteArtist must not hard-delete a shared canonical with song/catalog dependencies #770
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
19033b1
fix(artists): deleteArtist must not hard-delete a shared canonical wi…
sweetmantech ea2fb2d
Merge branch 'main' into fix/deleteartist-shared-canonical-guard
sweetmantech d986c43
refactor(song_artists): replace bespoke existence helper with generic…
sweetmantech 7cf611a
refactor(song_artists): consolidate to one param-driven selectSongArt…
sweetmantech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
|
|
||
| import { deleteArtist } from "../deleteArtist"; | ||
| import { deleteAccountArtistId } from "@/lib/supabase/account_artist_ids/deleteAccountArtistId"; | ||
| import { getAccountArtistIds } from "@/lib/supabase/account_artist_ids/getAccountArtistIds"; | ||
| import { deleteAccountById } from "@/lib/supabase/accounts/deleteAccountById"; | ||
| import { selectSongArtists } from "@/lib/supabase/song_artists/selectSongArtists"; | ||
|
|
||
| vi.mock("@/lib/supabase/account_artist_ids/deleteAccountArtistId", () => ({ | ||
| deleteAccountArtistId: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/supabase/account_artist_ids/getAccountArtistIds", () => ({ | ||
| getAccountArtistIds: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/supabase/accounts/deleteAccountById", () => ({ | ||
| deleteAccountById: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/supabase/song_artists/selectSongArtists", () => ({ | ||
| selectSongArtists: vi.fn(), | ||
| })); | ||
|
|
||
| describe("deleteArtist", () => { | ||
| const artistId = "550e8400-e29b-41d4-a716-446655440000"; | ||
| const requesterAccountId = "660e8400-e29b-41d4-a716-446655440000"; | ||
| const link = { account_id: requesterAccountId, artist_id: artistId }; | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| vi.mocked(deleteAccountArtistId).mockResolvedValue([link] as never); | ||
| vi.mocked(deleteAccountById).mockResolvedValue(undefined as never); | ||
| }); | ||
|
|
||
| it("throws when the requester link could not be deleted", async () => { | ||
| vi.mocked(deleteAccountArtistId).mockResolvedValue([] as never); | ||
|
|
||
| await expect(deleteArtist({ artistId, requesterAccountId })).rejects.toThrow( | ||
| "Failed to delete artist link", | ||
| ); | ||
| expect(deleteAccountById).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("does not hard-delete when other roster links remain", async () => { | ||
| vi.mocked(getAccountArtistIds).mockResolvedValue([ | ||
| { account_id: "another-account", artist_id: artistId }, | ||
| ] as never); | ||
|
|
||
| const result = await deleteArtist({ artistId, requesterAccountId }); | ||
|
|
||
| expect(result).toBe(artistId); | ||
| expect(deleteAccountById).not.toHaveBeenCalled(); | ||
| expect(selectSongArtists).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("keeps the canonical account when the last link is removed but song dependencies exist", async () => { | ||
| vi.mocked(getAccountArtistIds).mockResolvedValue([] as never); | ||
| vi.mocked(selectSongArtists).mockResolvedValue([ | ||
| { id: "sa-1", song: "ISRC1", artist: artistId }, | ||
| ] as never); | ||
|
|
||
| const result = await deleteArtist({ artistId, requesterAccountId }); | ||
|
|
||
| expect(result).toBe(artistId); | ||
| expect(selectSongArtists).toHaveBeenCalledWith({ artists: [artistId] }); | ||
| expect(deleteAccountById).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("fails closed and keeps the canonical account when the dependency lookup errors", async () => { | ||
| vi.mocked(getAccountArtistIds).mockResolvedValue([] as never); | ||
| vi.mocked(selectSongArtists).mockResolvedValue(null); | ||
|
|
||
| const result = await deleteArtist({ artistId, requesterAccountId }); | ||
|
|
||
| expect(result).toBe(artistId); | ||
| expect(deleteAccountById).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("hard-deletes the account when the last link is removed and no dependencies exist", async () => { | ||
| vi.mocked(getAccountArtistIds).mockResolvedValue([] as never); | ||
| vi.mocked(selectSongArtists).mockResolvedValue([] as never); | ||
|
|
||
| const result = await deleteArtist({ artistId, requesterAccountId }); | ||
|
|
||
| expect(result).toBe(artistId); | ||
| expect(deleteAccountById).toHaveBeenCalledWith(artistId); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
lib/supabase/song_artists/__tests__/selectSongArtists.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
|
|
||
| import { selectSongArtists } from "../selectSongArtists"; | ||
|
|
||
| const mockFrom = vi.fn(); | ||
| const mockSelect = vi.fn(); | ||
| const mockIn = vi.fn(); | ||
|
|
||
| vi.mock("@/lib/supabase/serverClient", () => ({ | ||
| default: { | ||
| from: (...args: unknown[]) => mockFrom(...args), | ||
| }, | ||
| })); | ||
|
|
||
| describe("selectSongArtists", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| mockFrom.mockReturnValue({ select: mockSelect }); | ||
| mockSelect.mockReturnValue({ in: mockIn }); | ||
| }); | ||
|
|
||
| it("filters by song ISRCs", async () => { | ||
| const rows = [{ id: "sa-1", song: "ISRC1", artist: "artist-1" }]; | ||
| mockIn.mockResolvedValue({ data: rows, error: null }); | ||
|
|
||
| const result = await selectSongArtists({ songs: ["ISRC1"] }); | ||
|
|
||
| expect(mockFrom).toHaveBeenCalledWith("song_artists"); | ||
| expect(mockIn).toHaveBeenCalledWith("song", ["ISRC1"]); | ||
| expect(result).toEqual(rows); | ||
| }); | ||
|
|
||
| it("filters by artist account IDs", async () => { | ||
| const rows = [{ id: "sa-1", song: "ISRC1", artist: "artist-1" }]; | ||
| mockIn.mockResolvedValue({ data: rows, error: null }); | ||
|
|
||
| const result = await selectSongArtists({ artists: ["artist-1"] }); | ||
|
|
||
| expect(mockIn).toHaveBeenCalledWith("artist", ["artist-1"]); | ||
| expect(result).toEqual(rows); | ||
| }); | ||
|
|
||
| it("returns an empty array without querying when the filter list is empty", async () => { | ||
| const result = await selectSongArtists({ artists: [] }); | ||
|
|
||
| expect(result).toEqual([]); | ||
| expect(mockFrom).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("chunks large filter lists to keep .in() within URL limits", async () => { | ||
| mockIn.mockResolvedValue({ data: [], error: null }); | ||
| const songs = Array.from({ length: 450 }, (_, i) => `ISRC${i}`); | ||
|
|
||
| await selectSongArtists({ songs }); | ||
|
|
||
| // 450 / 200 => 3 chunks | ||
| expect(mockIn).toHaveBeenCalledTimes(3); | ||
| }); | ||
|
|
||
| it("returns null on query error (callers decide how to treat an unknown state)", async () => { | ||
| const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {}); | ||
| mockIn.mockResolvedValue({ data: null, error: { message: "boom" } }); | ||
|
|
||
| const result = await selectSongArtists({ artists: ["artist-1"] }); | ||
|
|
||
| expect(result).toBeNull(); | ||
| expect(consoleSpy).toHaveBeenCalled(); | ||
| consoleSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it("throws when neither songs nor artists is provided", async () => { | ||
| await expect(selectSongArtists({})).rejects.toThrow("Must provide either songs or artists"); | ||
| }); | ||
| }); |
62 changes: 0 additions & 62 deletions
62
lib/supabase/song_artists/__tests__/selectSongArtistsBySongs.test.ts
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import supabase from "../serverClient"; | ||
| import type { Tables } from "@/types/database.types"; | ||
|
|
||
| // PostgREST encodes .in() filters in the query string — chunk so large | ||
| // inputs (1,000+ ISRCs) can't overflow the URL. | ||
| const CHUNK_SIZE = 200; | ||
|
|
||
| /** | ||
| * Select song_artists rows filtered by song ISRCs or artist account IDs. | ||
| * | ||
| * Provide exactly one of `songs` or `artists` (songs takes precedence if both | ||
| * are given). The filter list is chunked to keep `.in()` within URL limits. | ||
| * | ||
| * @param params.songs - Song ISRCs to match on the `song` column | ||
| * @param params.artists - Artist account IDs to match on the `artist` column | ||
| * @returns The matching rows, or null on query error | ||
| */ | ||
| export async function selectSongArtists(params: { | ||
| songs?: string[]; | ||
| artists?: string[]; | ||
| }): Promise<Tables<"song_artists">[] | null> { | ||
| const { songs, artists } = params; | ||
| const column = songs ? "song" : artists ? "artist" : null; | ||
| const values = songs ?? artists; | ||
|
|
||
| if (!column || !values) { | ||
| throw new Error("Must provide either songs or artists"); | ||
| } | ||
| if (values.length === 0) return []; | ||
|
|
||
| const rows: Tables<"song_artists">[] = []; | ||
| for (let i = 0; i < values.length; i += CHUNK_SIZE) { | ||
| const chunk = values.slice(i, i + CHUNK_SIZE); | ||
| const { data, error } = await supabase.from("song_artists").select("*").in(column, chunk); | ||
|
|
||
| if (error) { | ||
| console.error("Error fetching song_artists:", error); | ||
| return null; | ||
| } | ||
|
|
||
| rows.push(...(data ?? [])); | ||
| } | ||
|
|
||
| return rows; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.