← All repositories

crossmate

A collaborative crossword app for iPhone and iPad

git clone https://code.inqk.net/crossmate.git


Tests/Unit/GameSummaryThumbnailTests.swift (7933B)

1import CoreData
2import Foundation
3import Testing
4
5@testable import Crossmate
6
7/// Pins down `GameSummary`'s thumbnail derivation. An in-progress game's
8/// thumbnail mirrors the `CellEntity` cache, but a completed game is terminal
9/// and always renders solved (restore seals it to the solution), so its
10/// thumbnail must come from the completion latch — the raw merge behind the
11/// cache can permanently lack the winning letter when a clear stamped after
12/// the latch wins LWW.
13@Suite("GameSummary thumbnail")
14@MainActor
15struct GameSummaryThumbnailTests {
16
17    private static let puzzleSource = """
18    Title: Test Puzzle
19    Author: Test
20
21
22    ABC
23    D#E
24    FGH
25
26
27    A1. Across 1 ~ ABC
28    A4. Across 4 ~ DE
29    A5. Across 5 ~ FGH
30    D1. Down 1 ~ ADF
31    D2. Down 2 ~ BG
32    D3. Down 3 ~ CEH
33    """
34
35    private func makeGame(in ctx: NSManagedObjectContext) throws -> GameEntity {
36        let xd = try XD.parse(Self.puzzleSource)
37        let puzzle = Puzzle(xd: xd)
38
39        let entity = GameEntity(context: ctx)
40        entity.id = UUID()
41        entity.title = "Thumbnail"
42        entity.puzzleSource = Self.puzzleSource
43        entity.createdAt = Date()
44        entity.updatedAt = Date()
45        entity.populateCachedSummaryFields(from: puzzle)
46        return entity
47    }
48
49    private func addCell(
50        _ letter: String,
51        row: Int16,
52        col: Int16,
53        to entity: GameEntity,
54        in ctx: NSManagedObjectContext,
55        authorID: String? = nil,
56        mark: CellMark = .none
57    ) {
58        let cell = CellEntity(context: ctx)
59        cell.game = entity
60        cell.row = row
61        cell.col = col
62        cell.letter = letter
63        cell.letterAuthorID = authorID
64        cell.markCode = mark.code
65    }
66
67    private func addPlayer(
68        authorID: String,
69        name: String,
70        to entity: GameEntity,
71        in ctx: NSManagedObjectContext
72    ) {
73        let player = PlayerEntity(context: ctx)
74        player.game = entity
75        player.authorID = authorID
76        player.name = name
77        player.ckRecordName = "player-\(authorID)"
78        player.updatedAt = Date()
79    }
80
81    private func addMoves(
82        authorID: String,
83        to entity: GameEntity,
84        in ctx: NSManagedObjectContext
85    ) {
86        let moves = MovesEntity(context: ctx)
87        moves.game = entity
88        moves.authorID = authorID
89        moves.deviceID = "device-\(authorID)"
90        moves.cells = Data()
91        moves.updatedAt = Date()
92        moves.ckRecordName = "moves-\(authorID)"
93    }
94
95    @Test("In-progress thumbnail mirrors the CellEntity cache")
96    func inProgressThumbnailMirrorsCache() throws {
97        let persistence = makeTestPersistence()
98        let ctx = persistence.viewContext
99        let entity = try makeGame(in: ctx)
100        addCell("A", row: 0, col: 0, to: entity, in: ctx)
101        addCell("", row: 2, col: 2, to: entity, in: ctx)
102        try ctx.save()
103
104        let summary = try #require(GameSummary(entity: entity))
105        #expect(summary.thumbnailCells == [
106            .filled, .empty, .empty,
107            .empty, .block, .empty,
108            .empty, .empty, .empty,
109        ])
110    }
111
112    @Test("Completed thumbnail is full even when the cache has a hole")
113    func completedThumbnailIgnoresCacheHoles() throws {
114        let persistence = makeTestPersistence()
115        let ctx = persistence.viewContext
116        let entity = try makeGame(in: ctx)
117        // The winning letter never reached the durable merge: its cell holds
118        // an empty tombstone and another square has no row at all.
119        addCell("A", row: 0, col: 0, to: entity, in: ctx)
120        addCell("", row: 2, col: 2, to: entity, in: ctx)
121        entity.completedAt = Date()
122        try ctx.save()
123
124        let summary = try #require(GameSummary(entity: entity))
125        #expect(summary.thumbnailCells == [
126            .filled, .filled, .filled,
127            .filled, .block, .filled,
128            .filled, .filled, .filled,
129        ])
130    }
131
132    @Test("Shared summary lists remote participants with deterministic colours")
133    func sharedSummaryListsRemoteParticipants() throws {
134        let persistence = makeTestPersistence()
135        let ctx = persistence.viewContext
136        let entity = try makeGame(in: ctx)
137        entity.ckShareRecordName = "cloudkit.zoneshare"
138        addPlayer(authorID: "_Local", name: "Local", to: entity, in: ctx)
139        addPlayer(authorID: "_Alice", name: "Alice", to: entity, in: ctx)
140        addMoves(authorID: "_Bob", to: entity, in: ctx)
141
142        let friend = FriendEntity(context: ctx)
143        friend.authorID = "_Alice"
144        friend.createdAt = Date()
145        friend.displayName = "Alice"
146        friend.displayNameVersion = 0
147        friend.isBlocked = false
148        friend.nickname = "Ace"
149        friend.nicknameVersion = 1
150        friend.pairKey = "pair"
151        try ctx.save()
152
153        let summary = try #require(GameSummary(
154            entity: entity,
155            localAuthorID: "_Local",
156            localName: "Local Player",
157            localColor: .blue
158        ))
159
160        #expect(summary.allParticipants.map(\.authorID) == ["_Local", "_Alice", "_Bob"])
161        #expect(summary.allParticipants.map(\.name) == ["Local Player", "Ace", "Waiting for player…"])
162        #expect(summary.allParticipants.map(\.isLocal) == [true, false, false])
163        let remoteParticipants = summary.allParticipants.filter { !$0.isLocal }
164        // Collaborators are de-conflicted within the game, so the two remotes
165        // get distinct colours and neither collides with the local 'blue'.
166        #expect(!remoteParticipants.map(\.color.id).contains(PlayerColor.blue.id))
167        #expect(Set(remoteParticipants.map(\.color.id)).count == 2)
168    }
169
170    @Test("Shared summary includes a Player row before its name arrives")
171    func sharedSummaryIncludesUnnamedPlayer() throws {
172        let persistence = makeTestPersistence()
173        let ctx = persistence.viewContext
174        let entity = try makeGame(in: ctx)
175        entity.ckShareRecordName = "cloudkit.zoneshare"
176        addPlayer(authorID: "_Remote", name: "", to: entity, in: ctx)
177        try ctx.save()
178
179        let summary = try #require(GameSummary(
180            entity: entity,
181            localAuthorID: "_Local",
182            localName: "Local Player",
183            localColor: .blue
184        ))
185
186        #expect(summary.allParticipants.map(\.authorID) == ["_Local", "_Remote"])
187        #expect(summary.allParticipants.last?.name == "Waiting for player…")
188    }
189
190    @Test("Shared strip includes local player and orders by score")
191    func sharedStripIncludesLocalPlayerAndOrdersByScore() throws {
192        let persistence = makeTestPersistence()
193        let ctx = persistence.viewContext
194        let entity = try makeGame(in: ctx)
195        entity.ckShareRecordName = "cloudkit.zoneshare"
196        addPlayer(authorID: "_Local", name: "Local", to: entity, in: ctx)
197        addPlayer(authorID: "_Alice", name: "Alice", to: entity, in: ctx)
198        addPlayer(authorID: "_Bob", name: "Bob", to: entity, in: ctx)
199        addCell("A", row: 0, col: 0, to: entity, in: ctx, authorID: "_Alice")
200        addCell("B", row: 0, col: 1, to: entity, in: ctx, authorID: "_Local")
201        addCell("C", row: 0, col: 2, to: entity, in: ctx, authorID: "_Bob")
202        addCell("D", row: 1, col: 0, to: entity, in: ctx, authorID: "_Alice")
203        addCell("F", row: 2, col: 0, to: entity, in: ctx, authorID: "_Bob", mark: .revealed)
204        try ctx.save()
205
206        let summary = try #require(GameSummary(
207            entity: entity,
208            localAuthorID: "_Local",
209            localName: "Local Player",
210            localColor: .blue
211        ))
212
213        #expect(summary.stripParticipants.map(\.authorID) == ["_Alice", "_Bob", "_Local"])
214        #expect(summary.stripParticipants.map(\.isLocal) == [false, false, true])
215        #expect(summary.stripParticipants.map(\.color.id).contains(PlayerColor.blue.id))
216    }
217}