| 1 | import XCTest |
| 2 | @testable import LChatCore |
| 3 | |
| 4 | // Drift gate for native message decryption: the fixture blob was encrypted by |
| 5 | // the web crypto (regenerate with `node test/messageblob.test.mjs --emit`) with |
| 6 | // every optional envelope field populated. GroupKey + MessagePayload must open |
| 7 | // it and decode ALL of it — text, sender stamp, ts, attachment dimensions, and |
| 8 | // the reply reference — or the native feed renders garbage where the web doesn't. |
| 9 | final class MessagePayloadVectorTests: XCTestCase { |
| 10 | struct Vector: Decodable { |
| 11 | let keyB64: String |
| 12 | let blob: String |
| 13 | let expect: Expect |
| 14 | struct Expect: Decodable { |
| 15 | let text: String |
| 16 | let name: String |
| 17 | let uid: String |
| 18 | let ts: Int64 |
| 19 | let attachments: [Att] |
| 20 | let replyTo: Reply |
| 21 | struct Att: Decodable { |
| 22 | let id: String; let name: String; let type: String |
| 23 | let w: Int; let h: Int; let size: Int64 |
| 24 | let poster: String?; let dur: Double? |
| 25 | } |
| 26 | struct Reply: Decodable { let id: Int64; let name: String; let snippet: String } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func testMessageBlobDecodesFully() throws { |
| 31 | let v = try fixture(Vector.self, "message-payload-vector") |
| 32 | let raw = try XCTUnwrap(Data(base64urlEncoded: v.keyB64), "fixture key is not valid base64url") |
| 33 | let key = try GroupKey(rawKey: raw) |
| 34 | let p = try key.decryptJSON(MessagePayload.self, blob: v.blob) |
| 35 | |
| 36 | XCTAssertEqual(p.text, v.expect.text) |
| 37 | XCTAssertEqual(p.name, v.expect.name) |
| 38 | XCTAssertEqual(p.uid, v.expect.uid) |
| 39 | XCTAssertEqual(p.ts, v.expect.ts) |
| 40 | XCTAssertNil(p.type, "vector payload is untyped (today's messages)") |
| 41 | XCTAssertTrue(p.isRenderableChat) |
| 42 | |
| 43 | let atts = try XCTUnwrap(p.attachments, "attachments missing") |
| 44 | XCTAssertEqual(atts.count, v.expect.attachments.count) |
| 45 | for (att, expect) in zip(atts, v.expect.attachments) { |
| 46 | XCTAssertEqual(att.id, expect.id) |
| 47 | XCTAssertEqual(att.name, expect.name) |
| 48 | XCTAssertEqual(att.type, expect.type) |
| 49 | XCTAssertEqual(att.w, expect.w) |
| 50 | XCTAssertEqual(att.h, expect.h) |
| 51 | XCTAssertEqual(att.size, expect.size) |
| 52 | XCTAssertEqual(att.poster, expect.poster) |
| 53 | XCTAssertEqual(att.dur, expect.dur) |
| 54 | } |
| 55 | XCTAssertTrue(atts[0].isImage) |
| 56 | XCTAssertTrue(atts[1].isVideo) |
| 57 | |
| 58 | let reply = try XCTUnwrap(p.replyTo, "replyTo missing") |
| 59 | XCTAssertEqual(reply.id, v.expect.replyTo.id) |
| 60 | XCTAssertEqual(reply.name, v.expect.replyTo.name) |
| 61 | XCTAssertEqual(reply.snippet, v.expect.replyTo.snippet) |
| 62 | } |
| 63 | } |
| 64 | |