1- import { deepEqual , equal , match } from "node:assert" ;
1+ import { deepEqual , equal } from "node:assert" ;
22import type { Mock } from "node:test" ;
33import test , { describe , mock } from "node:test" ;
4-
5- const mockConfig = {
6- serviceName : "test-service" ,
7- serviceId : "test-service-id" ,
8- mvtDictionaryId : "test-mvt-dictionary-id" ,
9- mvtDictionaryName : "test-mvt-dictionary" ,
10- abTestsDictionaryId : "test-ab-tests-dictionary-id" ,
11- abTestsDictionaryName : "test-ab-tests-dictionary" ,
12- } ;
13-
14- // Mock environment variables
15- process . env . FASTLY_AB_TESTING_CONFIG = JSON . stringify ( mockConfig ) ;
16- process . env . FASTLY_API_TOKEN = "test-api-token" ;
4+ import { FastlyClient } from "./client.ts" ;
5+ import { FastlyDictionary } from "./dictionary.ts" ;
6+ import { FastlyService } from "./service.ts" ;
177
188type MockedFetch = Mock < typeof fetch > ;
199
@@ -27,18 +17,6 @@ function mockFetch(response: unknown, status = 200, statusText = "OK") {
2717 globalThis . fetch = mock . fn ( async ( ) => Promise . resolve ( mockResponse ) ) ;
2818}
2919
30- // Import after mocking
31- const { FastlyClient } = await import ( "./client.ts" ) ;
32- const {
33- getMVTGroupsFromDictionary,
34- getABTestGroupsFromDictionary,
35- updateMVTGroups,
36- updateABTestGroups,
37- } = await import ( "./dictionary.ts" ) ;
38-
39- const { FastlyService } = await import ( "./service.ts" ) ;
40- const { FastlyDictionary } = await import ( "./dictionary.ts" ) ;
41-
4220describe ( "FastlyDictionary" , async ( ) => {
4321 await test ( "getItems - calls service.getDictionaryItems" , async ( ) => {
4422 const mockResponse = [
@@ -91,93 +69,4 @@ describe("FastlyDictionary", async () => {
9169
9270 equal ( ( globalThis . fetch as MockedFetch ) . mock . calls . length , 1 ) ;
9371 } ) ;
94-
95- test ( "getMVTGroupsFromDictionary - calls the right endpoint" , async ( ) => {
96- const mockResponse = [ ] as unknown ;
97- mockFetch ( mockResponse ) ;
98-
99- const client = new FastlyClient ( "test-api-token" ) ;
100- await getMVTGroupsFromDictionary ( client ) ;
101-
102- equal ( ( globalThis . fetch as MockedFetch ) . mock . calls . length , 1 ) ;
103- match (
104- ( globalThis . fetch as MockedFetch ) . mock . calls [ 0 ]
105- ?. arguments [ 0 ] as string ,
106- new RegExp ( `/dictionary/${ mockConfig . mvtDictionaryId } /items` ) ,
107- ) ;
108- } ) ;
109-
110- test ( "getABTestGroupsFromDictionary - calls the right endpoint" , async ( ) => {
111- const mockResponse = [ ] as unknown ;
112- mockFetch ( mockResponse ) ;
113-
114- const client = new FastlyClient ( "test-api-token" ) ;
115- await getABTestGroupsFromDictionary ( client ) ;
116-
117- equal ( ( globalThis . fetch as MockedFetch ) . mock . calls . length , 1 ) ;
118- match (
119- ( globalThis . fetch as MockedFetch ) . mock . calls [ 0 ]
120- ?. arguments [ 0 ] as string ,
121- new RegExp ( `/dictionary/${ mockConfig . abTestsDictionaryId } /items` ) ,
122- ) ;
123- } ) ;
124-
125- test ( "updateMVTGroups - makes PATCH request with correct data" , async ( ) => {
126- mockFetch ( { status : "ok" } ) ;
127-
128- const client = new FastlyClient ( "test-api-token" ) ;
129- const items = [
130- { item_key : "key1" , item_value : "value1" , op : "create" as const } ,
131- ] ;
132- await updateMVTGroups ( client , items ) ;
133-
134- equal ( ( globalThis . fetch as MockedFetch ) . mock . calls . length , 1 ) ;
135- equal (
136- ( globalThis . fetch as MockedFetch ) . mock . calls [ 0 ] ?. arguments [ 1 ]
137- ?. method ,
138- "PATCH" ,
139- ) ;
140-
141- const requestBody = JSON . parse (
142- ( globalThis . fetch as MockedFetch ) . mock . calls [ 0 ] ?. arguments [ 1 ]
143- ?. body as string ,
144- ) as {
145- items : Array < {
146- item_key : string ;
147- item_value : string ;
148- op : "create" | "update" | "delete" ;
149- } > ;
150- } ;
151-
152- deepEqual ( requestBody . items , items ) ;
153- } ) ;
154-
155- test ( "updateABTestGroups - makes PATCH request with correct data" , async ( ) => {
156- mockFetch ( { status : "ok" } ) ;
157-
158- const client = new FastlyClient ( "test-api-token" ) ;
159- const items = [
160- { item_key : "key1" , item_value : "value1" , op : "update" as const } ,
161- ] ;
162- await updateABTestGroups ( client , items ) ;
163-
164- equal ( ( globalThis . fetch as MockedFetch ) . mock . calls . length , 1 ) ;
165- equal (
166- ( globalThis . fetch as MockedFetch ) . mock . calls [ 0 ] ?. arguments [ 1 ]
167- ?. method ,
168- "PATCH" ,
169- ) ;
170-
171- const requestBody = JSON . parse (
172- ( globalThis . fetch as MockedFetch ) . mock . calls [ 0 ] ?. arguments [ 1 ]
173- ?. body as string ,
174- ) as {
175- items : Array < {
176- item_key : string ;
177- item_value : string ;
178- op : "create" | "update" | "delete" ;
179- } > ;
180- } ;
181- deepEqual ( requestBody . items , items ) ;
182- } ) ;
18372} ) ;
0 commit comments