PulseCanvas / app · pulsecanvas.ai / scripts / NativeMigrationSmoke.swift
| 1 | import Foundation |
| 2 | |
| 3 | /// Direct-download edition: numeric scene indices saved before the native catalog grew |
| 4 | /// must keep pointing at the same external presets, never at newly added native designs. |
| 5 | @main struct NativeMigrationSmoke { |
| 6 | struct Stored: Codable { var presets: [Int: VisualPreset]; var identities: [Int: String]? } |
| 7 | |
| 8 | @MainActor static func main() throws { |
| 9 | let suite = "PulseCanvas.NativeMigration." + UUID().uuidString |
| 10 | let defaults = UserDefaults(suiteName: suite)! |
| 11 | defer { defaults.removePersistentDomain(forName: suite) } |
| 12 | let library = ProjectMPresets(defaults: defaults) |
| 13 | let nativeCount = RenderState.nativeSceneNames.count |
| 14 | precondition(nativeCount == NativeEffects.all.count, "Unexpected native catalog size \(nativeCount)") |
| 15 | let legacy = try JSONDecoder().decode([String].self, from: Data(contentsOf: library.bundledRoot.appendingPathComponent("legacy-identities.json"))) |
| 16 | |
| 17 | // 1. Every pre-expansion external index, with or without saved identities. |
| 18 | var resolved = 0, retired = 0 |
| 19 | for (offset, id) in legacy.enumerated() { |
| 20 | let old = NativeEffects.legacyCount + offset |
| 21 | if library.retiredIDs.contains(id) { |
| 22 | let fallback = try library.migratedScene(old, identities: nil) |
| 23 | precondition(fallback == 22) |
| 24 | retired += 1; continue |
| 25 | } |
| 26 | for identities in [nil, [old: id]] as [[Int: String]?] { |
| 27 | let scene = try library.migratedScene(old, identities: identities) |
| 28 | precondition(scene >= nativeCount, "Old external index \(old) became native scene \(scene)") |
| 29 | precondition(library.identity(for: scene) == id, "Old external index \(old) lost its identity") |
| 30 | } |
| 31 | resolved += 1 |
| 32 | } |
| 33 | // 2. Old indices that now overlap the new native range are external, explicitly. |
| 34 | for old in NativeEffects.legacyCount..<nativeCount where !library.retiredIDs.contains(legacy[old - NativeEffects.legacyCount]) { |
| 35 | let scene = try library.migratedScene(old, identities: nil) |
| 36 | precondition(scene >= nativeCount) |
| 37 | } |
| 38 | // 3. A saved identity map without an entry for an external index is an error, never native. |
| 39 | do { _ = try library.migratedScene(40, identities: [5: "native:5"]); preconditionFailure("Missing identity must not fall back to native") } catch is ProjectMError {} |
| 40 | // 4. Released native IDs stay put, retired ones open their replacement; native identities round-trip. |
| 41 | for scene in 0..<NativeEffects.legacyCount { let kept = try library.migratedScene(scene, identities: nil); precondition(kept == NativeEffects.resolved(scene)) } |
| 42 | let ids = library.identities(for: Set(0..<nativeCount)) |
| 43 | for scene in 0..<nativeCount { |
| 44 | precondition(ids[scene] == "native:\(scene)") |
| 45 | let direct = try library.migratedScene(scene, identities: ids) |
| 46 | let aliased = try library.migratedScene(scene + 5000, identities: [scene + 5000: "native:\(scene)"]) |
| 47 | precondition(direct == NativeEffects.resolved(scene) && aliased == direct) |
| 48 | } |
| 49 | print("PASS identities: \(resolved) legacy external presets keep their identity at index ≥ \(nativeCount), \(retired) retired fall back, natives 0–\(nativeCount - 1) round-trip") |
| 50 | |
| 51 | // 4b. Retired designs: IDs reserved, never offered, always resolved to an active design. |
| 52 | let retiredNative = NativeEffects.all.indices.filter(NativeEffects.isRetired) |
| 53 | let receipt = try JSONSerialization.jsonObject(with: Data(contentsOf: URL(fileURLWithPath: "docs/metal-keeper-selection.json"))) as! [String: Any] |
| 54 | let selected = Set(receipt["keepers"] as! [Int]) |
| 55 | // The review covered the first `reservedIDs` slots; designs appended after it are new and offered. |
| 56 | let reviewed = receipt["reservedIDs"] as! Int |
| 57 | precondition(Set(NativeEffects.activeIDs.filter { $0 < reviewed }) == selected, "Offered effects must match the approved red-label selection") |
| 58 | precondition(Set(retiredNative) == Set(0..<reviewed).subtracting(selected), "All unselected IDs must be reserved and withdrawn") |
| 59 | precondition(NativeEffects.activeIDs.filter { $0 >= reviewed } == Array(reviewed..<nativeCount), "Designs added after the review must be offered") |
| 60 | precondition(retiredNative.allSatisfy { !NativeEffects.isRetired(NativeEffects.resolved($0)) }) |
| 61 | precondition(Set(library.availableScenes).isDisjoint(with: retiredNative) && library.availableScenes.count == library.sceneNames.count - retiredNative.count) |
| 62 | precondition(Set(NativeEffects.timelineOrder).isDisjoint(with: retiredNative)) |
| 63 | let render = RenderState() |
| 64 | precondition(!NativeEffects.isRetired(render.scene), "A new session must open an active design") |
| 65 | for scene in retiredNative { render.scene = scene; precondition(render.scene == NativeEffects.resolved(scene)) } |
| 66 | print("PASS retired: \(retiredNative.count) withdrawn designs are hidden and resolve to replacements in projects and live state") |
| 67 | |
| 68 | // 5. Saved Studio styles from the 31-effect release. |
| 69 | var a = VisualPreset(); a.chaos = 0.11 |
| 70 | var b = VisualPreset(); b.chaos = 0.22 |
| 71 | var c = VisualPreset(); c.chaos = 0.33 |
| 72 | let firstActive = legacy.firstIndex { !library.retiredIDs.contains($0) }! |
| 73 | let laterActive = legacy.indices.dropFirst(40).first { !library.retiredIDs.contains(legacy[$0]) }! |
| 74 | let oldFirst = NativeEffects.legacyCount + firstActive, oldLater = NativeEffects.legacyCount + laterActive |
| 75 | precondition(oldLater < nativeCount, "Pick an old external index inside the new native range") |
| 76 | defaults.set(try JSONEncoder().encode(Stored(presets: [12: a, 5: c, oldFirst: b, oldLater: c], identities: nil)), forKey: "PulseCanvas.studio.v1") |
| 77 | let store = StudioStore(defaults: defaults) |
| 78 | precondition(store.presets[12] == a) |
| 79 | precondition(store.presets[5] == nil && store.presets[NativeEffects.resolved(5)] == nil, "A retired design's style must not attach to its replacement") |
| 80 | precondition(store.presets[library.scene(for: legacy[firstActive])!] == b) |
| 81 | precondition(store.presets[library.scene(for: legacy[laterActive])!] == c) |
| 82 | precondition(store.presets[oldFirst] == nil && store.presets[oldLater] == nil, "Old external styles must not attach to new native designs") |
| 83 | print("PASS saved styles: release-era styles at \(oldFirst) and \(oldLater) moved to their presets; native styles untouched") |
| 84 | |
| 85 | // 6. A release-era project with a timeline across the overlapping range. |
| 86 | let timeline = [StudioSegment(start: 0, end: 4, scene: 3), StudioSegment(start: 4, end: 8, scene: oldFirst), StudioSegment(start: 8, end: 12, scene: oldLater)] |
| 87 | let project = CreatorProject(scene: oldLater, appearance: c, aspect: "16:9", resolution: 1080, fps: 30, timeline: timeline, |
| 88 | studioEnabled: true, visualPresets: [3: a, oldFirst: b, oldLater: c], effectIdentities: nil) |
| 89 | let remapped = try project.remapped(using: library) |
| 90 | precondition(remapped.scene == library.scene(for: legacy[laterActive])!) |
| 91 | precondition(remapped.timeline!.map(\.scene) == [NativeEffects.resolved(3), library.scene(for: legacy[firstActive])!, library.scene(for: legacy[laterActive])!]) |
| 92 | precondition(remapped.visualPresets?[3] == nil && remapped.visualPresets?[NativeEffects.resolved(3)] == nil) |
| 93 | precondition(remapped.timeline!.allSatisfy { $0.scene < NativeEffects.legacyCount || $0.scene >= nativeCount }) |
| 94 | let saved = try JSONDecoder().decode(CreatorProject.self, from: JSONEncoder().encode(remapped)) |
| 95 | let reopened = try saved.remapped(using: library) |
| 96 | precondition(reopened.timeline!.map(\.scene) == remapped.timeline!.map(\.scene)) |
| 97 | print("PASS project: release-era timeline keeps ProjectM sections external and round-trips with stable identities") |
| 98 | |
| 99 | // 7. Favorites are stored by identity and survive catalog growth. |
| 100 | defaults.set(["native:12", "native:5", "native:31", legacy[firstActive]], forKey: "PulseCanvas.effectFavorites.v1") |
| 101 | let favorites = ProjectMPresets(defaults: defaults) |
| 102 | precondition(favorites.isFavorite(12) && favorites.isFavorite(31) && favorites.isFavorite(favorites.scene(for: legacy[firstActive])!)) |
| 103 | precondition(favorites.favoriteIDs == Set(["native:12", "native:31", legacy[firstActive]]), "A retired favorite must be removed rather than replaced") |
| 104 | precondition(!favorites.isFavorite(oldFirst), "An old external index must not inherit a favorite") |
| 105 | print("PASS favorites: native and external favorites keep their identities") |
| 106 | } |
| 107 | } |