|
| 1 | +@testable import ExFig |
| 2 | +@testable import FigmaAPI |
| 3 | +import XCTest |
| 4 | + |
| 5 | +final class ComponentPreFetcherTests: XCTestCase { |
| 6 | + var tempDirectory: URL! |
| 7 | + |
| 8 | + override func setUp() { |
| 9 | + super.setUp() |
| 10 | + tempDirectory = FileManager.default.temporaryDirectory |
| 11 | + .appendingPathComponent(UUID().uuidString) |
| 12 | + try? FileManager.default.createDirectory(at: tempDirectory, withIntermediateDirectories: true) |
| 13 | + } |
| 14 | + |
| 15 | + override func tearDown() { |
| 16 | + try? FileManager.default.removeItem(at: tempDirectory) |
| 17 | + super.tearDown() |
| 18 | + } |
| 19 | + |
| 20 | + // MARK: - Context Preservation Tests |
| 21 | + |
| 22 | + func testPreFetchOutsideBatchModeCreatesLocalContext() async throws { |
| 23 | + // Given: No existing batch context |
| 24 | + XCTAssertNil(BatchContextStorage.context) |
| 25 | + |
| 26 | + let client = MockClient() |
| 27 | + let params = Params.make(lightFileId: "file123") |
| 28 | + |
| 29 | + // Mock components response |
| 30 | + let mockComponents = [ |
| 31 | + Component.make(nodeId: "1:1", name: "icon_test", frameName: "Icons"), |
| 32 | + ] |
| 33 | + client.setResponse(mockComponents, for: ComponentsEndpoint.self) |
| 34 | + |
| 35 | + // When: Pre-fetching components outside batch mode |
| 36 | + var capturedContext: BatchContext? |
| 37 | + _ = try await ComponentPreFetcher.withPreFetchedComponentsIfNeeded( |
| 38 | + client: client, |
| 39 | + params: params |
| 40 | + ) { |
| 41 | + capturedContext = BatchContextStorage.context |
| 42 | + return "result" |
| 43 | + } |
| 44 | + |
| 45 | + // Then: Local context was created with components only |
| 46 | + XCTAssertNotNil(capturedContext) |
| 47 | + XCTAssertNotNil(capturedContext?.components) |
| 48 | + XCTAssertNil(capturedContext?.versions) |
| 49 | + XCTAssertNil(capturedContext?.granularCache) |
| 50 | + XCTAssertNil(capturedContext?.nodes) |
| 51 | + |
| 52 | + // And: Context is no longer available after closure |
| 53 | + XCTAssertNil(BatchContextStorage.context) |
| 54 | + } |
| 55 | + |
| 56 | + func testPreFetchPreservesExistingVersionsInBatchMode() async throws { |
| 57 | + // Given: Existing batch context with versions |
| 58 | + let existingVersions = PreFetchedFileVersions(versions: ["fileA": makeMetadata(version: "v1")]) |
| 59 | + let existingContext = BatchContext(versions: existingVersions) |
| 60 | + |
| 61 | + let client = MockClient() |
| 62 | + let params = Params.make(lightFileId: "file123") |
| 63 | + |
| 64 | + // Mock components response |
| 65 | + let mockComponents = [ |
| 66 | + Component.make(nodeId: "1:1", name: "icon_test", frameName: "Icons"), |
| 67 | + ] |
| 68 | + client.setResponse(mockComponents, for: ComponentsEndpoint.self) |
| 69 | + |
| 70 | + // When: Pre-fetching components inside batch mode with existing versions |
| 71 | + var capturedContext: BatchContext? |
| 72 | + await BatchContextStorage.$context.withValue(existingContext) { |
| 73 | + do { |
| 74 | + _ = try await ComponentPreFetcher.withPreFetchedComponentsIfNeeded( |
| 75 | + client: client, |
| 76 | + params: params |
| 77 | + ) { |
| 78 | + capturedContext = BatchContextStorage.context |
| 79 | + return "result" |
| 80 | + } |
| 81 | + } catch { |
| 82 | + XCTFail("Unexpected error: \(error)") |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // Then: Both components and versions are available |
| 87 | + XCTAssertNotNil(capturedContext?.components) |
| 88 | + XCTAssertNotNil(capturedContext?.versions) |
| 89 | + XCTAssertEqual(capturedContext?.versions?.metadata(for: "fileA")?.version, "v1") |
| 90 | + } |
| 91 | + |
| 92 | + func testPreFetchPreservesAllExistingContextFields() async throws { |
| 93 | + // Given: Existing batch context with all fields populated |
| 94 | + let existingVersions = PreFetchedFileVersions(versions: ["fileA": makeMetadata(version: "v1")]) |
| 95 | + let existingNodes = PreFetchedNodes(nodes: ["fileA": [:]]) |
| 96 | + |
| 97 | + var cache = ImageTrackingCache() |
| 98 | + cache.updateFileVersion(fileId: "fileA", version: "v1") |
| 99 | + let cachePath = tempDirectory.appendingPathComponent("test-cache.json") |
| 100 | + let existingGranularCache = SharedGranularCache(cache: cache, cachePath: cachePath) |
| 101 | + |
| 102 | + let existingContext = BatchContext( |
| 103 | + versions: existingVersions, |
| 104 | + components: nil, // No components yet |
| 105 | + granularCache: existingGranularCache, |
| 106 | + nodes: existingNodes |
| 107 | + ) |
| 108 | + |
| 109 | + let client = MockClient() |
| 110 | + let params = Params.make(lightFileId: "file123") |
| 111 | + |
| 112 | + // Mock components response |
| 113 | + let mockComponents = [ |
| 114 | + Component.make(nodeId: "1:1", name: "icon_test", frameName: "Icons"), |
| 115 | + ] |
| 116 | + client.setResponse(mockComponents, for: ComponentsEndpoint.self) |
| 117 | + |
| 118 | + // When: Pre-fetching components inside batch mode with all context fields |
| 119 | + var capturedContext: BatchContext? |
| 120 | + await BatchContextStorage.$context.withValue(existingContext) { |
| 121 | + do { |
| 122 | + _ = try await ComponentPreFetcher.withPreFetchedComponentsIfNeeded( |
| 123 | + client: client, |
| 124 | + params: params |
| 125 | + ) { |
| 126 | + capturedContext = BatchContextStorage.context |
| 127 | + return "result" |
| 128 | + } |
| 129 | + } catch { |
| 130 | + XCTFail("Unexpected error: \(error)") |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + // Then: All context fields are preserved |
| 135 | + XCTAssertNotNil(capturedContext?.versions, "versions should be preserved") |
| 136 | + XCTAssertNotNil(capturedContext?.components, "components should be added") |
| 137 | + XCTAssertNotNil(capturedContext?.granularCache, "granularCache should be preserved") |
| 138 | + XCTAssertNotNil(capturedContext?.nodes, "nodes should be preserved") |
| 139 | + } |
| 140 | + |
| 141 | + func testPreFetchSkipsWhenComponentsAlreadyAvailable() async throws { |
| 142 | + // Given: Existing batch context already has components |
| 143 | + let existingComponents = PreFetchedComponents(components: ["fileA": [ |
| 144 | + Component.make(nodeId: "1:1", name: "existing_icon", frameName: "Icons"), |
| 145 | + ]]) |
| 146 | + let existingContext = BatchContext(components: existingComponents) |
| 147 | + |
| 148 | + let client = MockClient() |
| 149 | + let params = Params.make(lightFileId: "file123") |
| 150 | + |
| 151 | + // When: Pre-fetching components when they're already available |
| 152 | + var capturedContext: BatchContext? |
| 153 | + await BatchContextStorage.$context.withValue(existingContext) { |
| 154 | + do { |
| 155 | + _ = try await ComponentPreFetcher.withPreFetchedComponentsIfNeeded( |
| 156 | + client: client, |
| 157 | + params: params |
| 158 | + ) { |
| 159 | + capturedContext = BatchContextStorage.context |
| 160 | + return "result" |
| 161 | + } |
| 162 | + } catch { |
| 163 | + XCTFail("Unexpected error: \(error)") |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + // Then: Original components are preserved (no new fetch) |
| 168 | + XCTAssertEqual(client.requestCount, 0, "No API calls should be made") |
| 169 | + XCTAssertNotNil(capturedContext?.components) |
| 170 | + XCTAssertTrue(capturedContext?.components?.hasComponents(for: "fileA") ?? false) |
| 171 | + } |
| 172 | + |
| 173 | + // MARK: - Helpers |
| 174 | + |
| 175 | + private func makeMetadata(version: String) -> FileMetadata { |
| 176 | + let json = """ |
| 177 | + { |
| 178 | + "version": "\(version)", |
| 179 | + "name": "Test File", |
| 180 | + "lastModified": "2024-01-01T00:00:00Z" |
| 181 | + } |
| 182 | + """ |
| 183 | + // swiftlint:disable:next force_try |
| 184 | + return try! JSONDecoder().decode(FileMetadata.self, from: Data(json.utf8)) |
| 185 | + } |
| 186 | +} |
0 commit comments