diff --git a/Tests/StackNudgePanelCoreTests/ExtensionSourceTests.swift b/Tests/StackNudgePanelCoreTests/ExtensionSourceTests.swift index 17f93eb..26e06c1 100644 --- a/Tests/StackNudgePanelCoreTests/ExtensionSourceTests.swift +++ b/Tests/StackNudgePanelCoreTests/ExtensionSourceTests.swift @@ -473,4 +473,89 @@ final class ExtensionSourceTests: XCTestCase { XCTAssertEqual(actual.ignoredSources, ["nonsense"]) XCTAssertEqual(actual.entries.map(\.id), ["derby"]) } + + // MARK: - Installs that predate .sources.json + + private func listed(_ id: String, from source: ExtensionSource?, version: String = "1") + -> ExtensionInstaller.IndexEntry { + var listing = entry(id, version: version) + listing.source = source?.slug + return listing + } + + private let outsideRepo = ExtensionSource(owner: "someone", repo: "extensions") + + func testAnInstallWithATierAndNoSourceTakesTheOneSourceThatListsIt() { + let actual = ExtensionTiers.adopting([:], tiered: ["claude-usage"], + listings: [listed("derby", from: nil), + listed("claude-usage", from: internalSource)]) + XCTAssertEqual(actual, ["claude-usage": internalSource.slug]) + } + + func testARecordedSourceIsNeverReplaced() { + let recorded = ["claude-usage": outsideRepo.slug] + let actual = ExtensionTiers.adopting(recorded, tiered: ["claude-usage"], + listings: [listed("claude-usage", from: internalSource)]) + XCTAssertEqual(actual, recorded) + } + + func testNothingIsAdoptedWhenTheAppsOwnIndexListsIt() { + let actual = ExtensionTiers.adopting([:], tiered: ["derby"], + listings: [listed("derby", from: nil), + listed("derby", from: internalSource)]) + XCTAssertEqual(actual, [:]) + } + + func testNothingIsAdoptedWhenTwoSourcesListIt() { + let actual = ExtensionTiers.adopting([:], tiered: ["radar"], + listings: [listed("radar", from: internalSource), + listed("radar", from: outsideRepo)]) + XCTAssertEqual(actual, [:]) + } + + // No tier means it wasn't installed from an index: put there by hand, so + // a source listing the same id says nothing about it. + func testACopyWithNoTierIsNeverAdopted() { + let actual = ExtensionTiers.adopting([:], tiered: [], + listings: [listed("claude-usage", from: internalSource)]) + XCTAssertEqual(actual, [:]) + } + + func testAdoptingWritesTheSourceBesideTheTiers() throws { + let path = FileManager.default.temporaryDirectory + .appendingPathComponent("adopt-\(UUID().uuidString)/.tiers.json").path + addTeardownBlock { + try? FileManager.default.removeItem(atPath: (path as NSString).deletingLastPathComponent) + } + try FileManager.default.createDirectory(atPath: (path as NSString).deletingLastPathComponent, + withIntermediateDirectories: true) + try #"{"claude-usage":"view","hand-made":"view"}"#.write(toFile: path, atomically: true, + encoding: .utf8) + ExtensionTiers.adoptUnrecordedSources(listings: [listed("claude-usage", from: internalSource)], + path: path) + let sourcesPath = ExtensionTiers.sourcesPath(beside: path) + XCTAssertEqual(ExtensionTiers.readSources(path: sourcesPath), ["claude-usage": internalSource.slug]) + XCTAssertEqual(ExtensionTiers.read(path: path), ["claude-usage": .view, "hand-made": .view], + "the tiers themselves are left to reconcile") + } + + // The load that names the source is the one that shows the update: the + // sources are read for the rows only after adopting has run. + func testACatalogueLoadAdoptsBeforeItReadsTheSources() { + var adopted: [String: String] = [:] + let listing = listed("claude-usage", from: internalSource, version: "0.3.1") + let catalog = ExtensionCatalog( + fetchCatalogue: { .success(ExtensionInstaller.Catalogue(entries: [listing])) }, + background: { $0() }, toMain: { $0() }, recordedTiers: { ["claude-usage": .view] }, + recordedSources: { adopted }, + adoptSources: { listings in + adopted = ExtensionTiers.adopting(adopted, tiered: ["claude-usage"], listings: listings) + }) + catalog.reload() + XCTAssertEqual(catalog.installedFrom, ["claude-usage": internalSource.slug]) + let row = catalog.settingsRows(installed: [manifest("claude-usage", version: "0.1.0")], + refused: []).first + XCTAssertEqual(row?.availableVersion, "0.3.1") + XCTAssertTrue(row?.updateAvailable == true) + } } diff --git a/docs/extensions.md b/docs/extensions.md index 0e83cf0..1204574 100644 --- a/docs/extensions.md +++ b/docs/extensions.md @@ -511,6 +511,13 @@ away, so a demoted service restarts with its smaller grant (or stops, below `service`). Until the catalogue loads, the recorded tier stands. Removing an extension forgets its tier and its source. +An install from an extra catalogue made before hosts recorded sources has a +tier and no source, so it reads as the app's own and no catalogue offers it an +update. When the catalogue loads, such an extension is given the source that +must have been its own: the app's catalogue doesn't list it and exactly one +extra catalogue does. One with no tier was put there by hand and is left alone, +as is one that two catalogues list. + **For developing an extension** that no release's index lists yet, set `STACKNUDGE_EXTENSIONS_LOCAL_TIERS=true` in `~/.stack-nudge/config`. The host then never re-checks `.tiers.json` against the index, so a tier you write there diff --git a/panel/ExtensionTier.swift b/panel/ExtensionTier.swift index 8ea728e..92d9809 100644 --- a/panel/ExtensionTier.swift +++ b/panel/ExtensionTier.swift @@ -155,6 +155,29 @@ enum ExtensionTiers { return sources } + // An install from an extra source that predates .sources.json has a tier + // and no source, so it reads as the app's own: no source's listing is its + // update, and it stays on the version it was installed at. Its source is + // recorded here when that can only be one source: the app's own index + // doesn't list the id and exactly one extra source does. A copy with no + // tier wasn't installed from an index at all (it was put there by hand), + // so another source listing its id still says nothing about it, and is + // never adopted. Nothing else changes: the tier is then re-checked against + // that source's listing like any other, and an update still asks first. + static func adopting(_ sources: [String: String], tiered: Set, + listings: [ExtensionInstaller.IndexEntry]) -> [String: String] { + var sources = sources + for id in tiered where sources[id] == nil { + let listed = listings.filter { $0.id == id } + guard !listed.contains(where: { $0.source == nil }), + let slug = listed.first?.source, + listed.allSatisfy({ ExtensionSource.same($0.source, slug) }) + else { continue } + sources[id] = slug + } + return sources + } + // A fresh index, re-checked against what's recorded. Each recorded tier is // checked against the listing from the source it was installed from // (`sources`; none is the app's own): it goes down to that listing's tier @@ -238,6 +261,19 @@ enum ExtensionTiers { return true } + // See `adopting`. Run on every catalogue load, before the sources are read + // for the rows, so the load that can name the source is the one that shows + // the update. + static func adoptUnrecordedSources(listings: [ExtensionInstaller.IndexEntry], + path: String = ExtensionTiers.path) { + fileLock.lock() + defer { fileLock.unlock() } + let sourcesPath = sourcesPath(beside: path) + let recorded = readSources(path: sourcesPath) + let next = adopting(recorded, tiered: Set(read(path: path).keys), listings: listings) + if next != recorded { writeSources(next, path: sourcesPath) } + } + static func forget(_ id: String, path: String = ExtensionTiers.path) { fileLock.lock() defer { fileLock.unlock() } diff --git a/panel/ExtensionsBrowser.swift b/panel/ExtensionsBrowser.swift index 70848bd..7184002 100644 --- a/panel/ExtensionsBrowser.swift +++ b/panel/ExtensionsBrowser.swift @@ -115,6 +115,7 @@ final class ExtensionCatalog: ObservableObject { private let probeRequirement: (String) -> ExtensionInstaller.RequirementCheck private let recordedTiers: () -> [String: ExtensionTier] private let recordedSources: () -> [String: String] + private let adoptSources: ([ExtensionInstaller.IndexEntry]) -> Void private let now: () -> Date init(fetchCatalogue: @escaping () -> Result, @@ -132,11 +133,14 @@ final class ExtensionCatalog: ObservableObject { = { ExtensionInstaller.checkRequirement($0) }, recordedTiers: @escaping () -> [String: ExtensionTier] = { ExtensionTiers.read() }, recordedSources: @escaping () -> [String: String] = { ExtensionTiers.readSources() }, + // Writes .sources.json, so it does nothing unless the app passes it. + adoptSources: @escaping ([ExtensionInstaller.IndexEntry]) -> Void = { _ in }, now: @escaping () -> Date = Date.init) { self.now = now self.probeRequirement = probeRequirement self.recordedTiers = recordedTiers self.recordedSources = recordedSources + self.adoptSources = adoptSources self.fetchCatalogue = fetchCatalogue self.performInstall = performInstall self.performRemove = performRemove @@ -201,6 +205,7 @@ final class ExtensionCatalog: ObservableObject { background { [weak self] in guard let self else { return } let result = self.fetchCatalogue() + if case .success(let catalogue) = result { self.adoptSources(catalogue.listings) } let sources = self.recordedSources() self.toMain { [weak self] in guard let self else { return } diff --git a/panel/Panel.swift b/panel/Panel.swift index bfa6fad..c2c66f6 100644 --- a/panel/Panel.swift +++ b/panel/Panel.swift @@ -1331,7 +1331,8 @@ final class PanelController: NSObject, NSApplicationDelegate, PanelKeyDelegate, // A fetched catalogue is what gives an installed row something to // compare against, so the Settings list is rebuilt when one lands. self.refreshInstalledExtensions() - }) + }, + adoptSources: { ExtensionTiers.adoptUnrecordedSources(listings: $0) }) // Extensions publish their tabs through nav, so nav stays the single source // of tab order and this stays the single source of what's in them. private lazy var extensions = ExtensionHost(onTabsChanged: { [weak self] tabs in