1- import { assert } from '@std/assert'
1+ import { assert , assertRejects } from '@std/assert'
22import { pathsToTree } from '../files/filetree.ts'
33import { validate } from './bids.ts'
44
@@ -67,6 +67,9 @@ Deno.test('Smoke tests of main validation function', async (t) => {
6767 assert ( warnings . get ( { location : '/dataset_description.json' } ) . length === 0 )
6868 } )
6969 await t . step ( 'Schema source is reported in validation output' , async ( ) => {
70+ // Check network permission status
71+ const netPermission = await Deno . permissions . query ( { name : 'net' } )
72+
7073 // Test with default schema (no source should be provided)
7174 let result = await validate ( dataset , {
7275 datasetPath : '/dataset' ,
@@ -78,46 +81,114 @@ Deno.test('Smoke tests of main validation function', async (t) => {
7881 assert ( result . summary . schemaVersion )
7982 assert ( result . summary . schemaSource === undefined )
8083
81- // Test with custom schema URL
82- result = await validate ( dataset , {
83- datasetPath : '/dataset' ,
84- debug : 'INFO' ,
85- ignoreNiftiHeaders : true ,
86- blacklistModalities : [ ] ,
87- datasetTypes : [ ] ,
88- schema : 'https://example.com/schema.json' ,
89- } )
90- assert ( result . summary . schemaVersion )
91- // Since the URL won't be reachable, it should fall back to default and not set source
92- assert ( result . summary . schemaSource === undefined )
84+ // Test with custom schema URL - should throw error without network
85+ if ( netPermission . state !== 'granted' ) {
86+ await assertRejects (
87+ async ( ) => await validate ( dataset , {
88+ datasetPath : '/dataset' ,
89+ debug : 'INFO' ,
90+ ignoreNiftiHeaders : true ,
91+ blacklistModalities : [ ] ,
92+ datasetTypes : [ ] ,
93+ schema : 'https://example.com/schema.json' ,
94+ } ) ,
95+ Error ,
96+ 'Failed to load schema'
97+ )
98+ } else {
99+ // With network, might fail with 404 or succeed with source set
100+ try {
101+ result = await validate ( dataset , {
102+ datasetPath : '/dataset' ,
103+ debug : 'INFO' ,
104+ ignoreNiftiHeaders : true ,
105+ blacklistModalities : [ ] ,
106+ datasetTypes : [ ] ,
107+ schema : 'https://example.com/schema.json' ,
108+ } )
109+ // If it works, source should be set
110+ assert ( result . summary . schemaVersion )
111+ assert ( result . summary . schemaSource === 'https://example.com/schema.json' )
112+ } catch ( error ) {
113+ // Expected to fail with unreachable URL
114+ assert ( error instanceof Error )
115+ assert ( error . message . includes ( 'Failed to load schema' ) )
116+ }
117+ }
93118
94119 // Test with version tag
95- result = await validate ( dataset , {
96- datasetPath : '/dataset' ,
97- debug : 'INFO' ,
98- ignoreNiftiHeaders : true ,
99- blacklistModalities : [ ] ,
100- datasetTypes : [ ] ,
101- schema : 'v1.9.0' ,
102- } )
103- assert ( result . summary . schemaVersion )
104- // Since network fetch will likely fail, it should fall back to default
105- assert ( result . summary . schemaSource === undefined )
120+ if ( netPermission . state !== 'granted' ) {
121+ await assertRejects (
122+ async ( ) => await validate ( dataset , {
123+ datasetPath : '/dataset' ,
124+ debug : 'INFO' ,
125+ ignoreNiftiHeaders : true ,
126+ blacklistModalities : [ ] ,
127+ datasetTypes : [ ] ,
128+ schema : 'v1.9.0' ,
129+ } ) ,
130+ Error ,
131+ 'Failed to load schema'
132+ )
133+ } else {
134+ // With network, might succeed
135+ try {
136+ result = await validate ( dataset , {
137+ datasetPath : '/dataset' ,
138+ debug : 'INFO' ,
139+ ignoreNiftiHeaders : true ,
140+ blacklistModalities : [ ] ,
141+ datasetTypes : [ ] ,
142+ schema : 'v1.9.0' ,
143+ } )
144+ assert ( result . summary . schemaVersion )
145+ // If successful, source should be the constructed URL
146+ if ( result . summary . schemaSource ) {
147+ assert ( result . summary . schemaSource . includes ( 'v1.9.0' ) )
148+ }
149+ } catch ( error ) {
150+ // Could fail if version doesn't exist
151+ assert ( error instanceof Error )
152+ assert ( error . message . includes ( 'Failed to load schema' ) )
153+ }
154+ }
106155
107156 // Test with BIDS_SCHEMA environment variable
108157 const originalEnv = Deno . env . get ( 'BIDS_SCHEMA' )
109158 try {
110159 Deno . env . set ( 'BIDS_SCHEMA' , 'https://custom-schema.example.com/schema.json' )
111- result = await validate ( dataset , {
112- datasetPath : '/dataset' ,
113- debug : 'INFO' ,
114- ignoreNiftiHeaders : true ,
115- blacklistModalities : [ ] ,
116- datasetTypes : [ ] ,
117- } )
118- assert ( result . summary . schemaVersion )
119- // Environment variable should override, but since network will fail, source won't be set
120- assert ( result . summary . schemaSource === undefined )
160+
161+ if ( netPermission . state !== 'granted' ) {
162+ await assertRejects (
163+ async ( ) => await validate ( dataset , {
164+ datasetPath : '/dataset' ,
165+ debug : 'INFO' ,
166+ ignoreNiftiHeaders : true ,
167+ blacklistModalities : [ ] ,
168+ datasetTypes : [ ] ,
169+ } ) ,
170+ Error ,
171+ 'Failed to load schema'
172+ )
173+ } else {
174+ // With network, might fail with 404
175+ try {
176+ result = await validate ( dataset , {
177+ datasetPath : '/dataset' ,
178+ debug : 'INFO' ,
179+ ignoreNiftiHeaders : true ,
180+ blacklistModalities : [ ] ,
181+ datasetTypes : [ ] ,
182+ } )
183+ assert ( result . summary . schemaVersion )
184+ if ( result . summary . schemaSource ) {
185+ assert ( result . summary . schemaSource === 'https://custom-schema.example.com/schema.json' )
186+ }
187+ } catch ( error ) {
188+ assert ( error instanceof Error )
189+ assert ( error . message . includes ( 'Failed to load schema' ) )
190+ }
191+ }
121192 } finally {
122193 if ( originalEnv !== undefined ) {
123194 Deno . env . set ( 'BIDS_SCHEMA' , originalEnv )
0 commit comments