Files
bitchat/bitchatTests/Sync/SyncTypeFlagsTests.swift
T
c2a5668569 Sync cleanups: normalize SyncTypeFlags, single announce-ID path, TODO (#1373)
Follow-ups deferred from the REQUEST_SYNC review (#1371):

- SyncTypeFlags.init(rawValue:) now masks to the union of bits that map to a
  known message type (derived from the bit↔type table, so it tracks new
  types automatically). Phantom bits from a truncated/garbled flags field —
  or a type a newer peer added — no longer live in the set as membership no
  contains() matches yet toData() re-serializes.

- GossipSyncManager stored each latest announce as (hex-id string, packet)
  and diffed announces against the stored string while every other type
  recomputed the ID via PacketIdUtil. Collapsed the store to just the packet
  and recompute the ID everywhere, removing the latent dual-path divergence.

- Documented the REQUEST_SYNC TLV table and marked fragmentIdFilter (0x06)
  with a TODO(v2): it's parsed/re-serialized but never populated or honored
  (reserved for incremental fragment sync) — finish or drop, not silent dead
  surface.

Adds SyncTypeFlags phantom-bit/round-trip tests and a GossipSyncManager test
that an announce already in the requester's filter is suppressed (guards the
recompute path). Full suite: 1034 tests pass.

Co-authored-by: jack <jackjackbits@users.noreply.github.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-06 21:04:14 +02:00

44 lines
1.6 KiB
Swift

import Foundation
import Testing
import BitFoundation
@testable import bitchat
struct SyncTypeFlagsTests {
@Test func knownTypesRoundTripThroughData() throws {
let flags: SyncTypeFlags = [.announce, .message, .fragment, .fileTransfer]
let data = try #require(flags.toData())
let decoded = try #require(SyncTypeFlags.decode(data))
#expect(decoded == flags)
}
@Test func decodeDropsPhantomBits() {
// Bits 8+ map to no message type. They must not survive decode as
// phantom membership.
let phantom = Data([0x00, 0xFF]) // bits 8..15 set, no known type
let decoded = SyncTypeFlags.decode(phantom)
#expect(decoded?.rawValue == 0)
#expect(decoded?.toMessageTypes().isEmpty == true)
}
@Test func phantomBitsAreStrippedButKnownBitsSurvive() {
// Low byte = announce(0) + message(1); high byte = phantom.
let mixed = Data([0b0000_0011, 0xFF])
let decoded = SyncTypeFlags.decode(mixed)
#expect(decoded?.contains(.announce) == true)
#expect(decoded?.contains(.message) == true)
// Only the two known bits remain; phantom high byte is gone.
#expect(decoded?.rawValue == 0b0000_0011)
}
@Test func rawValueInitNormalizesPhantomBits() {
let flags = SyncTypeFlags(rawValue: 0xFFFF_FFFF_FFFF_FFFF)
// Every known type bit is set; nothing above them survives, so the
// field serializes to a single byte.
#expect(flags.contains(.announce))
#expect(flags.contains(.fileTransfer))
let data = flags.toData()
#expect(data?.count == 1)
}
}