1- import { afterAll , describe , expect } from "vitest" ;
1+ import { afterAll , assert , describe , expect } from "vitest" ;
22import { alchemy } from "../../src/alchemy.ts" ;
33import { createCloudflareApi } from "../../src/cloudflare/api.ts" ;
4- import { DnsRecords } from "../../src/cloudflare/dns-records.ts" ;
4+ import { DnsRecords , listRecords } from "../../src/cloudflare/dns-records.ts" ;
55import { Zone } from "../../src/cloudflare/zone.ts" ;
66import { destroy } from "../../src/destroy.ts" ;
77import { BRANCH_PREFIX } from "../util.ts" ;
@@ -15,13 +15,11 @@ const test = alchemy.test(import.meta, {
1515
1616const testDomain = `${ BRANCH_PREFIX } -test-2.com` ;
1717
18- const isEnabled = process . env . ALL_TESTS ;
19-
18+ const api = await createCloudflareApi ( ) ;
2019let zone : Zone ;
21-
2220let scope : Scope | undefined ;
21+
2322test . beforeAll ( async ( _scope ) => {
24- if ( ! isEnabled ) return ;
2523 zone = await Zone ( `${ BRANCH_PREFIX } -zone` , {
2624 name : testDomain ,
2725 } ) ;
@@ -34,9 +32,8 @@ afterAll(async () => {
3432 }
3533} ) ;
3634
37- describe . skipIf ( ! isEnabled ) ( "DnsRecords Resource" , async ( ) => {
35+ describe . sequential ( "DnsRecords Resource" , async ( ) => {
3836 // Use BRANCH_PREFIX for deterministic, non-colliding resource names
39- const api = await createCloudflareApi ( ) ;
4037
4138 test ( "create, update, and delete DNS records" , async ( scope ) => {
4239 let dnsRecords ;
@@ -73,39 +70,7 @@ describe.skipIf(!isEnabled)("DnsRecords Resource", async () => {
7370 expect ( dnsRecords . records ) . toHaveLength ( 3 ) ;
7471
7572 // Verify records were created by querying the API directly
76- for ( const record of dnsRecords . records ) {
77- let response ;
78- const start = Date . now ( ) ;
79- const timeout = 120_000 ; // 120 seconds
80- const interval = 500 ; // 0.5 seconds
81- while ( true ) {
82- response = await api . get (
83- `/zones/${ dnsRecords . zoneId } /dns_records/${ record . id } ` ,
84- ) ;
85- if ( response . ok ) {
86- try {
87- const data : any = await response . json ( ) ;
88- expect ( data . result . name ) . toBe ( record . name ) ;
89- expect ( data . result . type ) . toBe ( record . type ) ;
90- expect ( data . result . content ) . toBe ( record . content ) ;
91- expect ( data . result . proxied ) . toBe ( record . proxied ) ;
92- expect ( data . result . comment ) . toBe ( record . comment ) ;
93- if ( record . priority ) {
94- expect ( data . result . priority ) . toBe ( record . priority ) ;
95- }
96- break ;
97- } catch ( err ) {
98- console . error ( "Error parsing response:" , err ) ;
99- }
100- }
101- if ( Date . now ( ) - start > timeout ) {
102- throw new Error (
103- `DNS record ${ record . id } did not become available within 10s` ,
104- ) ;
105- }
106- await new Promise ( ( resolve ) => setTimeout ( resolve , interval ) ) ;
107- }
108- }
73+ await assertRecordsMatch ( dnsRecords ) ;
10974
11075 // Update records - modify one record, add one record, remove one record
11176 dnsRecords = await DnsRecords ( `${ testDomain } -dns` , {
@@ -162,34 +127,13 @@ describe.skipIf(!isEnabled)("DnsRecords Resource", async () => {
162127 expect ( mailRecord ) . toBeUndefined ( ) ;
163128
164129 // Verify directly with API
165- const listResponse = await api . get (
166- `/zones/${ dnsRecords . zoneId } /dns_records` ,
167- ) ;
168- expect ( listResponse . ok ) . toBe ( true ) ;
169-
170- const listData : any = await listResponse . json ( ) ;
171- const apiRecords = listData . result ;
172-
173- // Should find our 3 records
174- const testRecords = apiRecords . filter ( ( r : any ) =>
175- r . name . includes ( testDomain ) ,
176- ) ;
177- expect ( testRecords ) . toHaveLength ( 3 ) ;
130+ await assertRecordsMatch ( dnsRecords ) ;
178131 } catch ( err ) {
179132 console . error ( "Test failed:" , err ) ;
180133 throw err ;
181134 } finally {
182- // Clean up all resources
183135 await destroy ( scope ) ;
184- // Verify records were deleted
185- if ( dnsRecords ?. records ) {
186- for ( const record of dnsRecords . records ) {
187- const response = await api . get (
188- `/zones/${ dnsRecords . zoneId } /dns_records/${ record . id } ` ,
189- ) ;
190- expect ( response . status ) . toBe ( 404 ) ;
191- }
192- }
136+ await assertRecordsDeleted ( zone . id ) ;
193137 }
194138 } ) ;
195139
@@ -230,6 +174,78 @@ describe.skipIf(!isEnabled)("DnsRecords Resource", async () => {
230174 expect ( dnsRecords . records [ 0 ] . content ) . toBe ( "192.0.2.2" ) ;
231175 } finally {
232176 await destroy ( scope ) ;
177+ await assertRecordsDeleted ( zone . id ) ;
178+ }
179+ } ) ;
180+
181+ test ( "handles multiple TXT records" , async ( scope ) => {
182+ try {
183+ let dnsRecords = await DnsRecords ( `${ testDomain } -txt-dns` , {
184+ zoneId : zone . id ,
185+ records : [
186+ {
187+ name : `www.${ testDomain } ` ,
188+ type : "TXT" ,
189+ content : "v=spf1 include:_spf.google.com ~all" ,
190+ } ,
191+ {
192+ name : `www.${ testDomain } ` ,
193+ type : "TXT" ,
194+ content : "google-site-verification=1234567890" ,
195+ } ,
196+ {
197+ name : `www.${ testDomain } ` ,
198+ type : "A" ,
199+ content : "192.0.2.1" ,
200+ } ,
201+ ] ,
202+ } ) ;
203+ await assertRecordsMatch ( dnsRecords ) ;
204+
205+ dnsRecords = await DnsRecords ( `${ testDomain } -txt-dns` , {
206+ zoneId : zone . id ,
207+ records : [
208+ {
209+ name : `www.${ testDomain } ` ,
210+ type : "TXT" ,
211+ content : "v=spf1 include:_spf.google.com ~all" ,
212+ } ,
213+ {
214+ name : `www.${ testDomain } ` ,
215+ type : "TXT" ,
216+ content : "google-site-verification=1234567890" ,
217+ } ,
218+ {
219+ name : `www.${ testDomain } ` ,
220+ type : "A" ,
221+ content : "192.0.2.2" ,
222+ } ,
223+ ] ,
224+ } ) ;
225+
226+ await assertRecordsMatch ( dnsRecords ) ;
227+ } finally {
228+ await destroy ( scope ) ;
229+ await assertRecordsDeleted ( zone . id ) ;
233230 }
234231 } ) ;
235232} ) ;
233+
234+ async function assertRecordsMatch ( dnsRecords : DnsRecords ) {
235+ const apiRecords = await listRecords ( api , dnsRecords . zoneId ) ;
236+ expect ( apiRecords ) . toHaveLength ( dnsRecords . records . length ) ;
237+ for ( const record of dnsRecords . records ) {
238+ const apiRecord = apiRecords . find ( ( r ) => r . id === record . id ) ;
239+ assert ( apiRecord , `Record ${ record . name } (${ record . id } ) not found in API` ) ;
240+ expect ( apiRecord . name ) . toBe ( record . name ) ;
241+ expect ( apiRecord . type ) . toBe ( record . type ) ;
242+ expect ( apiRecord . content ) . toBe ( record . content ) ;
243+ expect ( apiRecord . proxied ) . toBe ( record . proxied ) ;
244+ expect ( apiRecord . comment ) . toBe ( record . comment ) ;
245+ }
246+ }
247+
248+ async function assertRecordsDeleted ( zoneId : string ) {
249+ const apiRecords = await listRecords ( api , zoneId ) ;
250+ expect ( apiRecords ) . toHaveLength ( 0 ) ;
251+ }
0 commit comments