// Test Tool: File Blessing Validator (V2 - Argument Support) // Usage: swift Test_FileBlessing.swift "/path/to/target.json" import Foundation func runTest() { // 1. Parse Arguments guard CommandLine.arguments.count > 1 else { print("❌ Usage: swift Test_FileBlessing.swift \"\"") exit(1) } let targetPath = CommandLine.arguments[1] let fileURL = URL(fileURLWithPath: targetPath) // 2. Verify Existence guard FileManager.default.fileExists(atPath: targetPath) else { print("❌ Test File Not Found at: \(targetPath)") exit(1) } print("🔹 Target Acquired: \(targetPath)") // 3. Read Original Data do { let originalData = try Data(contentsOf: fileURL) guard var json = try JSONSerialization.jsonObject(with: originalData) as? [String: Any] else { print("❌ Failed to parse JSON.") exit(1) } // 4. Modify Data (Harmless Timestamp Bump) let timestamp = ISO8601DateFormatter().string(from: Date()) print("🔹 Injecting timestamp: \(timestamp)") // Ensure structure exists to hold the stamp if var chunked = json["chunkedPrompt"] as? [String: Any] { // Add a harmless comment chunk at the end var chunks = chunked["chunks"] as? [[String: Any]] ?? [] chunks.append(["text": "// Blessing Test (VM): \(timestamp)", "role": "user"]) chunked["chunks"] = chunks json["chunkedPrompt"] = chunked } // 5. Serialize New Data let newData = try JSONSerialization.data(withJSONObject: json, options: [.prettyPrinted, .withoutEscapingSlashes]) // 6. THE CRITICAL TEST: In-Place Write (No Atomic Swap) try writeInPlace(data: newData, to: fileURL) print("✅ Write Complete (In-Place). Check AI Studio for file visibility.") } catch { print("❌ Error: \(error.localizedDescription)") exit(1) } } func writeInPlace(data: Data, to url: URL) throws { // Open file for writing let handle = try FileHandle(forWritingTo: url) defer { try? handle.close() } // Truncate existing content to 0 try handle.truncate(atOffset: 0) // Write new data try handle.write(contentsOf: data) // Ensure data is flushed to disk try handle.synchronize() } runTest()