expo-targets 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (231) hide show
  1. package/.eslintrc.js +2 -0
  2. package/README.md +244 -0
  3. package/android/build.gradle +22 -0
  4. package/android/src/main/java/expo/modules/targets/ExpoTargetsLogger.kt +98 -0
  5. package/android/src/main/java/expo/modules/targets/ExpoTargetsReceiver.kt +136 -0
  6. package/android/src/main/java/expo/modules/targets/ExpoTargetsRemoteViewsProvider.kt +72 -0
  7. package/android/src/main/java/expo/modules/targets/ExpoTargetsWidgetProvider.kt +47 -0
  8. package/android/src/main/java/expo/modules/targets/ExpoTargetsWidgetSync.kt +78 -0
  9. package/android/src/main/java/expo/modules/targets/ExpoTargetsWidgetUpdateReceiver.kt +30 -0
  10. package/android/src/main/java/expo/modules/targets/extension/ExpoTargetsExtensionModule.kt +27 -0
  11. package/android/src/main/java/expo/modules/targets/messages/ExpoTargetsMessagesModule.kt +40 -0
  12. package/android/src/main/java/expo/modules/targets/storage/ExpoTargetsStorageModule.kt +119 -0
  13. package/app.plugin.js +1 -0
  14. package/build/metro/src/index.d.ts +2 -0
  15. package/build/metro/src/index.d.ts.map +1 -0
  16. package/build/metro/src/index.js +6 -0
  17. package/build/metro/src/index.js.map +1 -0
  18. package/build/metro/src/withTargetsMetro.d.ts +6 -0
  19. package/build/metro/src/withTargetsMetro.d.ts.map +1 -0
  20. package/build/metro/src/withTargetsMetro.js +171 -0
  21. package/build/metro/src/withTargetsMetro.js.map +1 -0
  22. package/build/plugin/src/android/config-plugins/withAndroidColors.d.ts +7 -0
  23. package/build/plugin/src/android/config-plugins/withAndroidColors.d.ts.map +1 -0
  24. package/build/plugin/src/android/config-plugins/withAndroidColors.js +56 -0
  25. package/build/plugin/src/android/config-plugins/withAndroidColors.js.map +1 -0
  26. package/build/plugin/src/android/config-plugins/withAndroidManifest.d.ts +7 -0
  27. package/build/plugin/src/android/config-plugins/withAndroidManifest.d.ts.map +1 -0
  28. package/build/plugin/src/android/config-plugins/withAndroidManifest.js +70 -0
  29. package/build/plugin/src/android/config-plugins/withAndroidManifest.js.map +1 -0
  30. package/build/plugin/src/android/config-plugins/withAndroidModule.d.ts +7 -0
  31. package/build/plugin/src/android/config-plugins/withAndroidModule.d.ts.map +1 -0
  32. package/build/plugin/src/android/config-plugins/withAndroidModule.js +41 -0
  33. package/build/plugin/src/android/config-plugins/withAndroidModule.js.map +1 -0
  34. package/build/plugin/src/android/config-plugins/withAndroidResources.d.ts +7 -0
  35. package/build/plugin/src/android/config-plugins/withAndroidResources.d.ts.map +1 -0
  36. package/build/plugin/src/android/config-plugins/withAndroidResources.js +75 -0
  37. package/build/plugin/src/android/config-plugins/withAndroidResources.js.map +1 -0
  38. package/build/plugin/src/android/config-plugins/withAndroidTarget.d.ts +28 -0
  39. package/build/plugin/src/android/config-plugins/withAndroidTarget.d.ts.map +1 -0
  40. package/build/plugin/src/android/config-plugins/withAndroidTarget.js +28 -0
  41. package/build/plugin/src/android/config-plugins/withAndroidTarget.js.map +1 -0
  42. package/build/plugin/src/android/config-plugins/withGradleModule.d.ts +7 -0
  43. package/build/plugin/src/android/config-plugins/withGradleModule.d.ts.map +1 -0
  44. package/build/plugin/src/android/config-plugins/withGradleModule.js +86 -0
  45. package/build/plugin/src/android/config-plugins/withGradleModule.js.map +1 -0
  46. package/build/plugin/src/android/config-plugins/withGradleSettings.d.ts +7 -0
  47. package/build/plugin/src/android/config-plugins/withGradleSettings.d.ts.map +1 -0
  48. package/build/plugin/src/android/config-plugins/withGradleSettings.js +23 -0
  49. package/build/plugin/src/android/config-plugins/withGradleSettings.js.map +1 -0
  50. package/build/plugin/src/android/withAndroidAppBuildGradle.d.ts +7 -0
  51. package/build/plugin/src/android/withAndroidAppBuildGradle.d.ts.map +1 -0
  52. package/build/plugin/src/android/withAndroidAppBuildGradle.js +31 -0
  53. package/build/plugin/src/android/withAndroidAppBuildGradle.js.map +1 -0
  54. package/build/plugin/src/android/withAndroidKotlinGradleProperty.d.ts +3 -0
  55. package/build/plugin/src/android/withAndroidKotlinGradleProperty.d.ts.map +1 -0
  56. package/build/plugin/src/android/withAndroidKotlinGradleProperty.js +24 -0
  57. package/build/plugin/src/android/withAndroidKotlinGradleProperty.js.map +1 -0
  58. package/build/plugin/src/android/withAndroidProjectKotlinVersion.d.ts +3 -0
  59. package/build/plugin/src/android/withAndroidProjectKotlinVersion.d.ts.map +1 -0
  60. package/build/plugin/src/android/withAndroidProjectKotlinVersion.js +27 -0
  61. package/build/plugin/src/android/withAndroidProjectKotlinVersion.js.map +1 -0
  62. package/build/plugin/src/android/withAndroidTarget.d.ts +10 -0
  63. package/build/plugin/src/android/withAndroidTarget.d.ts.map +1 -0
  64. package/build/plugin/src/android/withAndroidTarget.js +22 -0
  65. package/build/plugin/src/android/withAndroidTarget.js.map +1 -0
  66. package/build/plugin/src/android/withAndroidWidget.d.ts +13 -0
  67. package/build/plugin/src/android/withAndroidWidget.d.ts.map +1 -0
  68. package/build/plugin/src/android/withAndroidWidget.js +564 -0
  69. package/build/plugin/src/android/withAndroidWidget.js.map +1 -0
  70. package/build/plugin/src/config.d.ts +141 -0
  71. package/build/plugin/src/config.d.ts.map +1 -0
  72. package/build/plugin/src/config.js +54 -0
  73. package/build/plugin/src/config.js.map +1 -0
  74. package/build/plugin/src/index.d.ts +9 -0
  75. package/build/plugin/src/index.d.ts.map +1 -0
  76. package/build/plugin/src/index.js +11 -0
  77. package/build/plugin/src/index.js.map +1 -0
  78. package/build/plugin/src/ios/config-plugins/withEntitlements.d.ts +11 -0
  79. package/build/plugin/src/ios/config-plugins/withEntitlements.d.ts.map +1 -0
  80. package/build/plugin/src/ios/config-plugins/withEntitlements.js +49 -0
  81. package/build/plugin/src/ios/config-plugins/withEntitlements.js.map +1 -0
  82. package/build/plugin/src/ios/config-plugins/withIOSTarget.d.ts +17 -0
  83. package/build/plugin/src/ios/config-plugins/withIOSTarget.d.ts.map +1 -0
  84. package/build/plugin/src/ios/config-plugins/withIOSTarget.js +152 -0
  85. package/build/plugin/src/ios/config-plugins/withIOSTarget.js.map +1 -0
  86. package/build/plugin/src/ios/config-plugins/withIosColorset.d.ts +8 -0
  87. package/build/plugin/src/ios/config-plugins/withIosColorset.d.ts.map +1 -0
  88. package/build/plugin/src/ios/config-plugins/withIosColorset.js +25 -0
  89. package/build/plugin/src/ios/config-plugins/withIosColorset.js.map +1 -0
  90. package/build/plugin/src/ios/config-plugins/withIosStickerPack.d.ts +8 -0
  91. package/build/plugin/src/ios/config-plugins/withIosStickerPack.d.ts.map +1 -0
  92. package/build/plugin/src/ios/config-plugins/withIosStickerPack.js +52 -0
  93. package/build/plugin/src/ios/config-plugins/withIosStickerPack.js.map +1 -0
  94. package/build/plugin/src/ios/config-plugins/withPodfile.d.ts +12 -0
  95. package/build/plugin/src/ios/config-plugins/withPodfile.d.ts.map +1 -0
  96. package/build/plugin/src/ios/config-plugins/withPodfile.js +162 -0
  97. package/build/plugin/src/ios/config-plugins/withPodfile.js.map +1 -0
  98. package/build/plugin/src/ios/config-plugins/withXcodeChanges.d.ts +15 -0
  99. package/build/plugin/src/ios/config-plugins/withXcodeChanges.d.ts.map +1 -0
  100. package/build/plugin/src/ios/config-plugins/withXcodeChanges.js +646 -0
  101. package/build/plugin/src/ios/config-plugins/withXcodeChanges.js.map +1 -0
  102. package/build/plugin/src/ios/target.d.ts +41 -0
  103. package/build/plugin/src/ios/target.d.ts.map +1 -0
  104. package/build/plugin/src/ios/target.js +574 -0
  105. package/build/plugin/src/ios/target.js.map +1 -0
  106. package/build/plugin/src/ios/utils/asset.d.ts +60 -0
  107. package/build/plugin/src/ios/utils/asset.d.ts.map +1 -0
  108. package/build/plugin/src/ios/utils/asset.js +375 -0
  109. package/build/plugin/src/ios/utils/asset.js.map +1 -0
  110. package/build/plugin/src/ios/utils/file.d.ts +33 -0
  111. package/build/plugin/src/ios/utils/file.d.ts.map +1 -0
  112. package/build/plugin/src/ios/utils/file.js +88 -0
  113. package/build/plugin/src/ios/utils/file.js.map +1 -0
  114. package/build/plugin/src/ios/utils/index.d.ts +12 -0
  115. package/build/plugin/src/ios/utils/index.d.ts.map +1 -0
  116. package/build/plugin/src/ios/utils/index.js +38 -0
  117. package/build/plugin/src/ios/utils/index.js.map +1 -0
  118. package/build/plugin/src/ios/utils/paths.d.ts +128 -0
  119. package/build/plugin/src/ios/utils/paths.d.ts.map +1 -0
  120. package/build/plugin/src/ios/utils/paths.js +141 -0
  121. package/build/plugin/src/ios/utils/paths.js.map +1 -0
  122. package/build/plugin/src/ios/utils/plist.d.ts +29 -0
  123. package/build/plugin/src/ios/utils/plist.d.ts.map +1 -0
  124. package/build/plugin/src/ios/utils/plist.js +114 -0
  125. package/build/plugin/src/ios/utils/plist.js.map +1 -0
  126. package/build/plugin/src/ios/utils/podfile.d.ts +82 -0
  127. package/build/plugin/src/ios/utils/podfile.d.ts.map +1 -0
  128. package/build/plugin/src/ios/utils/podfile.js +408 -0
  129. package/build/plugin/src/ios/utils/podfile.js.map +1 -0
  130. package/build/plugin/src/ios/utils/reactNativeSwift.d.ts +11 -0
  131. package/build/plugin/src/ios/utils/reactNativeSwift.d.ts.map +1 -0
  132. package/build/plugin/src/ios/utils/reactNativeSwift.js +263 -0
  133. package/build/plugin/src/ios/utils/reactNativeSwift.js.map +1 -0
  134. package/build/plugin/src/ios/utils/xcode.d.ts +185 -0
  135. package/build/plugin/src/ios/utils/xcode.d.ts.map +1 -0
  136. package/build/plugin/src/ios/utils/xcode.js +706 -0
  137. package/build/plugin/src/ios/utils/xcode.js.map +1 -0
  138. package/build/plugin/src/logger.d.ts +10 -0
  139. package/build/plugin/src/logger.d.ts.map +1 -0
  140. package/build/plugin/src/logger.js +62 -0
  141. package/build/plugin/src/logger.js.map +1 -0
  142. package/build/plugin/src/parseTargetConfig.d.ts +3 -0
  143. package/build/plugin/src/parseTargetConfig.d.ts.map +1 -0
  144. package/build/plugin/src/parseTargetConfig.js +113 -0
  145. package/build/plugin/src/parseTargetConfig.js.map +1 -0
  146. package/build/plugin/src/withTargetsDir.d.ts +6 -0
  147. package/build/plugin/src/withTargetsDir.d.ts.map +1 -0
  148. package/build/plugin/src/withTargetsDir.js +90 -0
  149. package/build/plugin/src/withTargetsDir.js.map +1 -0
  150. package/build/src/Target.d.ts +43 -0
  151. package/build/src/Target.d.ts.map +1 -0
  152. package/build/src/Target.js +128 -0
  153. package/build/src/Target.js.map +1 -0
  154. package/build/src/index.d.ts +9 -0
  155. package/build/src/index.d.ts.map +1 -0
  156. package/build/src/index.js +27 -0
  157. package/build/src/index.js.map +1 -0
  158. package/build/src/modules/extension/index.d.ts +18 -0
  159. package/build/src/modules/extension/index.d.ts.map +1 -0
  160. package/build/src/modules/extension/index.js +44 -0
  161. package/build/src/modules/extension/index.js.map +1 -0
  162. package/build/src/modules/messages/index.d.ts +40 -0
  163. package/build/src/modules/messages/index.d.ts.map +1 -0
  164. package/build/src/modules/messages/index.js +94 -0
  165. package/build/src/modules/messages/index.js.map +1 -0
  166. package/build/src/modules/storage/index.d.ts +16 -0
  167. package/build/src/modules/storage/index.d.ts.map +1 -0
  168. package/build/src/modules/storage/index.js +122 -0
  169. package/build/src/modules/storage/index.js.map +1 -0
  170. package/expo-module.config.json +17 -0
  171. package/ios/ExpoTargetsExtension.podspec +23 -0
  172. package/ios/ExpoTargetsExtensionModule.swift +181 -0
  173. package/ios/ExpoTargetsMessages.podspec +23 -0
  174. package/ios/ExpoTargetsMessagesModule.swift +267 -0
  175. package/ios/ExpoTargetsStorage.podspec +23 -0
  176. package/ios/ExpoTargetsStorageModule.swift +91 -0
  177. package/ios/Tests/.swiftlint.yml +52 -0
  178. package/ios/Tests/Helpers/MockExtensionContext.swift +58 -0
  179. package/ios/Tests/Helpers/MockUserDefaults.swift +70 -0
  180. package/ios/Tests/Helpers/MockWidgetCenter.swift +71 -0
  181. package/ios/Tests/Helpers/TestHelpers.swift +90 -0
  182. package/ios/Tests/Integration/TargetIntegrationTests.swift +478 -0
  183. package/ios/Tests/Main/AllTests.swift +80 -0
  184. package/ios/Tests/Package.swift +66 -0
  185. package/ios/Tests/Performance/StoragePerformanceTests.swift +461 -0
  186. package/ios/Tests/Unit/Extension/ExpoTargetsExtensionModuleTests.swift +501 -0
  187. package/ios/Tests/Unit/Storage/ExpoTargetsStorageModuleTests.swift +578 -0
  188. package/ios/Tests/Unit/Template/ShareExtensionTemplateTests.swift +421 -0
  189. package/ios/Tests/XCTestPlan.xctestplan +87 -0
  190. package/metro/src/index.ts +1 -0
  191. package/metro/src/withTargetsMetro.ts +247 -0
  192. package/metro/tsconfig.build.json +9 -0
  193. package/package.json +74 -0
  194. package/plugin/src/android/withAndroidAppBuildGradle.ts +34 -0
  195. package/plugin/src/android/withAndroidTarget.ts +26 -0
  196. package/plugin/src/android/withAndroidWidget.ts +778 -0
  197. package/plugin/src/config.ts +255 -0
  198. package/plugin/src/index.ts +19 -0
  199. package/plugin/src/ios/config-plugins/withEntitlements.ts +69 -0
  200. package/plugin/src/ios/config-plugins/withIOSTarget.ts +179 -0
  201. package/plugin/src/ios/config-plugins/withIosColorset.ts +29 -0
  202. package/plugin/src/ios/config-plugins/withIosStickerPack.ts +65 -0
  203. package/plugin/src/ios/config-plugins/withPodfile.ts +238 -0
  204. package/plugin/src/ios/config-plugins/withXcodeChanges.ts +891 -0
  205. package/plugin/src/ios/target.ts +668 -0
  206. package/plugin/src/ios/templates/MessagesViewController.swift +120 -0
  207. package/plugin/src/ios/templates/ReactNativeViewController.swift +240 -0
  208. package/plugin/src/ios/templates/action-extension-data.swift +106 -0
  209. package/plugin/src/ios/templates/clip-extension-data.swift +28 -0
  210. package/plugin/src/ios/templates/messages-extension-data.swift +28 -0
  211. package/plugin/src/ios/templates/share-extension-data.swift +113 -0
  212. package/plugin/src/ios/utils/asset.ts +490 -0
  213. package/plugin/src/ios/utils/file.ts +80 -0
  214. package/plugin/src/ios/utils/index.ts +12 -0
  215. package/plugin/src/ios/utils/paths.ts +253 -0
  216. package/plugin/src/ios/utils/plist.ts +101 -0
  217. package/plugin/src/ios/utils/podfile.ts +534 -0
  218. package/plugin/src/ios/utils/reactNativeSwift.ts +283 -0
  219. package/plugin/src/ios/utils/xcode.ts +989 -0
  220. package/plugin/src/logger.ts +63 -0
  221. package/plugin/src/parseTargetConfig.ts +115 -0
  222. package/plugin/src/types/normalize-colors.d.ts +3 -0
  223. package/plugin/src/withTargetsDir.ts +120 -0
  224. package/plugin/tsconfig.build.json +9 -0
  225. package/plugin/tsconfig.json +9 -0
  226. package/src/Target.ts +239 -0
  227. package/src/index.ts +52 -0
  228. package/src/modules/extension/index.ts +54 -0
  229. package/src/modules/messages/index.ts +124 -0
  230. package/src/modules/storage/index.ts +126 -0
  231. package/tsconfig.json +8 -0
