From 9397553ca4af5ea4ccd7ac6574f855c1a542d32a Mon Sep 17 00:00:00 2001 From: Niels Mokkenstorm Date: Wed, 8 Jul 2026 09:16:57 +0200 Subject: [PATCH] test(server): split discogs_client_test out of discogs_oauth_test; the file name now matches what it covers --- server/test/discogs_client_test.gleam | 97 ++++++++++++++++++++++++++ server/test/discogs_oauth_test.gleam | 99 +-------------------------- 2 files changed, 98 insertions(+), 98 deletions(-) create mode 100644 server/test/discogs_client_test.gleam diff --git a/server/test/discogs_client_test.gleam b/server/test/discogs_client_test.gleam new file mode 100644 index 0000000..47435a8 --- /dev/null +++ b/server/test/discogs_client_test.gleam @@ -0,0 +1,97 @@ +import at_record_server/discogs_client.{Release, SearchPage} +import gleam/list +import gleam/option.{None, Some} +import gleam/set + +const collection_body = "{ + \"pagination\": {\"page\": 1, \"pages\": 2}, + \"releases\": [ + {\"id\": 1, \"basic_information\": { + \"id\": 4577, \"title\": \"Selected Ambient Works 85-92\", \"year\": 1992, + \"artists\": [{\"name\": \"Aphex Twin\"}], + \"formats\": [{\"name\": \"Vinyl\"}, {\"name\": \"LP\"}], + \"thumb\": \"https://i.discogs.com/t.jpg\", + \"cover_image\": \"https://i.discogs.com/c.jpg\"}}, + {\"id\": 2, \"basic_information\": { + \"id\": 9000, \"title\": \"Untitled\", \"year\": 0, + \"artists\": [{\"name\": \"A\"}, {\"name\": \"B\"}], + \"formats\": [], \"thumb\": \"\", \"cover_image\": \"\"}} + ] +}" + +pub fn collection_page_decodes_basic_information_test() { + let send = fn(_req) { Ok(#(200, collection_body)) } + let assert Ok(SearchPage(releases:, page: 1, pages: 2)) = + discogs_client.collection_page(send, "OAuth ...", "someone", 1) + let assert [first, second] = releases + assert first + == Release( + discogs_id: 4577, + title: "Selected Ambient Works 85-92", + artist: "Aphex Twin", + year: Some(1992), + format: Some("Vinyl, LP"), + thumb_url: Some("https://i.discogs.com/t.jpg"), + cover_url: Some("https://i.discogs.com/c.jpg"), + ) + // Year 0 means unknown; empty images collapse to None; artists join. + assert second.year == None + assert second.artist == "A, B" + assert second.thumb_url == None +} + +pub fn collection_page_rate_limit_test() { + let send = fn(_req) { Ok(#(429, "slow down")) } + assert discogs_client.collection_page(send, "OAuth ...", "someone", 1) + == Error("rate limited") +} + +// --- import planning --- + +fn release(id: Int) -> discogs_client.Release { + Release( + discogs_id: id, + title: "T", + artist: "A", + year: None, + format: None, + thumb_url: None, + cover_url: None, + ) +} + +pub fn plan_import_dedups_against_crate_and_within_run_test() { + let items = [release(1), release(2), release(1), release(3)] + // 2 is already in the crate with a cover. + let plan = + discogs_client.plan_import( + items, + set.from_list(["2"]), + set.from_list(["2"]), + 10, + ) + assert list.map(plan.new, fn(r) { r.discogs_id }) == [1, 3] + // Skips the covered id AND the duplicate within the page. + assert plan.skipped == 2 + assert set.contains(plan.seen, "3") +} + +pub fn plan_import_backfills_uncovered_entries_test() { + let items = [release(1), release(2)] + // 1 is in the crate but has no cover blob yet -> upsert bucket. + let plan = + discogs_client.plan_import(items, set.from_list(["1"]), set.new(), 10) + assert list.map(plan.backfill, fn(r) { r.discogs_id }) == [1] + assert list.map(plan.new, fn(r) { r.discogs_id }) == [2] + assert plan.skipped == 0 +} + +pub fn plan_import_respects_budget_across_buckets_test() { + let items = [release(1), release(2), release(3)] + // 1 needs a backfill; budget 2 covers it plus one new genesis. + let plan = + discogs_client.plan_import(items, set.from_list(["1"]), set.new(), 2) + assert list.map(plan.backfill, fn(r) { r.discogs_id }) == [1] + assert list.map(plan.new, fn(r) { r.discogs_id }) == [2] + assert plan.skipped == 0 +} diff --git a/server/test/discogs_oauth_test.gleam b/server/test/discogs_oauth_test.gleam index 12ad2eb..3f3b8d7 100644 --- a/server/test/discogs_oauth_test.gleam +++ b/server/test/discogs_oauth_test.gleam @@ -1,8 +1,6 @@ -import at_record_server/discogs_client.{Auth, Release, SearchPage} +import at_record_server/discogs_client.{Auth} import at_record_server/discogs_oauth.{TokenPair} -import gleam/list import gleam/option.{None, Some} -import gleam/set // Golden: the exact header (order, quoting, percent-encoding) is pinned. pub fn auth_header_without_token_test() { @@ -47,98 +45,3 @@ pub fn parse_token_body_test() { assert discogs_oauth.parse_token_body("nope=1") == Error("discogs token response missing oauth_token") } - -// --- collection page decoding --- - -const collection_body = "{ - \"pagination\": {\"page\": 1, \"pages\": 2}, - \"releases\": [ - {\"id\": 1, \"basic_information\": { - \"id\": 4577, \"title\": \"Selected Ambient Works 85-92\", \"year\": 1992, - \"artists\": [{\"name\": \"Aphex Twin\"}], - \"formats\": [{\"name\": \"Vinyl\"}, {\"name\": \"LP\"}], - \"thumb\": \"https://i.discogs.com/t.jpg\", - \"cover_image\": \"https://i.discogs.com/c.jpg\"}}, - {\"id\": 2, \"basic_information\": { - \"id\": 9000, \"title\": \"Untitled\", \"year\": 0, - \"artists\": [{\"name\": \"A\"}, {\"name\": \"B\"}], - \"formats\": [], \"thumb\": \"\", \"cover_image\": \"\"}} - ] -}" - -pub fn collection_page_decodes_basic_information_test() { - let send = fn(_req) { Ok(#(200, collection_body)) } - let assert Ok(SearchPage(releases:, page: 1, pages: 2)) = - discogs_client.collection_page(send, "OAuth ...", "someone", 1) - let assert [first, second] = releases - assert first - == Release( - discogs_id: 4577, - title: "Selected Ambient Works 85-92", - artist: "Aphex Twin", - year: Some(1992), - format: Some("Vinyl, LP"), - thumb_url: Some("https://i.discogs.com/t.jpg"), - cover_url: Some("https://i.discogs.com/c.jpg"), - ) - // Year 0 means unknown; empty images collapse to None; artists join. - assert second.year == None - assert second.artist == "A, B" - assert second.thumb_url == None -} - -pub fn collection_page_rate_limit_test() { - let send = fn(_req) { Ok(#(429, "slow down")) } - assert discogs_client.collection_page(send, "OAuth ...", "someone", 1) - == Error("rate limited") -} - -// --- import planning --- - -fn release(id: Int) -> discogs_client.Release { - Release( - discogs_id: id, - title: "T", - artist: "A", - year: None, - format: None, - thumb_url: None, - cover_url: None, - ) -} - -pub fn plan_import_dedups_against_crate_and_within_run_test() { - let items = [release(1), release(2), release(1), release(3)] - // 2 is already in the crate with a cover. - let plan = - discogs_client.plan_import( - items, - set.from_list(["2"]), - set.from_list(["2"]), - 10, - ) - assert list.map(plan.new, fn(r) { r.discogs_id }) == [1, 3] - // Skips the covered id AND the duplicate within the page. - assert plan.skipped == 2 - assert set.contains(plan.seen, "3") -} - -pub fn plan_import_backfills_uncovered_entries_test() { - let items = [release(1), release(2)] - // 1 is in the crate but has no cover blob yet -> upsert bucket. - let plan = - discogs_client.plan_import(items, set.from_list(["1"]), set.new(), 10) - assert list.map(plan.backfill, fn(r) { r.discogs_id }) == [1] - assert list.map(plan.new, fn(r) { r.discogs_id }) == [2] - assert plan.skipped == 0 -} - -pub fn plan_import_respects_budget_across_buckets_test() { - let items = [release(1), release(2), release(3)] - // 1 needs a backfill; budget 2 covers it plus one new genesis. - let plan = - discogs_client.plan_import(items, set.from_list(["1"]), set.new(), 2) - assert list.map(plan.backfill, fn(r) { r.discogs_id }) == [1] - assert list.map(plan.new, fn(r) { r.discogs_id }) == [2] - assert plan.skipped == 0 -} -- 2.51.2