iOS client for Grain grain.social
ios photography atproto
7
fork

Configure Feed

Select the types of activity you want to include in your feed.

test: add FeedCache round-trip tests

Cover save/load round-trip, missing-key empty return, empty-save
preserving prior data, and keys with slashes/colons/spaces.

authored by

Hima Aramona and committed by
Chad Miller
360677e8 b0f6fd24

+59
+59
GrainTests/FeedCacheTests.swift
··· 1 + @testable import Grain 2 + import XCTest 3 + 4 + @MainActor 5 + final class FeedCacheTests: XCTestCase { 6 + private func makeGallery(uri: String, cid: String = "bafytest") -> GrainGallery { 7 + GrainGallery( 8 + uri: uri, 9 + cid: cid, 10 + creator: GrainProfile(cid: "bafyprofile", did: "did:plc:test", handle: "tester.test"), 11 + indexedAt: "2026-04-11T00:00:00Z" 12 + ) 13 + } 14 + 15 + override func tearDown() { 16 + let dir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0] 17 + .appendingPathComponent("grain_feed_cache", isDirectory: true) 18 + if let files = try? FileManager.default.contentsOfDirectory(at: dir, includingPropertiesForKeys: nil) { 19 + for file in files { 20 + try? FileManager.default.removeItem(at: file) 21 + } 22 + } 23 + super.tearDown() 24 + } 25 + 26 + func testRoundTripPersistsAndLoadsGalleries() { 27 + let key = "test_round_trip" 28 + let input = [ 29 + makeGallery(uri: "at://a", cid: "cid-a"), 30 + makeGallery(uri: "at://b", cid: "cid-b"), 31 + ] 32 + FeedCache.shared.save(input, key: key) 33 + let loaded = FeedCache.shared.load(key: key) 34 + XCTAssertEqual(loaded.map(\.uri), ["at://a", "at://b"]) 35 + XCTAssertEqual(loaded.map(\.cid), ["cid-a", "cid-b"]) 36 + } 37 + 38 + func testLoadReturnsEmptyForMissingKey() { 39 + XCTAssertEqual(FeedCache.shared.load(key: "test_missing_key").count, 0) 40 + } 41 + 42 + func testSaveIgnoresEmptyArrayAndPreservesPriorData() { 43 + let key = "test_empty_save" 44 + let seed = [makeGallery(uri: "at://seed")] 45 + FeedCache.shared.save(seed, key: key) 46 + 47 + FeedCache.shared.save([], key: key) 48 + 49 + let loaded = FeedCache.shared.load(key: key) 50 + XCTAssertEqual(loaded.map(\.uri), ["at://seed"]) 51 + } 52 + 53 + func testKeyWithSlashesColonsAndSpacesRoundTrips() { 54 + let key = "feed/home:pinned 1" 55 + let input = [makeGallery(uri: "at://tricky")] 56 + FeedCache.shared.save(input, key: key) 57 + XCTAssertEqual(FeedCache.shared.load(key: key).map(\.uri), ["at://tricky"]) 58 + } 59 + }