1+ import type { ChannelDraft } from "@commercetools/platform-sdk" ;
2+ import { describe , expect , test } from "vitest" ;
3+ import type { Config } from "~src/config" ;
4+ import { getBaseResourceProperties } from "~src/helpers" ;
5+ import { InMemoryStorage } from "~src/storage" ;
6+ import { ChannelRepository } from "./channel" ;
7+
8+ describe ( "Channel Repository" , ( ) => {
9+ const storage = new InMemoryStorage ( ) ;
10+ const config : Config = { storage, strict : false } ;
11+ const repository = new ChannelRepository ( config ) ;
12+
13+ // Add a custom type for testing
14+ storage . add ( "dummy" , "type" , {
15+ ...getBaseResourceProperties ( ) ,
16+ id : "custom-type-id" ,
17+ key : "custom-type-key" ,
18+ name : { "en-US" : "Custom Type" } ,
19+ resourceTypeIds : [ "channel" ] ,
20+ fieldDefinitions : [
21+ {
22+ name : "description" ,
23+ label : { "en-US" : "Description" } ,
24+ required : false ,
25+ type : { name : "String" } ,
26+ inputHint : "SingleLine" ,
27+ } ,
28+ ] ,
29+ } ) ;
30+
31+ test ( "create channel" , ( ) => {
32+ const draft : ChannelDraft = {
33+ key : "distribution-center-1" ,
34+ name : { "en-US" : "Distribution Center 1" } ,
35+ description : { "en-US" : "Main distribution center" } ,
36+ roles : [ "InventorySupply" , "OrderExport" ] ,
37+ } ;
38+
39+ const ctx = { projectKey : "dummy" } ;
40+ const result = repository . create ( ctx , draft ) ;
41+
42+ expect ( result . id ) . toBeDefined ( ) ;
43+ expect ( result . version ) . toBe ( 1 ) ;
44+ expect ( result . key ) . toBe ( draft . key ) ;
45+ expect ( result . name ) . toEqual ( draft . name ) ;
46+ expect ( result . description ) . toEqual ( draft . description ) ;
47+ expect ( result . roles ) . toEqual ( draft . roles ) ;
48+ expect ( result . geoLocation ) . toBeUndefined ( ) ;
49+ expect ( result . address ) . toBeUndefined ( ) ;
50+ expect ( result . custom ) . toBeUndefined ( ) ;
51+
52+ // Test that the channel is stored
53+ const items = repository . query ( ctx ) ;
54+ expect ( items . count ) . toBe ( 1 ) ;
55+ expect ( items . results [ 0 ] . id ) . toBe ( result . id ) ;
56+ } ) ;
57+
58+ test ( "create channel with all optional fields" , ( ) => {
59+ const draft : ChannelDraft = {
60+ key : "store-berlin" ,
61+ name : { "en-US" : "Berlin Store" , "de-DE" : "Berlin Geschäft" } ,
62+ description : { "en-US" : "Store in Berlin" } ,
63+ roles : [ "ProductDistribution" ] ,
64+ address : {
65+ country : "DE" ,
66+ city : "Berlin" ,
67+ streetName : "Hauptstraße" ,
68+ streetNumber : "123" ,
69+ postalCode : "10115" ,
70+ } ,
71+ geoLocation : {
72+ type : "Point" ,
73+ coordinates : [ 13.4050 , 52.5200 ] ,
74+ } ,
75+ custom : {
76+ type : {
77+ typeId : "type" ,
78+ id : "custom-type-id" ,
79+ } ,
80+ fields : {
81+ description : "Custom description" ,
82+ } ,
83+ } ,
84+ } ;
85+
86+ const ctx = { projectKey : "dummy" } ;
87+ const result = repository . create ( ctx , draft ) ;
88+
89+ expect ( result . id ) . toBeDefined ( ) ;
90+ expect ( result . key ) . toBe ( draft . key ) ;
91+ expect ( result . name ) . toEqual ( draft . name ) ;
92+ expect ( result . address ?. country ) . toBe ( "DE" ) ;
93+ expect ( result . address ?. city ) . toBe ( "Berlin" ) ;
94+ expect ( result . geoLocation ) . toEqual ( draft . geoLocation ) ;
95+ expect ( result . custom ?. fields . description ) . toBe ( "Custom description" ) ;
96+ } ) ;
97+
98+ test ( "update channel - changeName" , ( ) => {
99+ const draft : ChannelDraft = {
100+ key : "test-channel" ,
101+ name : { "en-US" : "Test Channel" } ,
102+ } ;
103+
104+ const ctx = { projectKey : "dummy" } ;
105+ const channel = repository . create ( ctx , draft ) ;
106+
107+ const result = repository . processUpdateActions ( ctx , channel , channel . version , [
108+ {
109+ action : "changeName" ,
110+ name : { "en-US" : "Updated Test Channel" } ,
111+ } ,
112+ ] ) ;
113+
114+ expect ( result . name ) . toEqual ( { "en-US" : "Updated Test Channel" } ) ;
115+ expect ( result . version ) . toBe ( channel . version + 1 ) ;
116+ } ) ;
117+
118+ test ( "update channel - changeKey" , ( ) => {
119+ const draft : ChannelDraft = {
120+ key : "test-channel-2" ,
121+ name : { "en-US" : "Test Channel 2" } ,
122+ } ;
123+
124+ const ctx = { projectKey : "dummy" } ;
125+ const channel = repository . create ( ctx , draft ) ;
126+
127+ const result = repository . processUpdateActions ( ctx , channel , channel . version , [
128+ {
129+ action : "changeKey" ,
130+ key : "new-channel-key" ,
131+ } ,
132+ ] ) ;
133+
134+ expect ( result . key ) . toBe ( "new-channel-key" ) ;
135+ expect ( result . version ) . toBe ( channel . version + 1 ) ;
136+ } ) ;
137+
138+ test ( "update channel - changeDescription" , ( ) => {
139+ const draft : ChannelDraft = {
140+ key : "test-channel-3" ,
141+ name : { "en-US" : "Test Channel 3" } ,
142+ } ;
143+
144+ const ctx = { projectKey : "dummy" } ;
145+ const channel = repository . create ( ctx , draft ) ;
146+
147+ const result = repository . processUpdateActions ( ctx , channel , channel . version , [
148+ {
149+ action : "changeDescription" ,
150+ description : { "en-US" : "New description" } ,
151+ } ,
152+ ] ) ;
153+
154+ expect ( result . description ) . toEqual ( { "en-US" : "New description" } ) ;
155+ expect ( result . version ) . toBe ( channel . version + 1 ) ;
156+ } ) ;
157+
158+ test ( "update channel - setAddress" , ( ) => {
159+ const draft : ChannelDraft = {
160+ key : "test-channel-4" ,
161+ name : { "en-US" : "Test Channel 4" } ,
162+ } ;
163+
164+ const ctx = { projectKey : "dummy" } ;
165+ const channel = repository . create ( ctx , draft ) ;
166+
167+ const result = repository . processUpdateActions ( ctx , channel , channel . version , [
168+ {
169+ action : "setAddress" ,
170+ address : {
171+ country : "US" ,
172+ city : "New York" ,
173+ streetName : "Broadway" ,
174+ streetNumber : "123" ,
175+ postalCode : "10001" ,
176+ } ,
177+ } ,
178+ ] ) ;
179+
180+ expect ( result . address ?. country ) . toBe ( "US" ) ;
181+ expect ( result . address ?. city ) . toBe ( "New York" ) ;
182+ expect ( result . version ) . toBe ( channel . version + 1 ) ;
183+ } ) ;
184+
185+ test ( "update channel - setGeoLocation" , ( ) => {
186+ const draft : ChannelDraft = {
187+ key : "test-channel-5" ,
188+ name : { "en-US" : "Test Channel 5" } ,
189+ } ;
190+
191+ const ctx = { projectKey : "dummy" } ;
192+ const channel = repository . create ( ctx , draft ) ;
193+
194+ const result = repository . processUpdateActions ( ctx , channel , channel . version , [
195+ {
196+ action : "setGeoLocation" ,
197+ geoLocation : {
198+ type : "Point" ,
199+ coordinates : [ 2.3522 , 48.8566 ] , // Paris coordinates
200+ } ,
201+ } ,
202+ ] ) ;
203+
204+ expect ( result . geoLocation ) . toEqual ( {
205+ type : "Point" ,
206+ coordinates : [ 2.3522 , 48.8566 ] ,
207+ } ) ;
208+ expect ( result . version ) . toBe ( channel . version + 1 ) ;
209+ } ) ;
210+
211+ test ( "update channel - setCustomType" , ( ) => {
212+ const draft : ChannelDraft = {
213+ key : "test-channel-6" ,
214+ name : { "en-US" : "Test Channel 6" } ,
215+ } ;
216+
217+ const ctx = { projectKey : "dummy" } ;
218+ const channel = repository . create ( ctx , draft ) ;
219+
220+ // Set custom type
221+ const result = repository . processUpdateActions ( ctx , channel , channel . version , [
222+ {
223+ action : "setCustomType" ,
224+ type : {
225+ typeId : "type" ,
226+ id : "custom-type-id" ,
227+ } ,
228+ fields : {
229+ description : "New custom field value" ,
230+ } ,
231+ } ,
232+ ] ) ;
233+
234+ expect ( result . custom ) . toBeDefined ( ) ;
235+ expect ( result . custom ?. fields . description ) . toBe ( "New custom field value" ) ;
236+ expect ( result . version ) . toBe ( channel . version + 1 ) ;
237+
238+ // Remove custom type
239+ const result2 = repository . processUpdateActions ( ctx , result , result . version , [
240+ {
241+ action : "setCustomType" ,
242+ } ,
243+ ] ) ;
244+
245+ expect ( result2 . custom ) . toBeUndefined ( ) ;
246+ expect ( result2 . version ) . toBe ( result . version + 1 ) ;
247+ } ) ;
248+
249+ test ( "update channel - setCustomField" , ( ) => {
250+ const draft : ChannelDraft = {
251+ key : "test-channel-7" ,
252+ name : { "en-US" : "Test Channel 7" } ,
253+ custom : {
254+ type : {
255+ typeId : "type" ,
256+ id : "custom-type-id" ,
257+ } ,
258+ fields : {
259+ description : "Initial description" ,
260+ } ,
261+ } ,
262+ } ;
263+
264+ const ctx = { projectKey : "dummy" } ;
265+ const channel = repository . create ( ctx , draft ) ;
266+
267+ // Update custom field
268+ const result = repository . processUpdateActions ( ctx , channel , channel . version , [
269+ {
270+ action : "setCustomField" ,
271+ name : "description" ,
272+ value : "Updated description" ,
273+ } ,
274+ ] ) ;
275+
276+ expect ( result . custom ?. fields . description ) . toBe ( "Updated description" ) ;
277+ expect ( result . version ) . toBe ( channel . version + 1 ) ;
278+
279+ // Remove custom field
280+ const result2 = repository . processUpdateActions ( ctx , result , result . version , [
281+ {
282+ action : "setCustomField" ,
283+ name : "description" ,
284+ value : null ,
285+ } ,
286+ ] ) ;
287+
288+ expect ( result2 . custom ?. fields . description ) . toBeUndefined ( ) ;
289+ expect ( result2 . version ) . toBe ( result . version + 1 ) ;
290+ } ) ;
291+
292+ test ( "get and delete channel" , ( ) => {
293+ const draft : ChannelDraft = {
294+ key : "delete-test" ,
295+ name : { "en-US" : "Delete Test Channel" } ,
296+ } ;
297+
298+ const ctx = { projectKey : "dummy" } ;
299+ const channel = repository . create ( ctx , draft ) ;
300+
301+ // Test get
302+ const retrieved = repository . get ( ctx , channel . id ) ;
303+ expect ( retrieved ) . toBeDefined ( ) ;
304+ expect ( retrieved ?. id ) . toBe ( channel . id ) ;
305+
306+ // Test getByKey
307+ const retrievedByKey = repository . getByKey ( ctx , channel . key ! ) ;
308+ expect ( retrievedByKey ) . toBeDefined ( ) ;
309+ expect ( retrievedByKey ?. key ) . toBe ( channel . key ) ;
310+
311+ // Test delete
312+ const deleted = repository . delete ( ctx , channel . id ) ;
313+ expect ( deleted ) . toBeDefined ( ) ;
314+ expect ( deleted ?. id ) . toBe ( channel . id ) ;
315+
316+ // Verify it's deleted
317+ const notFound = repository . get ( ctx , channel . id ) ;
318+ expect ( notFound ) . toBeNull ( ) ;
319+ } ) ;
320+ } ) ;
0 commit comments