Files
FamilyMealPlanner/MealMoodTests/AutocompleteEngineTests.swift
T
alexandrev-tibco 4ffa15b06a 2.0: CloudKit private-database sync replaces KV snapshot sync
Foundation for 2.1 family sharing (CKShare needs these models + container).
SwiftData cannot share across Apple IDs today, so 2.0 ships true multi-device
sync for the same account instead:

- Models made CloudKit-compatible: dropped @Attribute(.unique) on all 5,
  inline defaults on every attribute, WeekPlan.slots stored as optional
  relationship (name preserved → lightweight migration) with non-optional
  slotList facade; ~73 call sites renamed.
- Container: cloudKitDatabase .automatic, falling back to the local-only
  store when CloudKit is unavailable; failures recorded to Crashlytics.
- Entitlements: iCloud CloudKit service (container already existed);
  remote-notification background mode for push-driven sync.
- Legacy KV snapshot sync (ICloudSyncService) stays inert when CloudKit is
  active — kept only for 1.x devices.
- DeduplicationService collapses cross-device duplicates deterministically on
  launch (settings singleton, default tags with tagId remapping, same-week
  plans).
- Note: AppSettings.isPremium now syncs across same-Apple-ID devices; StoreKit
  (PremiumSyncService + Transaction.updates) remains the source of truth and
  reconciles on every launch/foreground.

All 21 unit tests pass, including ICloudSyncPremiumIsolationTests.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BkanrydYtrme8wipTzWssG
2026-07-12 18:13:45 +02:00

84 lines
3.2 KiB
Swift

import XCTest
@testable import MealMood
final class AutocompleteEngineTests: XCTestCase {
func testValidateDropRejectsConsecutiveTag() {
let tag = Tag(
name: "Carne",
nameEN: "Meat",
color: "#E74C3C",
maxPerWeek: nil,
noConsecutive: true,
noDuplicateInDay: false
)
let dishA = Dish(name: "Pollo", tagIds: [tag.id])
let dishB = Dish(name: "Ternera", tagIds: [tag.id])
let plan = WeekPlan(weekStartDate: Date().startOfWeek())
let mondayDinner = MealSlot(dayOfWeek: 0, mealType: MealType.dinner.rawValue, dishId: dishA.id)
let tuesdayDinner = MealSlot(dayOfWeek: 1, mealType: MealType.dinner.rawValue, dishId: nil)
plan.slotList = [mondayDinner, tuesdayDinner]
let isValid = AutocompleteEngine.validateDrop(
dish: dishB,
slot: tuesdayDinner,
plan: plan,
allTags: [tag],
allDishes: [dishA, dishB]
)
XCTAssertFalse(isValid)
}
func testAutocompleteLeavesSlotEmptyWhenMaxPerWeekReached() {
let tag = Tag(
name: "Pescado",
nameEN: "Fish",
color: "#3498DB",
maxPerWeek: 1,
noConsecutive: false,
noDuplicateInDay: false
)
let fishDish = Dish(name: "Salmon", tagIds: [tag.id])
let plan = WeekPlan(weekStartDate: Date().startOfWeek())
let mondayDinner = MealSlot(dayOfWeek: 0, mealType: MealType.dinner.rawValue, dishId: fishDish.id)
let tuesdayDinner = MealSlot(dayOfWeek: 1, mealType: MealType.dinner.rawValue, dishId: nil)
plan.slotList = [mondayDinner, tuesdayDinner]
let result = AutocompleteEngine.autocomplete(
emptySlots: [tuesdayDinner],
currentPlan: plan,
allDishes: [fishDish],
allTags: [tag]
)
XCTAssertEqual(result.unfilledCount, 1)
XCTAssertNil(tuesdayDinner.dishId)
}
func testDishStableSortRemainsConsistentAfterDeletion() {
let base = Date()
let oldest = Dish(id: UUID(uuidString: "00000000-0000-0000-0000-000000000001")!, name: "Old", createdAt: base.addingTimeInterval(-60))
let newest = Dish(id: UUID(uuidString: "00000000-0000-0000-0000-000000000002")!, name: "New", createdAt: base)
let middle = Dish(id: UUID(uuidString: "00000000-0000-0000-0000-000000000003")!, name: "Middle", createdAt: base.addingTimeInterval(-30))
let initial = Dish.stableSortedForDisplay([oldest, newest, middle]).map(\.id)
XCTAssertEqual(initial, [newest.id, middle.id, oldest.id])
let afterDeletion = Dish.stableSortedForDisplay([newest, oldest]).map(\.id)
XCTAssertEqual(afterDeletion, [newest.id, oldest.id])
}
func testPreferredDuplicateSlotKeepsAssignedDish() {
let duplicateWithDish = MealSlot(dayOfWeek: 0, mealType: MealType.dinner.rawValue, dishId: UUID())
let duplicateWithoutDish = MealSlot(dayOfWeek: 0, mealType: MealType.dinner.rawValue, dishId: nil)
let preferred = MealSlot.preferredForDuplicateResolution([duplicateWithoutDish, duplicateWithDish])
XCTAssertEqual(preferred.id, duplicateWithDish.id)
}
}