Zum Inhalt

apple/PhysarumApp/Tests/EmbedSnippetTests.swift

#if !os(tvOS)
import XCTest
import PhysarumEngine
@testable import Physarum

/// The web-embed snippet must be a self-contained `<physarum-player>` element whose inline `json:`
/// payload round-trips back to the exact preset params and is safe inside the single-quoted attribute.
final class EmbedSnippetTests: XCTestCase {
    func testSnippetShape() {
        let s = EmbedSnippet.make(PresetCatalog.fallbackPreset.params)
        XCTAssertTrue(s.contains(EmbedSnippet.bundleURL))
        XCTAssertTrue(s.contains("<script type=\"module\" src="))
        XCTAssertTrue(s.contains("<physarum-player preset='json:"))
        XCTAssertTrue(s.contains("'></physarum-player>"))
    }

    func testEmbeddedJSONRoundTripsAndIsAttributeSafe() throws {
        let params = PresetCatalog.loadAll()[0].params
        let s = EmbedSnippet.make(params)
        guard let lo = s.range(of: "preset='json:"),
              let hi = s.range(of: "'></physarum-player>") else { return XCTFail("no json payload") }
        let json = String(s[lo.upperBound ..< hi.lowerBound])
        // No single quote in the payload → safe inside the single-quoted `preset` attribute.
        XCTAssertFalse(json.contains("'"))
        let back = try JSONDecoder().decode(PresetParams.self, from: Data(json.utf8))
        XCTAssertEqual(back, params)
    }
}
#endif