@@ -0,0 +1,578 @@
1
+ import XCTest
2
+ import Foundation
3
+ @testable import ExpoTargetsStorage
4
+
5
+ /// Comprehensive unit tests for ExpoTargetsStorageModule
6
+ /// Tests all storage operations, widget refresh, and data persistence
7
+ class ExpoTargetsStorageModuleTests: XCTestCase {
8
+
9
+ var storage: MockUserDefaults!
10
+ let testAppGroup = "group.com.test.expotargets"
11
+ let testKey = "testKey"
12
+
13
+ // MARK: - Setup & Teardown
14
+
15
+ override func setUp() {
16
+ super.setUp()
17
+ storage = MockUserDefaults(suiteName: testAppGroup)
18
+ storage?.clearStorage()
19
+ }
20
+
21
+ override func tearDown() {
22
+ storage?.clearStorage()
23
+ storage = nil
24
+ super.tearDown()
25
+ }
26
+
27
+ // MARK: - String Storage Tests
28
+
29
+ func testSetString_StoresValue() {
30
+ // Given
31
+ let value = "Hello, World!"
32
+
33
+ // When
34
+ storage.set(value, forKey: testKey)
35
+
36
+ // Then
37
+ XCTAssertEqual(storage.string(forKey: testKey), value)
38
+ XCTAssertTrue(storage.wasSynchronizeCalled())
39
+ }
40
+
41
+ func testSetString_EmptyString() {
42
+ // Given
43
+ let emptyValue = ""
44
+
45
+ // When
46
+ storage.set(emptyValue, forKey: testKey)
47
+
48
+ // Then
49
+ XCTAssertEqual(storage.string(forKey: testKey), emptyValue)
50
+ }
51
+
52
+ func testSetString_OverwritesExistingValue() {
53
+ // Given
54
+ storage.set("old value", forKey: testKey)
55
+ let newValue = "new value"
56
+
57
+ // When
58
+ storage.resetSynchronizeFlag()
59
+ storage.set(newValue, forKey: testKey)
60
+
61
+ // Then
62
+ XCTAssertEqual(storage.string(forKey: testKey), newValue)
63
+ XCTAssertTrue(storage.wasSynchronizeCalled())
64
+ }
65
+
66
+ func testSetString_WithSpecialCharacters() {
67
+ // Given
68
+ let specialValue = "Hello! 👋 \n\t Special: @#$%^&*()"
69
+
70
+ // When
71
+ storage.set(specialValue, forKey: testKey)
72
+
73
+ // Then
74
+ XCTAssertEqual(storage.string(forKey: testKey), specialValue)
75
+ }
76
+
77
+ func testSetString_WithUnicode() {
78
+ // Given
79
+ let unicodeValue = "Hello 世界 🌍 مرحبا"
80
+
81
+ // When
82
+ storage.set(unicodeValue, forKey: testKey)
83
+
84
+ // Then
85
+ XCTAssertEqual(storage.string(forKey: testKey), unicodeValue)
86
+ }
87
+
88
+ // MARK: - Integer Storage Tests
89
+
90
+ func testSetInt_StoresValue() {
91
+ // Given
92
+ let value = 42
93
+
94
+ // When
95
+ storage.set(value, forKey: testKey)
96
+
97
+ // Then
98
+ XCTAssertEqual(storage.integer(forKey: testKey), value)
99
+ XCTAssertTrue(storage.wasSynchronizeCalled())
100
+ }
101
+
102
+ func testSetInt_ZeroValue() {
103
+ // Given
104
+ let value = 0
105
+
106
+ // When
107
+ storage.set(value, forKey: testKey)
108
+
109
+ // Then
110
+ XCTAssertEqual(storage.integer(forKey: testKey), value)
111
+ }
112
+
113
+ func testSetInt_NegativeValue() {
114
+ // Given
115
+ let value = -100
116
+
117
+ // When
118
+ storage.set(value, forKey: testKey)
119
+
120
+ // Then
121
+ XCTAssertEqual(storage.integer(forKey: testKey), value)
122
+ }
123
+
124
+ func testSetInt_LargeValue() {
125
+ // Given
126
+ let value = Int.max
127
+
128
+ // When
129
+ storage.set(value, forKey: testKey)
130
+
131
+ // Then
132
+ XCTAssertEqual(storage.integer(forKey: testKey), value)
133
+ }
134
+
135
+ func testSetInt_OverwritesExistingValue() {
136
+ // Given
137
+ storage.set(10, forKey: testKey)
138
+ let newValue = 20
139
+
140
+ // When
141
+ storage.resetSynchronizeFlag()
142
+ storage.set(newValue, forKey: testKey)
143
+
144
+ // Then
145
+ XCTAssertEqual(storage.integer(forKey: testKey), newValue)
146
+ XCTAssertTrue(storage.wasSynchronizeCalled())
147
+ }
148
+
149
+ // MARK: - Object Storage Tests
150
+
151
+ func testSetObject_StoresDictionary() {
152
+ // Given
153
+ let dictionary: [String: Any] = [
154
+ "name": "John Doe",
155
+ "age": 30,
156
+ "active": true
157
+ ]
158
+
159
+ // When
160
+ storage.set(dictionary, forKey: testKey)
161
+
162
+ // Then
163
+ let retrieved = storage.object(forKey: testKey) as? [String: Any]
164
+ XCTAssertNotNil(retrieved)
165
+ XCTAssertEqual(retrieved?["name"] as? String, "John Doe")
166
+ XCTAssertEqual(retrieved?["age"] as? Int, 30)
167
+ XCTAssertEqual(retrieved?["active"] as? Bool, true)
168
+ }
169
+
170
+ func testSetObject_StoresArray() {
171
+ // Given
172
+ let array = ["item1", "item2", "item3"]
173
+
174
+ // When
175
+ storage.set(array, forKey: testKey)
176
+
177
+ // Then
178
+ let retrieved = storage.object(forKey: testKey) as? [String]
179
+ XCTAssertEqual(retrieved, array)
180
+ }
181
+
182
+ func testSetObject_StoresNestedStructure() {
183
+ // Given
184
+ let nested: [String: Any] = [
185
+ "user": [
186
+ "name": "Jane",
187
+ "preferences": ["theme": "dark", "notifications": true]
188
+ ],
189
+ "scores": [100, 200, 300]
190
+ ]
191
+
192
+ // When
193
+ storage.set(nested, forKey: testKey)
194
+
195
+ // Then
196
+ let retrieved = storage.object(forKey: testKey) as? [String: Any]
197
+ XCTAssertNotNil(retrieved)
198
+ let user = retrieved?["user"] as? [String: Any]
199
+ XCTAssertEqual(user?["name"] as? String, "Jane")
200
+ }
201
+
202
+ // MARK: - Get Operations Tests
203
+
204
+ func testGet_ReturnsNilForNonExistentKey() {
205
+ // When
206
+ let value = storage.string(forKey: "nonExistentKey")
207
+
208
+ // Then
209
+ XCTAssertNil(value)
210
+ }
211
+
212
+ func testGet_ReturnsCorrectValue() {
213
+ // Given
214
+ let expectedValue = "test value"
215
+ storage.set(expectedValue, forKey: testKey)
216
+
217
+ // When
218
+ let retrievedValue = storage.string(forKey: testKey)
219
+
220
+ // Then
221
+ XCTAssertEqual(retrievedValue, expectedValue)
222
+ }
223
+
224
+ func testGetInteger_ReturnsZeroForNonExistentKey() {
225
+ // When
226
+ let value = storage.integer(forKey: "nonExistentKey")
227
+
228
+ // Then
229
+ XCTAssertEqual(value, 0)
230
+ }
231
+
232
+ // MARK: - Remove Operations Tests
233
+
234
+ func testRemove_DeletesValue() {
235
+ // Given
236
+ storage.set("value", forKey: testKey)
237
+ XCTAssertNotNil(storage.string(forKey: testKey))
238
+
239
+ // When
240
+ storage.resetSynchronizeFlag()
241
+ storage.removeObject(forKey: testKey)
242
+
243
+ // Then
244
+ XCTAssertNil(storage.string(forKey: testKey))
245
+ XCTAssertTrue(storage.wasSynchronizeCalled())
246
+ }
247
+
248
+ func testRemove_NonExistentKey() {
249
+ // When/Then - Should not crash
250
+ storage.removeObject(forKey: "nonExistentKey")
251
+ XCTAssertTrue(storage.wasSynchronizeCalled())
252
+ }
253
+
254
+ func testRemove_MultipleKeys() {
255
+ // Given
256
+ storage.set("value1", forKey: "key1")
257
+ storage.set("value2", forKey: "key2")
258
+ storage.set("value3", forKey: "key3")
259
+
260
+ // When
261
+ storage.removeObject(forKey: "key1")
262
+ storage.removeObject(forKey: "key3")
263
+
264
+ // Then
265
+ XCTAssertNil(storage.string(forKey: "key1"))
266
+ XCTAssertNotNil(storage.string(forKey: "key2"))
267
+ XCTAssertNil(storage.string(forKey: "key3"))
268
+ }
269
+
270
+ // MARK: - Get All Keys Tests
271
+
272
+ func testGetAllKeys_EmptyStorage() {
273
+ // When
274
+ let keys = storage.dictionaryRepresentation().keys
275
+
276
+ // Then
277
+ XCTAssertTrue(keys.isEmpty)
278
+ }
279
+
280
+ func testGetAllKeys_ReturnsAllKeys() {
281
+ // Given
282
+ storage.set("value1", forKey: "key1")
283
+ storage.set("value2", forKey: "key2")
284
+ storage.set(42, forKey: "key3")
285
+
286
+ // When
287
+ let keys = Array(storage.dictionaryRepresentation().keys)
288
+
289
+ // Then
290
+ XCTAssertEqual(keys.count, 3)
291
+ XCTAssertTrue(keys.contains("key1"))
292
+ XCTAssertTrue(keys.contains("key2"))
293
+ XCTAssertTrue(keys.contains("key3"))
294
+ }
295
+
296
+ // MARK: - Get All Data Tests
297
+
298
+ func testGetAllData_EmptyStorage() {
299
+ // When
300
+ let data = storage.dictionaryRepresentation()
301
+
302
+ // Then
303
+ XCTAssertTrue(data.isEmpty)
304
+ }
305
+
306
+ func testGetAllData_ReturnsAllData() {
307
+ // Given
308
+ storage.set("value1", forKey: "key1")
309
+ storage.set(42, forKey: "key2")
310
+ storage.set(true, forKey: "key3")
311
+
312
+ // When
313
+ let data = storage.dictionaryRepresentation()
314
+
315
+ // Then
316
+ XCTAssertEqual(data.count, 3)
317
+ XCTAssertEqual(data["key1"] as? String, "value1")
318
+ XCTAssertEqual(data["key2"] as? Int, 42)
319
+ XCTAssertEqual(data["key3"] as? Bool, true)
320
+ }
321
+
322
+ // MARK: - Clear All Tests
323
+
324
+ func testClearAll_RemovesAllData() {
325
+ // Given
326
+ storage.set("value1", forKey: "key1")
327
+ storage.set("value2", forKey: "key2")
328
+ storage.set(42, forKey: "key3")
329
+ XCTAssertEqual(storage.getStorageSize(), 3)
330
+
331
+ // When
332
+ let dict = storage.dictionaryRepresentation()
333
+ for key in dict.keys {
334
+ storage.removeObject(forKey: key)
335
+ }
336
+
337
+ // Then
338
+ XCTAssertEqual(storage.getStorageSize(), 0)
339
+ XCTAssertTrue(storage.wasSynchronizeCalled())
340
+ }
341
+
342
+ func testClearAll_OnEmptyStorage() {
343
+ // Given
344
+ XCTAssertEqual(storage.getStorageSize(), 0)
345
+
346
+ // When
347
+ let dict = storage.dictionaryRepresentation()
348
+ for key in dict.keys {
349
+ storage.removeObject(forKey: key)
350
+ }
351
+
352
+ // Then
353
+ XCTAssertEqual(storage.getStorageSize(), 0)
354
+ }
355
+
356
+ // MARK: - Multiple App Groups Tests
357
+
358
+ func testMultipleAppGroups_IsolatedStorage() {
359
+ // Given
360
+ let group1 = MockUserDefaults(suiteName: "group.com.test.app1")
361
+ let group2 = MockUserDefaults(suiteName: "group.com.test.app2")
362
+
363
+ // When
364
+ group1?.set("value1", forKey: testKey)
365
+ group2?.set("value2", forKey: testKey)
366
+
367
+ // Then
368
+ XCTAssertEqual(group1?.string(forKey: testKey), "value1")
369
+ XCTAssertEqual(group2?.string(forKey: testKey), "value2")
370
+
371
+ // Cleanup
372
+ group1?.clearStorage()
373
+ group2?.clearStorage()
374
+ }
375
+
376
+ // MARK: - Synchronization Tests
377
+
378
+ func testSynchronize_CalledOnSet() {
379
+ // When
380
+ storage.resetSynchronizeFlag()
381
+ storage.set("value", forKey: testKey)
382
+
383
+ // Then
384
+ XCTAssertTrue(storage.wasSynchronizeCalled())
385
+ }
386
+
387
+ func testSynchronize_CalledOnRemove() {
388
+ // Given
389
+ storage.set("value", forKey: testKey)
390
+
391
+ // When
392
+ storage.resetSynchronizeFlag()
393
+ storage.removeObject(forKey: testKey)
394
+
395
+ // Then
396
+ XCTAssertTrue(storage.wasSynchronizeCalled())
397
+ }
398
+
399
+ func testSynchronize_CalledOnClearAll() {
400
+ // Given
401
+ storage.set("value", forKey: testKey)
402
+
403
+ // When
404
+ storage.resetSynchronizeFlag()
405
+ let dict = storage.dictionaryRepresentation()
406
+ for key in dict.keys {
407
+ storage.removeObject(forKey: key)
408
+ }
409
+ storage.synchronize()
410
+
411
+ // Then
412
+ XCTAssertTrue(storage.wasSynchronizeCalled())
413
+ }
414
+
415
+ // MARK: - Edge Cases Tests
416
+
417
+ func testLongKeyName() {
418
+ // Given
419
+ let longKey = String(repeating: "a", count: 1000)
420
+
421
+ // When
422
+ storage.set("value", forKey: longKey)
423
+
424
+ // Then
425
+ XCTAssertEqual(storage.string(forKey: longKey), "value")
426
+ }
427
+
428
+ func testLongValue() {
429
+ // Given
430
+ let longValue = String(repeating: "x", count: 10000)
431
+
432
+ // When
433
+ storage.set(longValue, forKey: testKey)
434
+
435
+ // Then
436
+ XCTAssertEqual(storage.string(forKey: testKey), longValue)
437
+ }
438
+
439
+ func testSpecialCharactersInKey() {
440
+ // Given
441
+ let specialKey = "key@#$%^&*()_+-={}[]|\\:\";<>?,./~`"
442
+
443
+ // When
444
+ storage.set("value", forKey: specialKey)
445
+
446
+ // Then
447
+ XCTAssertEqual(storage.string(forKey: specialKey), "value")
448
+ }
449
+
450
+ func testNilAppGroup() {
451
+ // Given
452
+ let nilGroupDefaults = MockUserDefaults(suiteName: nil)
453
+
454
+ // When
455
+ nilGroupDefaults?.set("value", forKey: testKey)
456
+
457
+ // Then
458
+ XCTAssertEqual(nilGroupDefaults?.string(forKey: testKey), "value")
459
+
460
+ // Cleanup
461
+ nilGroupDefaults?.clearStorage()
462
+ }
463
+
464
+ // MARK: - Data Type Mixing Tests
465
+
466
+ func testOverwriteStringWithInt() {
467
+ // Given
468
+ storage.set("string value", forKey: testKey)
469
+
470
+ // When
471
+ storage.set(42, forKey: testKey)
472
+
473
+ // Then
474
+ XCTAssertEqual(storage.integer(forKey: testKey), 42)
475
+ XCTAssertNil(storage.string(forKey: testKey))
476
+ }
477
+
478
+ func testOverwriteIntWithObject() {
479
+ // Given
480
+ storage.set(42, forKey: testKey)
481
+
482
+ // When
483
+ let dict = ["key": "value"]
484
+ storage.set(dict, forKey: testKey)
485
+
486
+ // Then
487
+ let retrieved = storage.object(forKey: testKey) as? [String: String]
488
+ XCTAssertEqual(retrieved, dict)
489
+ XCTAssertEqual(storage.integer(forKey: testKey), 0)
490
+ }
491
+
492
+ // MARK: - Concurrent Access Tests
493
+
494
+ func testConcurrentWrites() {
495
+ // Given
496
+ let expectation = XCTestExpectation(description: "Concurrent writes complete")
497
+ expectation.expectedFulfillmentCount = 100
498
+ let queue = DispatchQueue(label: "test.concurrent", attributes: .concurrent)
499
+
500
+ // When
501
+ for i in 0..<100 {
502
+ queue.async {
503
+ self.storage.set("value\(i)", forKey: "key\(i)")
504
+ expectation.fulfill()
505
+ }
506
+ }
507
+
508
+ // Then
509
+ wait(for: [expectation], timeout: 5.0)
510
+ XCTAssertEqual(storage.getStorageSize(), 100)
511
+ }
512
+
513
+ func testConcurrentReadsAndWrites() {
514
+ // Given
515
+ let writeExpectation = XCTestExpectation(description: "Writes complete")
516
+ writeExpectation.expectedFulfillmentCount = 50
517
+ let readExpectation = XCTestExpectation(description: "Reads complete")
518
+ readExpectation.expectedFulfillmentCount = 50
519
+ let queue = DispatchQueue(label: "test.concurrent.rw", attributes: .concurrent)
520
+
521
+ // Pre-populate some data
522
+ for i in 0..<50 {
523
+ storage.set("initial\(i)", forKey: "key\(i)")
524
+ }
525
+
526
+ // When
527
+ for i in 0..<50 {
528
+ queue.async {
529
+ self.storage.set("new\(i)", forKey: "key\(i)")
530
+ writeExpectation.fulfill()
531
+ }
532
+ queue.async {
533
+ _ = self.storage.string(forKey: "key\(i)")
534
+ readExpectation.fulfill()
535
+ }
536
+ }
537
+
538
+ // Then
539
+ wait(for: [writeExpectation, readExpectation], timeout: 5.0)
540
+ }
541
+
542
+ // MARK: - Performance Tests
543
+
544
+ func testPerformance_SetString() {
545
+ measure {
546
+ for i in 0..<1000 {
547
+ storage.set("value\(i)", forKey: "key\(i)")
548
+ }
549
+ }
550
+ }
551
+
552
+ func testPerformance_GetString() {
553
+ // Setup
554
+ for i in 0..<1000 {
555
+ storage.set("value\(i)", forKey: "key\(i)")
556
+ }
557
+
558
+ measure {
559
+ for i in 0..<1000 {
560
+ _ = storage.string(forKey: "key\(i)")
561
+ }
562
+ }
563
+ }
564
+
565
+ func testPerformance_SetObject() {
566
+ let testObject: [String: Any] = [
567
+ "name": "Test",
568
+ "value": 42,
569
+ "nested": ["a": 1, "b": 2, "c": 3]
570
+ ]
571
+
572
+ measure {
573
+ for i in 0..<100 {
574
+ storage.set(testObject, forKey: "key\(i)")
575
+ }
576
+ }
577
+ }
578
+ }