MockServices.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // MockServices.swift
  3. // indeed_ai_job_search
  4. //
  5. import Foundation
  6. import Observation
  7. /// Plan alias — Phase 1 mock search uses `MockJobDataSource`.
  8. typealias MockJobSearchService = MockJobDataSource
  9. struct MockJobDataSource: JobDataSource {
  10. var delayNanoseconds: UInt64 = 700_000_000
  11. var shouldFail: Bool = false
  12. func search(criteria: SearchCriteria, query: String) async throws -> [JobListing] {
  13. try await Task.sleep(nanoseconds: delayNanoseconds)
  14. if shouldFail {
  15. throw MockSearchError.network
  16. }
  17. return JobListingFilter.filter(MockJobListings.all, criteria: criteria, query: query)
  18. }
  19. }
  20. enum MockSearchError: LocalizedError {
  21. case network
  22. var errorDescription: String? {
  23. switch self {
  24. case .network:
  25. return "We couldn’t reach job results. Check your connection and try again."
  26. }
  27. }
  28. }
  29. struct MockSearchInterpreter: OpenAISearchService {
  30. func interpret(query: String) async throws -> InterpretedSearch {
  31. try await Task.sleep(nanoseconds: 350_000_000)
  32. var criteria = SearchCriteria()
  33. let lower = query.lowercased()
  34. if lower.contains("ios") {
  35. criteria.jobTitle = "iOS Developer"
  36. } else if lower.contains("android") {
  37. criteria.jobTitle = "Android Developer"
  38. } else if lower.contains("swift") {
  39. criteria.jobTitle = "Swift Developer"
  40. } else {
  41. criteria.keywords = query
  42. }
  43. if lower.contains("remote") {
  44. criteria.remoteTypes.insert(.remote)
  45. }
  46. if lower.contains("hybrid") {
  47. criteria.remoteTypes.insert(.hybrid)
  48. }
  49. // Geo only when the user names a place — stay neutral otherwise.
  50. if lower.contains("pakistan") {
  51. criteria.country = "Pakistan"
  52. }
  53. if lower.contains("united states") || lower.contains("usa") || lower.contains("u.s.") {
  54. criteria.country = "United States"
  55. }
  56. if lower.contains("united kingdom") || lower.contains(" uk") || lower.hasSuffix(" uk") {
  57. criteria.country = "United Kingdom"
  58. }
  59. if lower.contains("lahore") {
  60. criteria.city = "Lahore"
  61. if criteria.country.isEmpty { criteria.country = "Pakistan" }
  62. }
  63. if lower.contains("karachi") {
  64. criteria.city = "Karachi"
  65. if criteria.country.isEmpty { criteria.country = "Pakistan" }
  66. }
  67. if lower.contains("london") {
  68. criteria.city = "London"
  69. if criteria.country.isEmpty { criteria.country = "United Kingdom" }
  70. }
  71. if lower.contains("full-time") || lower.contains("full time") {
  72. criteria.employmentTypes.insert(.fullTime)
  73. }
  74. if lower.contains("senior") || lower.contains("2+") || lower.contains("experience") {
  75. criteria.experienceLevels.insert(.senior)
  76. }
  77. return InterpretedSearch(
  78. naturalLanguageQuery: query,
  79. criteria: criteria,
  80. confidenceNote: "Mock AI interpretation for Phase 1 preview."
  81. )
  82. }
  83. }
  84. struct MockJobValidationService: JobValidationService {
  85. func validate(listing: JobListing) async -> JobAvailability {
  86. listing.availability
  87. }
  88. }
  89. @Observable
  90. @MainActor
  91. final class MockSavedJobsStore: SavedJobsStore {
  92. private(set) var savedJobs: [SavedJob] = []
  93. func isSaved(_ listing: JobListing) -> Bool {
  94. savedJobs.contains { $0.listing.indeedJobKey == listing.indeedJobKey }
  95. }
  96. func toggleSave(_ listing: JobListing) {
  97. if let index = savedJobs.firstIndex(where: { $0.listing.indeedJobKey == listing.indeedJobKey }) {
  98. savedJobs.remove(at: index)
  99. } else {
  100. savedJobs.insert(SavedJob(listing: listing, savedAt: .now), at: 0)
  101. }
  102. }
  103. func remove(_ listing: JobListing) {
  104. savedJobs.removeAll { $0.listing.indeedJobKey == listing.indeedJobKey }
  105. }
  106. func updateListing(_ listing: JobListing, lastVerifiedAt: Date) {
  107. guard let index = savedJobs.firstIndex(where: { $0.listing.indeedJobKey == listing.indeedJobKey }) else {
  108. return
  109. }
  110. savedJobs[index].listing = listing
  111. savedJobs[index].lastVerifiedAt = lastVerifiedAt
  112. }
  113. func clearAll() {
  114. savedJobs.removeAll()
  115. }
  116. }
  117. @Observable
  118. @MainActor
  119. final class MockAlertsStore: AlertsStore {
  120. private(set) var alerts: [JobAlert]
  121. private(set) var events: [AlertNotificationEvent]
  122. init() {
  123. var remoteIOS = SearchCriteria()
  124. remoteIOS.jobTitle = "iOS Developer"
  125. remoteIOS.country = "Pakistan"
  126. remoteIOS.remoteTypes = [.remote]
  127. remoteIOS.employmentTypes = [.fullTime]
  128. var senior = SearchCriteria()
  129. senior.jobTitle = "Senior iOS Developer"
  130. senior.country = "Pakistan"
  131. senior.experienceLevels = [.senior]
  132. self.alerts = [
  133. JobAlert(
  134. name: "Remote iOS in Pakistan",
  135. criteria: remoteIOS,
  136. frequency: .daily,
  137. isEnabled: true,
  138. lastRunAt: Date().addingTimeInterval(-3_600),
  139. matchCount: 12
  140. ),
  141. JobAlert(
  142. name: "Senior iOS roles",
  143. criteria: senior,
  144. frequency: .weekly,
  145. isEnabled: false,
  146. lastRunAt: Date().addingTimeInterval(-86_400 * 3),
  147. matchCount: 4
  148. )
  149. ]
  150. self.events = [
  151. AlertNotificationEvent(
  152. alertName: "Remote iOS in Pakistan",
  153. message: "12 new matching jobs since yesterday.",
  154. createdAt: Date().addingTimeInterval(-2_000)
  155. ),
  156. AlertNotificationEvent(
  157. alertName: "Senior iOS roles",
  158. message: "4 new senior roles this week.",
  159. createdAt: Date().addingTimeInterval(-86_400),
  160. isRead: true
  161. )
  162. ]
  163. }
  164. func upsert(_ alert: JobAlert) {
  165. if let index = alerts.firstIndex(where: { $0.id == alert.id }) {
  166. alerts[index] = alert
  167. } else {
  168. alerts.insert(alert, at: 0)
  169. }
  170. }
  171. func delete(_ alert: JobAlert) {
  172. alerts.removeAll { $0.id == alert.id }
  173. }
  174. func setEnabled(_ alert: JobAlert, enabled: Bool) {
  175. guard let index = alerts.firstIndex(where: { $0.id == alert.id }) else { return }
  176. alerts[index].isEnabled = enabled
  177. }
  178. func markEventsRead() {
  179. for index in events.indices {
  180. events[index].isRead = true
  181. }
  182. }
  183. }