Cotton code
75 lines · 3.3 KB · SwiftRaw
// Project membership is its project identity. Record-filing screens never// write a membership note; core owns the brand slot writer after the Swift// database adapter was retired. The sweep keeps filing free of slot prose.import XCTest@testable import CottonUIfinal class ProjectsSheetCapTests: XCTestCase {    /// RULE: Swift record filing writes only membership identity. Native    /// brand-slot writes are owned by core; no deleted Swift adapter has an allowance.    func testNoSwiftFilingScreenWritesALinkNote() throws {        var offenders: [String] = []        for (name, text) in try AppSources.all() {            let code = SourceSweep.withoutComments(text)            guard code.contains("link_to_project") || code.contains("linkToProject") else { continue }            offenders += Self.noteWrites(in: code).map { "\(name):\($0)" }        }        XCTAssertEqual(offenders, [], "record filing writes only kind and id: \(offenders)")    }    /// Every line that writes a `note` ONTO A LINK RECORD, by its line number.    ///    /// Both forms, because the first sweep only knew the second and the brand    /// kit walked straight through it: an inline `"note": .string(role)` in    /// the record literal, and a `record["note"] = …` built up beside it. A    /// link record is `{kind, id, …}`, so a `"kind":` within a few lines is    /// what separates a note on a MEMBERSHIP from a screen patching some    /// record's own note column.    private static func noteWrites(in code: String) -> [Int] {        let lines = code.components(separatedBy: "\n")        var found: [Int] = []        for (index, line) in lines.enumerated() {            let writesANote =                line.range(of: #""note"\s*:"#, options: .regularExpression) != nil                || line.range(of: #"\["note"\]\s*="#, options: .regularExpression) != nil            guard writesANote else { continue }            let near = lines[max(0, index - 6)...min(lines.count - 1, index + 6)]                .joined(separator: "\n")            guard near.range(of: #""kind"\s*:"#, options: .regularExpression) != nil else {                continue            }            found.append(index + 1)        }        return found    }    /// The matcher fires on BOTH shapes and on neither innocent one — written    /// against a synthetic source, so it keeps saying what it says after the    /// real call sites change.    func testTheNoteSweepReadsBothShapesAndNoOthers() {        XCTAssertEqual(            Self.noteWrites(                in: """                    let record: JSONValue = .object([                        "kind": .string("document"),                        "id": .string(documentId),                        "note": .string(role),                    ])                    """),            [4])        XCTAssertEqual(            Self.noteWrites(                in: """                    var record: [String: JSONValue] = ["kind": .string("todo"), "id": .string(id)]                    if let note { record["note"] = .string(note) }                    """),            [2])        // A record's OWN note column, patched by a screen that also files        // things under projects, is not a link note.        XCTAssertEqual(            Self.noteWrites(in: #"_ = await model.patch(["note": .string(next)])"#), [])    }}