Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.
Swift apple-siliconcustom-formathomebrewiosipaipadipadosiphonemetalnative-portnintendo-switchnxnativeromfsswitch-games
Library covers: the game's own product wins over editions and bundles
v0lumehi2d32645b76d5
diff --git a/frontend/README.md b/frontend/README.mdindex bd49c563..46bc3e5b 100644--- a/frontend/README.md+++ b/frontend/README.md@@ -219,6 +219,16 @@ ID → image and kept with the images in`~/Library/Application Support/NS Port Builder/artwork/`. Custom images stay attheir chosen path.+The list can hold several shop products under one title ID: the game, its Nintendo+Switch 2 Edition, or a bundle with a membership or an expansion pass. The launcher+takes the game's own product. A product whose name marks an edition, a bundle, a pass+or a demo loses, and of two equal ones the first in the shop wins. `sources.json` in+the same folder records where each image came from. An image saved by an older+launcher is checked once against this rule and replaced when it belongs to another+product. A banner with a promotion painted in (for example "Demo Available") cannot be+told from the list. Change artwork covers it. A game the list does not know, as most+Switch 2 games today, falls back to the screenshot, the app icon or the placeholder.+## TypeThe interface is set in Manrope (SIL Open Font License, with its license in `fonts/`):diff --git a/frontend/Sources/NX2Apple/Artwork.swift b/frontend/Sources/NX2Apple/Artwork.swiftindex 654b2e96..0d1d1f8d 100644--- a/frontend/Sources/NX2Apple/Artwork.swift+++ b/frontend/Sources/NX2Apple/Artwork.swift@@ -5,11 +5,19 @@ import NXCore/// community title database blawar/titledb lists for each title. The region list is large (about 90 MB), so/// it is fetched only while a game in the library has no banner yet, reduced to title ID → banner URL and/// kept beside the library; each banner is downloaded once into the same folder.+///+/// The list can hold several shop products under one title ID (LNPANDY/NX2Apple#403): the game, its Nintendo Switch 2+/// Edition, a bundle with a membership or an expansion pass. They share the ID and differ in name, nsuId and banner.+/// `BannerPick` chooses the game's own product. `sources.json` beside the banners records which URL each one came from, so+/// a banner saved before the rule (no record) is checked once against it and replaced when the pick differs.actor ArtworkStore {static let shared = ArtworkStore(directory: BannerCache.directory)static let titleListURL = URL(string: "https://raw.githubusercontent.com/blawar/titledb/master/US.en.json")!static let imageHosts: Set<String> = ["img-eshop.cdn.nintendo.net"]+ /// The index format. An index written by an older launcher (no version: the first product per title ID won) is fetched+ /// again once, so its picks are made by `BannerPick`.+ static let indexVersion = 2let directory: URLprivate let titleList: URL@@ -19,12 +27,10 @@ actor ArtworkStore {private var running = falseprivate struct Index: Codable {+ var version: Int?var fetched: Datevar banners: [String: String]- }- private struct Entry: Decodable {- let id: String?- let bannerUrl: String?+ var several: Set<String>? // title IDs the list holds more than one product with a banner for}init(directory: URL, titleList: URL = ArtworkStore.titleListURL, allowsFileURLs: Bool = false) {@@ -38,65 +44,164 @@ actor ArtworkStore {BannerCache.cached(titleID, in: directory)}- /// Download the banners the given titles lack. Returns the title IDs that got one.+ /// Download the banners the given titles lack, and replace one saved before `BannerPick` when the list holds another+ /// product for its title. Returns the title IDs whose banner is new.@discardableResult func fetchMissing(_ titleIDs: [String]) async -> [String] {+ var sources = readSources()let wanted = Set(titleIDs.map { $0.uppercased() }.filter { $0.range(of: "^[0-9A-F]{16}$", options: .regularExpression) != nil })- .filter { Self.cachedBanner($0, in: directory) == nil }+ .filter { Self.cachedBanner($0, in: directory) == nil || sources[$0] == nil }guard !wanted.isEmpty, !running else { return [] }running = truedefer { running = false }- guard let banners = await banners(for: wanted) else { return [] }+ guard let index = await index(for: wanted) else { return [] }+ let current = index.version == Self.indexVersionvar added: [String] = []for id in wanted.sorted() {- guard let link = banners[id], let url = URL(string: link), allowed(url) else { continue }+ let cached = Self.cachedBanner(id, in: directory) != nil+ guard let link = index.banners[id] else {+ if cached && current { sources[id] = "" } // the list no longer has it: keep the banner, check it no more+ continue+ }+ if cached {+ guard current else { continue } // an old index: its pick may be the wrong product+ if !(index.several ?? []).contains(id) { sources[id] = link; continue } // one product: the saved banner is it+ }+ guard let url = URL(string: link), allowed(url) else { continue }do {let (data, response) = try await URLSession.shared.data(from: url)if let http = response as? HTTPURLResponse, http.statusCode != 200 { continue }guard data.count < 20 << 20, NSImage(data: data) != nil else { continue }try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)try data.write(to: directory.appendingPathComponent(id + ".jpg"), options: .atomic)+ if current { sources[id] = link }added.append(id)} catch { fputs("artwork: \(id): \(error.localizedDescription)\n", stderr) }}+ writeSources(sources)return added}- /// The title ID → banner URL index: the saved one while it is fresh or knows every wanted title, else the list again.- private func banners(for wanted: Set<String>) async -> [String: String]? {+ /// The title ID → banner URL index: the saved one while it is current and fresh or knows every wanted title, else the list again.+ private func index(for wanted: Set<String>) async -> Index? {let file = directory.appendingPathComponent("index.json")if index == nil, let data = try? Data(contentsOf: file) {let decoder = JSONDecoder(); decoder.dateDecodingStrategy = .iso8601index = try? decoder.decode(Index.self, from: data)}- if let index, wanted.isSubset(of: index.banners.keys) || Date().timeIntervalSince(index.fetched) < 7 * 86400 || listFailed {- return index.banners+ if let index, listFailed || index.version == Self.indexVersion+ && (wanted.isSubset(of: index.banners.keys) || Date().timeIntervalSince(index.fetched) < 7 * 86400) {+ return index}guard !listFailed else { return nil }do {let (data, response) = try await URLSession.shared.data(from: titleList)if let http = response as? HTTPURLResponse, http.statusCode != 200 { throw URLError(.badServerResponse) }- let entries = try JSONDecoder().decode([String: Entry].self, from: data)- var banners: [String: String] = [:]- for entry in entries.values {- guard let id = entry.id?.uppercased(), let banner = entry.bannerUrl, !banner.isEmpty, banners[id] == nil else { continue }- banners[id] = banner- }- let fresh = Index(fetched: Date(), banners: banners)+ let entries = try JSONDecoder().decode([String: BannerPick.Product].self, from: data)+ let picked = BannerPick.banners(Array(entries.values))+ let fresh = Index(version: Self.indexVersion, fetched: Date(), banners: picked.banners, several: picked.several)index = freshtry FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)let encoder = JSONEncoder(); encoder.dateEncodingStrategy = .iso8601try encoder.encode(fresh).write(to: file, options: .atomic)- return banners+ return fresh} catch {listFailed = truefputs("artwork: title list \(titleList.absoluteString): \(error.localizedDescription)\n", stderr)- return index?.banners+ return index}}+ /// Title ID → the URL its saved banner came from ("" when the list no longer names one). A banner without a record was+ /// saved by an older launcher.+ private func readSources() -> [String: String] {+ guard let data = try? Data(contentsOf: directory.appendingPathComponent("sources.json")) else { return [:] }+ return (try? JSONDecoder().decode([String: String].self, from: data)) ?? [:]+ }++ private func writeSources(_ sources: [String: String]) {+ guard sources != readSources(), FileManager.default.fileExists(atPath: directory.path) else { return }+ try? JSONEncoder().encode(sources).write(to: directory.appendingPathComponent("sources.json"), options: .atomic)+ }+/// Only Nintendo's image CDN over HTTPS: the list is third-party data.private func allowed(_ url: URL) -> Bool {if allowsFileURLs && url.isFileURL { return true }return url.scheme == "https" && Self.imageHosts.contains(url.host ?? "")}}++/// Which of the products the title list holds under one title ID gives the game's banner (LNPANDY/NX2Apple#403).+///+/// In blawar/titledb's US list (checked 2026-11-04) 167 of the 19,416 base title IDs with a banner have more than one+/// product with different banners. The ID, the category and the size are the same or close for all of them. What tells+/// them apart is the name: the other products add "Nintendo Switch 2 Edition", "+ Nintendo Switch Online Membership",+/// "Expansion Pass", "Bundle", "Collection", "Deluxe", "Complete Edition" or "Retail Version" to the game's name. So each+/// product scores one point per such mark in its name (a demo many more), the lowest score wins, and on a tie the+/// lowest nsuId, the product that came to the shop first. That leaves 3 IDs where two products have the same marks and+/// the tie decides (Cadence of Hyrule, Dragon Marked for Death, Police Simulator). Banners with a promotion painted in+/// ("Demo Available", an award) cannot be told from the list: the player's own image (Manage > Change artwork) covers them.+enum BannerPick {+ struct Product: Decodable, Equatable {+ var id: String?+ var name: String?+ var nsuId: Int64?+ var isDemo: Bool?+ var bannerUrl: String?++ init(id: String?, name: String?, nsuId: Int64?, isDemo: Bool? = nil, bannerUrl: String?) {+ self.id = id; self.name = name; self.nsuId = nsuId; self.isDemo = isDemo; self.bannerUrl = bannerUrl+ }++ private enum CodingKeys: String, CodingKey { case id, name, nsuId, isDemo, bannerUrl }++ /// Third-party data: a field of an unexpected type is left out instead of failing the whole list.+ init(from decoder: Decoder) throws {+ let c = try decoder.container(keyedBy: CodingKeys.self)+ id = try? c.decodeIfPresent(String.self, forKey: .id)+ name = try? c.decodeIfPresent(String.self, forKey: .name)+ if let number = try? c.decodeIfPresent(Int64.self, forKey: .nsuId) { nsuId = number }+ else if let text = try? c.decodeIfPresent(String.self, forKey: .nsuId) { nsuId = Int64(text) }+ else { nsuId = nil }+ isDemo = try? c.decodeIfPresent(Bool.self, forKey: .isDemo)+ bannerUrl = try? c.decodeIfPresent(String.self, forKey: .bannerUrl)+ }+ }++ /// Words in a product's name that mark another product than the game alone: an edition, a bundle, a pass, a demo.+ static let marks: [String] = [+ "switch\\W*2\\b", "edition", "\\+", "membership", "\\bpass\\b", "bundle", "\\bpacks?\\b", "collection", "ultimate",+ "deluxe", "\\bplus\\b", "\\blite\\b", "\\bset\\b", "retail", "upgrade", "\\bdemo\\b", "\\btrial\\b",+ ]++ static func score(_ product: Product) -> Int {+ let name = product.name ?? ""+ let words = marks.filter { name.range(of: $0, options: [.regularExpression, .caseInsensitive]) != nil }.count+ return words + (product.isDemo == true ? 100 : 0)+ }++ /// The product whose banner the game gets, among those with a banner; nil when none has one.+ static func pick(_ products: [Product]) -> Product? {+ products.filter { !($0.bannerUrl ?? "").isEmpty }.min { a, b in+ let (sa, sb) = (score(a), score(b))+ if sa != sb { return sa < sb }+ if a.nsuId != b.nsuId { return (a.nsuId ?? .max) < (b.nsuId ?? .max) }+ return (a.bannerUrl ?? "") < (b.bannerUrl ?? "") // the same product twice: any order gives the same answer+ }+ }++ /// Title ID → the picked banner URL, and the IDs that had more than one banner to choose from.+ static func banners(_ products: [Product]) -> (banners: [String: String], several: Set<String>) {+ var byID: [String: [Product]] = [:]+ for product in products {+ guard let id = product.id?.uppercased(), !(product.bannerUrl ?? "").isEmpty else { continue }+ byID[id, default: []].append(product)+ }+ var banners: [String: String] = [:]+ var several: Set<String> = []+ for (id, list) in byID {+ if let banner = pick(list)?.bannerUrl { banners[id] = banner }+ if Set(list.compactMap(\.bannerUrl)).count > 1 { several.insert(id) }+ }+ return (banners, several)+ }+}diff --git a/frontend/Tests/NX2AppleTests/ArtworkPickTests.swift b/frontend/Tests/NX2AppleTests/ArtworkPickTests.swiftnew file mode 100644index 00000000..e80585e3--- /dev/null+++ b/frontend/Tests/NX2AppleTests/ArtworkPickTests.swift@@ -0,0 +1,288 @@+import AppKit+import SwiftUI+import XCTest+@testable import NX2Apple+@testable import NXCore+import NXModel+import NXLauncher++/// The library's cover pick (LNPANDY/NX2Apple#403): blawar/titledb lists several shop products under one title ID, the game+/// and its Switch 2 Edition or a bundle, and the game's own banner must win. The lists here copy the shape and the names of+/// the real US list (2026-11-04), with placeholder images served from disk.+@MainActor final class ArtworkPickTests: IsolatedDefaultsTestCase {+ private var temporary: URL!++ override func setUpWithError() throws {+ temporary = FileManager.default.temporaryDirectory.appendingPathComponent("nxnative-artwork-" + UUID().uuidString)+ try FileManager.default.createDirectory(at: temporary, withIntermediateDirectories: true)+ }++ override func tearDownWithError() throws { try? FileManager.default.removeItem(at: temporary) }++ private func product(_ id: String, _ name: String, _ nsuId: Int64, demo: Bool = false, banner: String? = nil) -> BannerPick.Product {+ BannerPick.Product(id: id, name: name, nsuId: nsuId, isDemo: demo, bannerUrl: banner ?? "https://img-eshop.cdn.nintendo.net/i/\(nsuId).jpg")+ }++ /// The pairs from the issue and the other kinds the real list holds: the game's product wins in either order.+ func testTheGameWinsOverItsEditionsBundlesAndPasses() {+ let cases: [(game: BannerPick.Product, other: BannerPick.Product)] = [+ (product("0100C2500FC20000", "Splatoon™ 3", 70010000046395),+ product("0100C2500FC20000", "Splatoon™ 3 + Nintendo Switch Online Individual Membership (3 Months)", 70010000066990)),+ (product("010015100B514000", "Super Mario Bros.™ Wonder", 70010000068688),+ product("010015100B514000", "Super Mario Bros.™ Wonder – Nintendo Switch™ 2 Edition + Meetup in Bellabel Park", 70010000115567)),+ (product("01004D300C5AE000", "Kirby™ and the Forgotten Land", 70010000046405),+ product("01004D300C5AE000", "Kirby™ and the Forgotten Land – Nintendo Switch™ 2 Edition + Star-Crossed World", 70010000096829)),+ (product("0100F2C0115B6000", "The Legend of Zelda™: Tears of the Kingdom", 70010000063714),+ product("0100F2C0115B6000", "The Legend of Zelda™: Tears of the Kingdom – Nintendo Switch™ 2 Edition", 70010000096821)),+ (product("0100633007D48000", "Hollow Knight", 70010000003208),+ product("0100633007D48000", "Hollow Knight – Nintendo Switch 2 Edition", 70010000117998)),+ // The game's own name says Definitive Edition: both products carry "edition", the Switch 2 one more+ (product("010027400CDC6000", "Divinity: Original Sin 2 - Definitive Edition", 70010000015971),+ product("010027400CDC6000", "Divinity: Original Sin 2 - Nintendo Switch™ 2 Edition", 70010000114263)),+ // The bundle came to the shop first here: the lowest nsuId alone would pick it+ (product("0100D680194B2000", "Pikmin™ 2", 70010000068673), product("0100D680194B2000", "Pikmin™ 1+2", 70010000059262)),+ (product("01008DB008C2C000", "Pokémon™ Shield", 70010000004492),+ product("01008DB008C2C000", "Pokémon Shield + Pokémon Shield Expansion Pass", 70010000035969)),+ (product("0100A2F00EEFC000", "Disney Classic Games: Aladdin and The Lion King", 70010000022309),+ product("0100A2F00EEFC000", "Disney Classic Games Collection", 70010000047661)),+ (product("0100E1E00CF1A000", "Power Rangers: Battle for the Grid", 70010000016742),+ product("0100E1E00CF1A000", "Power Rangers: Battle for the Grid - Retail Version", 70010000030832)),+ (product("0100ABC000000000", "A Game", 70010000099999), product("0100ABC000000000", "A Game", 70010000000001, demo: true)),+ ]+ for (game, other) in cases {+ XCTAssertEqual(BannerPick.pick([game, other]), game, game.name ?? "")+ XCTAssertEqual(BannerPick.pick([other, game]), game, game.name ?? "")+ }+ }++ /// Two products with the same marks: the data cannot tell them apart, so the one that came to the shop first wins, in any order.+ func testATieKeepsTheFirstProductInTheShop() {+ let first = product("01000B900D8B0000", "Cadence of Hyrule: Crypt of the NecroDancer Featuring The Legend of Zelda", 70010000021364)+ let retail = product("01000B900D8B0000", "Cadence of Hyrule: Crypt of the NecroDancer featuring The Legend of Zelda", 70010000036004)+ XCTAssertEqual(BannerPick.pick([first, retail]), first)+ XCTAssertEqual(BannerPick.pick([retail, first]), first)+ XCTAssertNil(BannerPick.pick([product("0100000000000000", "No banner", 1, banner: "")]))+ let index = BannerPick.banners([first, retail, product("0100633007D48000", "Hollow Knight", 70010000003208)])+ XCTAssertEqual(index.banners["01000B900D8B0000"], first.bannerUrl)+ XCTAssertEqual(index.several, ["01000B900D8B0000"])+ }++ // MARK: The store, with the list and the images on disk++ private func image(_ color: NSColor, _ label: String, to url: URL) throws {+ let picture = NSImage(size: NSSize(width: 640, height: 360), flipped: false) { rect in+ color.setFill(); rect.fill()+ let style = NSMutableParagraphStyle(); style.alignment = .center+ (label as NSString).draw(in: rect.insetBy(dx: 24, dy: 130), withAttributes: [+ .font: NSFont.boldSystemFont(ofSize: 34), .foregroundColor: NSColor.white, .paragraphStyle: style])+ return true+ }+ let tiff = try XCTUnwrap(picture.tiffRepresentation)+ try XCTUnwrap(NSBitmapImageRep(data: tiff)?.representation(using: .jpeg, properties: [:])).write(to: url)+ }++ private struct Fixture {+ let list: URL, folder: URL+ let splatoon: URL, bundle: URL, wonder: URL, edition: URL, bananza: URL+ }++ /// A list shaped like the real one (keyed by nsuId, each product a full entry) with the issue's two pairs, a Switch 2 game+ /// it knows (Donkey Kong Bananza) and none for Splatoon Raiders, as in the US, GB, DE, FR and JP lists.+ private func fixture() throws -> Fixture {+ let cdn = temporary.appendingPathComponent("cdn")+ try FileManager.default.createDirectory(at: cdn, withIntermediateDirectories: true)+ let urls = ["splatoon", "bundle", "wonder", "edition", "bananza"].map { cdn.appendingPathComponent($0 + ".jpg") }+ try image(.systemTeal, "Splatoon 3\n(the game)", to: urls[0])+ try image(.systemPink, "Splatoon 3 + membership\n(a bundle)", to: urls[1])+ try image(.systemGreen, "Super Mario Bros. Wonder\n(the game)", to: urls[2])+ try image(.systemRed, "Wonder Switch 2 Edition\n(another product)", to: urls[3])+ try image(.systemOrange, "Donkey Kong Bananza\n(Switch 2 game)", to: urls[4])+ func entry(_ nsu: String, _ id: String, _ name: String, _ banner: URL, demo: Bool = false, nsuAsText: Bool = false) -> String {+ """+ "\(nsu)": {"id": "\(id)", "name": "\(name)", "nsuId": \(nsuAsText ? "\"\(nsu)\"" : nsu), "isDemo": \(demo), "bannerUrl": "\(banner.absoluteString)",+ "category": ["Action"], "size": 1, "ids": null, "rightsId": null}+ """+ }+ let list = temporary.appendingPathComponent("US.en.json")+ try Data(("{" + [+ entry("70010000066990", "0100C2500FC20000", "Splatoon™ 3 + Nintendo Switch Online Individual Membership (3 Months)", urls[1]),+ entry("70010000046395", "0100C2500FC20000", "Splatoon™ 3", urls[0]),+ entry("70010000115567", "010015100B514000", "Super Mario Bros.™ Wonder – Nintendo Switch™ 2 Edition + Meetup in Bellabel Park", urls[3]),+ entry("70010000068688", "010015100B514000", "Super Mario Bros.™ Wonder", urls[2], nsuAsText: true),+ entry("70010000096809", "040074a01bf12000", "Donkey Kong™ Bananza", urls[4]),+ entry("70010000000001", "0100C2500FC20000", "Splatoon™ 3 Demo", urls[1], demo: true), // first in the shop, but a demo+ #""1": {"id": null, "bannerUrl": null}"#,+ ].joined(separator: ",\n") + "}").utf8).write(to: list)+ return Fixture(list: list, folder: temporary.appendingPathComponent("artwork"),+ splatoon: urls[0], bundle: urls[1], wonder: urls[2], edition: urls[3], bananza: urls[4])+ }++ private func bytes(_ url: URL?) -> Data? { url.flatMap { try? Data(contentsOf: $0) } }++ /// A new library: each title gets its game's banner, the demo product is skipped, and a title the list lacks gets none.+ func testTheStoreSavesTheGamesBannerForEachTitle() async throws {+ let f = try fixture()+ let store = ArtworkStore(directory: f.folder, titleList: f.list, allowsFileURLs: true)+ let added = await store.fetchMissing(["0100C2500FC20000", "010015100B514000", "040074A01BF12000", "0400380020056000"])+ XCTAssertEqual(added, ["010015100B514000", "0100C2500FC20000", "040074A01BF12000"])+ XCTAssertEqual(bytes(ArtworkStore.cachedBanner("0100C2500FC20000", in: f.folder)), bytes(f.splatoon))+ XCTAssertEqual(bytes(ArtworkStore.cachedBanner("010015100B514000", in: f.folder)), bytes(f.wonder))+ XCTAssertEqual(bytes(ArtworkStore.cachedBanner("040074A01BF12000", in: f.folder)), bytes(f.bananza))+ XCTAssertNil(ArtworkStore.cachedBanner("0400380020056000", in: f.folder)) // Splatoon Raiders: in no list+ let again = await store.fetchMissing(["0100C2500FC20000", "010015100B514000", "040074A01BF12000"])+ XCTAssertEqual(again, [])+ }++ /// An older launcher kept the first product per ID (here the bundle and the Switch 2 Edition) in an index with no version.+ /// The update fetches the list once, replaces those two banners, keeps the one of a title with a single product without+ /// downloading it, and after that needs the list no more.+ func testABannerSavedByAnOlderLauncherIsReplaced() async throws {+ let f = try fixture()+ try FileManager.default.createDirectory(at: f.folder, withIntermediateDirectories: true)+ try Data(contentsOf: f.bundle).write(to: f.folder.appendingPathComponent("0100C2500FC20000.jpg"))+ try Data(contentsOf: f.edition).write(to: f.folder.appendingPathComponent("010015100B514000.jpg"))+ let ownBananza = f.folder.appendingPathComponent("040074A01BF12000.jpg")+ try image(.systemBrown, "saved earlier", to: ownBananza)+ let saved = try Data(contentsOf: ownBananza)+ let fresh = ISO8601DateFormatter().string(from: Date())+ try Data("""+ {"fetched": "\(fresh)", "banners": {"0100C2500FC20000": "\(f.bundle.absoluteString)", "010015100B514000": "\(f.edition.absoluteString)",+ "040074A01BF12000": "\(f.bananza.absoluteString)"}}+ """.utf8).write(to: f.folder.appendingPathComponent("index.json"))++ let ids = ["0100C2500FC20000", "010015100B514000", "040074A01BF12000"]+ let store = ArtworkStore(directory: f.folder, titleList: f.list, allowsFileURLs: true)+ let replaced = await store.fetchMissing(ids)+ XCTAssertEqual(replaced, ["010015100B514000", "0100C2500FC20000"])+ XCTAssertEqual(bytes(ArtworkStore.cachedBanner("0100C2500FC20000", in: f.folder)), bytes(f.splatoon))+ XCTAssertEqual(bytes(ArtworkStore.cachedBanner("010015100B514000", in: f.folder)), bytes(f.wonder))+ XCTAssertEqual(bytes(ArtworkStore.cachedBanner("040074A01BF12000", in: f.folder)), saved) // one product: not fetched again+ let index = try XCTUnwrap(JSONSerialization.jsonObject(with: Data(contentsOf: f.folder.appendingPathComponent("index.json"))) as? [String: Any])+ XCTAssertEqual(index["version"] as? Int, ArtworkStore.indexVersion)++ // The next launch: the list is gone, and nothing asks for it+ try FileManager.default.removeItem(at: f.list)+ let next = ArtworkStore(directory: f.folder, titleList: f.list, allowsFileURLs: true)+ let nothing = await next.fetchMissing(ids)+ XCTAssertEqual(nothing, [])+ XCTAssertEqual(bytes(ArtworkStore.cachedBanner("0100C2500FC20000", in: f.folder)), bytes(f.splatoon))+ }++ /// With the list unreachable, an old index still gives a missing title its banner but leaves the saved ones alone: they are+ /// checked once the list comes.+ func testAnOldIndexWithoutTheListKeepsTheSavedBanners() async throws {+ let f = try fixture()+ try FileManager.default.createDirectory(at: f.folder, withIntermediateDirectories: true)+ try Data(contentsOf: f.bundle).write(to: f.folder.appendingPathComponent("0100C2500FC20000.jpg"))+ try Data("""+ {"fetched": "\(ISO8601DateFormatter().string(from: Date()))", "banners": {"0100C2500FC20000": "\(f.bundle.absoluteString)",+ "010015100B514000": "\(f.wonder.absoluteString)"}}+ """.utf8).write(to: f.folder.appendingPathComponent("index.json"))+ let store = ArtworkStore(directory: f.folder, titleList: temporary.appendingPathComponent("missing.json"), allowsFileURLs: true)+ let added = await store.fetchMissing(["0100C2500FC20000", "010015100B514000"])+ XCTAssertEqual(added, ["010015100B514000"])+ XCTAssertEqual(bytes(ArtworkStore.cachedBanner("0100C2500FC20000", in: f.folder)), bytes(f.bundle))++ let later = ArtworkStore(directory: f.folder, titleList: f.list, allowsFileURLs: true)+ let replaced = await later.fetchMissing(["0100C2500FC20000", "010015100B514000"])+ XCTAssertEqual(replaced, ["010015100B514000", "0100C2500FC20000"]) // both were saved without a record: fetched once more+ XCTAssertEqual(bytes(ArtworkStore.cachedBanner("0100C2500FC20000", in: f.folder)), bytes(f.splatoon))+ }++ /// A Switch 2 game the list lacks (Splatoon Raiders): no banner and no error. The library shows the engine's screenshot of+ /// the game when the repository has one, else the placeholder.+ func testATitleTheListLacksFallsBack() async throws {+ let f = try fixture()+ let store = ArtworkStore(directory: f.folder, titleList: f.list, allowsFileURLs: true)+ let added = await store.fetchMissing(["0400380020056000"])+ XCTAssertEqual(added, [])+ let game = LibraryGame(titleID: "0400380020056000", name: "Splatoon Raiders")+ let repo = temporary.appendingPathComponent("repo")+ XCTAssertNil(GameLibrary.artwork(for: game, repo: repo.path, banners: f.folder))+ let images = repo.appendingPathComponent("docs/images")+ try FileManager.default.createDirectory(at: images, withIntermediateDirectories: true)+ try image(.systemBlue, "screenshot", to: images.appendingPathComponent("splatoon-raiders.jpg"))+ XCTAssertEqual(GameLibrary.artwork(for: game, repo: repo.path, banners: f.folder)?.size, NSSize(width: 640, height: 360))+ }++ // MARK: Visual QA (matched by --filter testRenderLauncherForVisualQA)++ /// The library before and after the update: covers an older launcher saved (the bundle, the Switch 2 Edition), then the+ /// game's own after the store ran. Donkey Kong Bananza (Switch 2, in the list) has its cover, Splatoon Raiders (Switch 2,+ /// in no list) the placeholder. The capture's banner folder is its own, so each card shows its saved banner through the+ /// game's artwork path.+ @MainActor func testRenderLauncherForVisualQACovers() async throws {+ guard let directory = ProcessInfo.processInfo.environment["NX_LAUNCHER_CAPTURE_DIR"] else { throw XCTSkip("Set NX_LAUNCHER_CAPTURE_DIR to capture native views") }+ let output = URL(fileURLWithPath: directory)+ try FileManager.default.createDirectory(at: output, withIntermediateDirectories: true)+ let f = try fixture()+ try FileManager.default.createDirectory(at: f.folder, withIntermediateDirectories: true)+ try Data(contentsOf: f.bundle).write(to: f.folder.appendingPathComponent("0100C2500FC20000.jpg"))+ try Data(contentsOf: f.edition).write(to: f.folder.appendingPathComponent("010015100B514000.jpg"))+ try Data(contentsOf: f.bananza).write(to: f.folder.appendingPathComponent("040074A01BF12000.jpg"))+ try Data("""+ {"fetched": "\(ISO8601DateFormatter().string(from: Date()))", "banners": {}}+ """.utf8).write(to: f.folder.appendingPathComponent("index.json"))++ // A stand-in engine: the marker and the two Switch 2 profiles' first lines, so their cards carry the badge+ let repo = temporary.appendingPathComponent("repo")+ func write(_ path: String, _ text: String) throws {+ let url = repo.appendingPathComponent(path)+ try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)+ try Data(text.utf8).write(to: url)+ }+ try write("engine/" + EngineLayout.marker, "")+ try write("engine/dm/games/040074A01BF12000/game.tsv", "# Donkey Kong Bananza (Switch 2, update v458752)\nname\tDonkey Kong Bananza\n")+ try write("engine/dm/games/0400380020056000/game.tsv", "# Splatoon Raiders (Switch 2, update v65536)\nname\tSplatoon Raiders\n")++ NSApplication.shared.setActivationPolicy(.accessory)+ let stores = LauncherStores(defaults: testDefaults); let model = stores.build+ stores.repo.path = repo.path+ model.lang = CaptureLanguage.current(.en)+ stores.dependencies.checked = true+ stores.dependencies.deps = [Dep(id: "test", name: "Xcode", hint: "", present: true, installCmd: [])]+ let library = GameLibrary(file: temporary.appendingPathComponent("preview.json"))+ let games = [("0100C2500FC20000", "Splatoon 3"), ("010015100B514000", "Super Mario Bros. Wonder"),+ ("040074A01BF12000", "Donkey Kong Bananza"), ("0400380020056000", "Splatoon Raiders")]+ var ids: [String: UUID] = [:]+ for (titleID, name) in games { ids[titleID] = try library.addApp(makeApp(titleID, name: name)) }+ func showSaved() {+ for (titleID, _) in games {+ guard let id = ids[titleID] else { continue }+ library.setArtwork(ArtworkStore.cachedBanner(titleID, in: f.folder)?.path ?? "", for: id)+ }+ library.selectedID = ids["0100C2500FC20000"]+ }+ func capture(_ name: String) throws {+ let host = NSHostingView(rootView: LauncherView(stores: stores, library: library, initialPage: .library, performStartupChecks: false))+ let window = OffscreenWindow(size: NSSize(width: 1440, height: 1024), styleMask: [.titled, .closable, .resizable])+ window.contentView = host+ window.orderFront(nil)+ RunLoop.main.run(until: Date().addingTimeInterval(0.8))+ host.layoutSubtreeIfNeeded()+ let rep = try XCTUnwrap(host.bitmapImageRepForCachingDisplay(in: host.bounds))+ host.cacheDisplay(in: host.bounds, to: rep)+ try XCTUnwrap(rep.representation(using: .png, properties: [:])).write(to: output.appendingPathComponent(name + ".png"))+ window.orderOut(nil)+ }+ showSaved()+ try capture("library-covers-before")+ let store = ArtworkStore(directory: f.folder, titleList: f.list, allowsFileURLs: true)+ _ = await store.fetchMissing(games.map(\.0))+ showSaved()+ try capture("library-covers-after")+ }++ private func makeApp(_ titleID: String, name: String) throws -> URL {+ let app = temporary.appendingPathComponent(name + ".app")+ let executables = app.appendingPathComponent("Contents/MacOS")+ try FileManager.default.createDirectory(at: executables, withIntermediateDirectories: true)+ for tool in ["nxnative", "dmhost"] {+ try FileManager.default.createSymbolicLink(at: executables.appendingPathComponent(tool), withDestinationURL: URL(fileURLWithPath: "/usr/bin/true"))+ }+ let info: [String: Any] = ["DMTitleID": titleID, "CFBundleDisplayName": name, "CFBundleExecutable": "nxnative",+ "CFBundleIdentifier": "org.nxnative.frontendtests.artwork." + temporary.lastPathComponent]+ try PropertyListSerialization.data(fromPropertyList: info, format: .xml, options: 0).write(to: app.appendingPathComponent("Contents/Info.plist"))+ return app+ }+}