Add .shoegaze document type and rig/board sharing

Introduce a single shareable document type for rigs and boards:

- ShoeGazeDocument: versioned JSON envelope wrapping either a RigArchive
  (one or many rigs) or a BoardVersion, with a tolerant decoder that also
  reads the legacy bare-RigArchive and bare-ShoeGazeState shapes.
- Register the .shoegaze UTType (net.realizedsound.shoegaze, conforms to
  public.json) in all four demo app Info.plists.
- Migrate library export/import onto the envelope: exportDocumentData() and
  a decoder-tolerant importArchive(); RigFileDocument and the Builder
  exporter/importer now use .shoegaze. Internal per-rig and board-version
  files stay app-private JSON.
- Add OS share-sheet entries via SwiftUI ShareLink: Share per rig in the
  library sidebar, Share per version in the board editor, backed by the
  Transferable wrappers RigShareItem / BoardShareItem.

Tests: ShoeGazeDocument round-trip and legacy decode, library .shoegaze
export/import round-trip, and a board-document-imports-no-rigs case.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-28 17:49:20 -07:00
parent 657ecaf4f4
commit 9e09eb284a
16 changed files with 737 additions and 33 deletions
@@ -149,8 +149,9 @@ struct RigLibraryStoreTests {
try store.save(entry)
// An archive carrying the SAME id must not overwrite the existing rig on import.
// Encode with the on-disk strategy (.secondsSince1970) so dates survive the import decoder.
let archive: RigArchive = RigArchive(rigs: [entry])
let data: Data = try JSONEncoder().encode(archive)
let data: Data = try ShoeGazeDocument.makeEncoder().encode(archive)
let added: Int = try store.importArchive(from: data)
#expect(added == 1)
#expect(store.entries.count == 2, "colliding id should be imported as a new entry")
@@ -163,10 +164,42 @@ struct RigLibraryStoreTests {
// A legacy file is a bare ShoeGazeState (no `rigs` array).
let legacyState: ShoeGazeState = ShoeGazeState(boards: [BoardState(label: "Legacy", segments: [])])
let data: Data = try JSONEncoder().encode(legacyState)
let data: Data = try ShoeGazeDocument.makeEncoder().encode(legacyState)
let added: Int = try store.importArchive(from: data, legacyRigName: "Recovered")
#expect(added == 1)
#expect(store.entries.first?.name == "Recovered")
store.flushPendingWrites()
}
@Test func exportDocumentThenImportRoundTrips() async throws {
let (store, dir): (RigLibraryStore, URL) = await makeStore()
defer { try? FileManager.default.removeItem(at: dir) }
try store.save(makeEntry("One"))
try store.save(makeEntry("Two"))
// The user-facing export is a `.shoegaze` ShoeGazeDocument envelope.
let documentData: Data = try store.exportDocumentData()
#expect(try ShoeGazeDocument.decoded(from: documentData).rigEntries?.count == 2)
let (other, otherDir): (RigLibraryStore, URL) = await makeStore()
defer { try? FileManager.default.removeItem(at: otherDir) }
let added: Int = try other.importArchive(from: documentData)
#expect(added == 2)
#expect(other.entries.map(\.name) == ["One", "Two"])
store.flushPendingWrites()
other.flushPendingWrites()
}
@Test func importBoardDocumentAddsNoRigs() async throws {
let (store, dir): (RigLibraryStore, URL) = await makeStore()
defer { try? FileManager.default.removeItem(at: dir) }
// A board-carrying `.shoegaze` document is valid but contributes no library rigs.
let boardDocument: ShoeGazeDocument =
ShoeGazeDocument(board: BoardVersion(name: "Lead", state: BoardState(label: "Lead", segments: [])))
let added: Int = try store.importArchive(from: try boardDocument.data())
#expect(added == 0)
#expect(store.entries.isEmpty)
store.flushPendingWrites()
}
}
@@ -0,0 +1,111 @@
// ShoeGazeDocumentTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
@testable import LyrebirdShoeGaze
@Suite("ShoeGazeDocument — share/export envelope")
struct ShoeGazeDocumentTests {
/// A minimal two-board rig state for snapshotting without an engine.
private func makeState(lead: String = "Lead") -> ShoeGazeState {
let board: BoardState = BoardState(label: lead, segments: [])
let other: BoardState = BoardState(label: "Clean", segments: [])
return ShoeGazeState(boards: [board, other])
}
private func makeEntry(name: String = "Ambient Wash") -> RigEntry {
RigEntry(name: name, state: makeState())
}
private func makeBoardVersion(name: String = "Lead Crunch") -> BoardVersion {
BoardVersion(name: name, state: BoardState(label: "Lead", segments: []))
}
// MARK: - Round trips
@Test func singleRigRoundTripIsLossless() throws {
let entry: RigEntry = makeEntry()
let document: ShoeGazeDocument = ShoeGazeDocument(rig: entry)
let data: Data = try document.data()
let decoded: ShoeGazeDocument = try ShoeGazeDocument.decoded(from: data)
#expect(decoded == document)
#expect(decoded.rigEntries == [entry])
#expect(decoded.boardVersion == nil)
#expect(decoded.version == shoeGazeDocumentFormatVersion)
}
@Test func multiRigRoundTripIsLossless() throws {
let entries: [RigEntry] = [makeEntry(name: "A"), makeEntry(name: "B")]
let document: ShoeGazeDocument = ShoeGazeDocument(rigs: entries)
let data: Data = try document.data()
let decoded: ShoeGazeDocument = try ShoeGazeDocument.decoded(from: data)
#expect(decoded == document)
#expect(decoded.rigEntries == entries)
}
@Test func boardRoundTripIsLossless() throws {
let boardVersion: BoardVersion = makeBoardVersion()
let document: ShoeGazeDocument = ShoeGazeDocument(board: boardVersion)
let data: Data = try document.data()
let decoded: ShoeGazeDocument = try ShoeGazeDocument.decoded(from: data)
#expect(decoded == document)
#expect(decoded.boardVersion == boardVersion)
#expect(decoded.rigEntries == nil)
}
// MARK: - Backward compatibility
@Test func decodesLegacyRigArchive() throws {
let entries: [RigEntry] = [makeEntry(name: "Old A"), makeEntry(name: "Old B")]
let archive: RigArchive = RigArchive(rigs: entries)
let encoder: JSONEncoder = ShoeGazeDocument.makeEncoder()
let data: Data = try encoder.encode(archive)
let decoded: ShoeGazeDocument = try ShoeGazeDocument.decoded(from: data)
#expect(decoded.rigEntries == entries)
}
@Test func decodesLegacyBareState() throws {
let state: ShoeGazeState = makeState()
let encoder: JSONEncoder = ShoeGazeDocument.makeEncoder()
let data: Data = try encoder.encode(state)
let decoded: ShoeGazeDocument = try ShoeGazeDocument.decoded(from: data, legacyRigName: "Recovered")
#expect(decoded.rigEntries?.count == 1)
#expect(decoded.rigEntries?.first?.name == "Recovered")
#expect(decoded.rigEntries?.first?.state == state)
}
@Test func rejectsGarbage() {
let data: Data = Data("not a shoegaze document".utf8)
#expect(throws: (any Error).self) {
_ = try ShoeGazeDocument.decoded(from: data)
}
}
// MARK: - On-disk shape
@Test func envelopeCarriesKindDiscriminator() throws {
let rigData: Data = try ShoeGazeDocument(rig: makeEntry()).data()
let boardData: Data = try ShoeGazeDocument(board: makeBoardVersion()).data()
let rigJSON: String = String(decoding: rigData, as: UTF8.self)
let boardJSON: String = String(decoding: boardData, as: UTF8.self)
#expect(rigJSON.contains("\"kind\" : \"rigs\""))
#expect(boardJSON.contains("\"kind\" : \"board\""))
}
}