import Testing @testable import HUDKit // The sizes a picture comes in, as the server says them (an 's // srcset), and the one a picture drawn so wide loads: the narrowest at // least that wide, else the widest; with none, its src. @Suite struct SourceSizes { @Test func aSrcsetIsReadNarrowestFirstWhatDoesNotReadLeftOut() { let sizes = Source.sizes("https://cdn.test/l.jpg 1440w, https://cdn.test/s.jpg 640w, https://cdn.test/x.jpg 2x, https://cdn.test/z.jpg 0w, bare") #expect(sizes == [.init(url: "https://cdn.test/s.jpg", width: 640), .init(url: "https://cdn.test/l.jpg", width: 1440)]) #expect(Source.sizes(nil).isEmpty) #expect(Source.sizes("").isEmpty) } @Test func theNarrowestThatFillsItIsLoadedElseTheWidestElseItsSrc() { let sized = Source(src: "https://cdn.test/t.jpg", fallback: nil, sizes: Source.sizes("https://cdn.test/s.jpg 640w, https://cdn.test/t.jpg 1000w, https://cdn.test/l.jpg 1440w")) #expect(sized.url(for: 300 * 2) == "https://cdn.test/s.jpg") #expect(sized.url(for: 300 * 3) == "https://cdn.test/t.jpg") #expect(sized.url(for: 1000) == "https://cdn.test/t.jpg") #expect(sized.url(for: 600 * 2) == "https://cdn.test/l.jpg") #expect(sized.url(for: 1000 * 3) == "https://cdn.test/l.jpg") #expect(Source(src: "https://cdn.test/t.jpg", fallback: nil).url(for: 3000) == "https://cdn.test/t.jpg") } /// An item's sizes go with its picture: a slide's, a gallery photo's, a cover's. @Test func anItemsSizesGoWithItsPicture() { let item: JSON = ["src": ["src": "https://cdn.test/a.jpg", "fallback": "https://hud.test/blob/a"], "srcset": "https://cdn.test/a-640.jpg 640w, https://cdn.test/a.jpg 1440w"] let source = Resolved.source(of: item) #expect(source?.fallback == "https://hud.test/blob/a") #expect(source?.sizes.map(\.width) == [640, 1440]) #expect(ResolvedSlides.slides([item])[0].source == source) #expect(Resolved.source(of: ["src": .null, "srcset": "https://cdn.test/a.jpg 640w"]) == nil) #expect(Resolved.source(of: ["src": ["src": "https://cdn.test/a.jpg"]])?.sizes.isEmpty == true) } }