@@ -198,3 +198,64 @@ describe('a BRACKETED `package` key on the two arity doors that read `package`',
198198 }
199199 } ) ;
200200} ) ;
201+
202+ /**
203+ * The same class on `_drafts`' own two filters. `?packageId[]=ID`,
204+ * `?packageId[0]=ID`, `?type[]=TYPE` and `?type[0]=TYPE` reached the door under
205+ * the key as spelled, so `listDrafts` was handed no package and no type, and a
206+ * caller who named one package's (or one type's) drafts got every draft it may
207+ * read. Refused at the door's arity step, before `listDrafts`, in the same
208+ * sentence frame as the list's bracketed `package`.
209+ */
210+ const bracketedDraftsMessage = ( key : string , param : 'packageId' | 'type' ) : string => ( param === 'packageId'
211+ ? `Query parameter '${ key } ' names no package; the 'packageId' filter takes exactly one package id, `
212+ + `spelled ?packageId=ID. The list was not served: a bracketed 'packageId' key is not a filter. `
213+ + `Name one package, or leave the parameter out to list the drafts of every package.`
214+ : `Query parameter '${ key } ' names no metadata type; the 'type' filter takes exactly one metadata type, `
215+ + `spelled ?type=TYPE. The list was not served: a bracketed 'type' key is not a filter. `
216+ + `Name one metadata type, or leave the parameter out to list the drafts of every type.` ) ;
217+ const DRAFTS_KEYS = [
218+ [ 'packageId[]' , 'packageId' ] , [ 'packageId[0]' , 'packageId' ] , [ 'packageId[id]' , 'packageId' ] ,
219+ [ 'type[]' , 'type' ] , [ 'type[0]' , 'type' ] , [ 'type[name]' , 'type' ] ,
220+ ] as const ;
221+
222+ describe ( 'a BRACKETED `packageId` or `type` key on `_drafts`' , ( ) => {
223+ it . each ( DRAFTS_KEYS ) ( '`%s` is refused as data, naming the key' , ( key , param ) => {
224+ expect ( metaQueryArityRefusal ( 'drafts' , { [ key ] : 'x' } ) ) . toEqual ( {
225+ status : 400 ,
226+ code : 'VALIDATION_ERROR' ,
227+ message : bracketedDraftsMessage ( key , param ) ,
228+ rule : 'bracketed-parameter' ,
229+ } ) ;
230+ // Beside the plain key it is two spellings of one filter.
231+ expect ( metaQueryArityRefusal ( 'drafts' , { [ param ] : 'x' , [ key ] : 'y' } ) ?. rule ) . toBe ( 'bracketed-parameter' ) ;
232+ } ) ;
233+
234+ it ( 'a repeated plain `packageId` still answers the multiplicity sentence first' , ( ) => {
235+ expect ( metaQueryArityRefusal ( 'drafts' , { packageId : [ 'a' , 'b' ] , 'type[]' : 'view' } ) ?. message )
236+ . toBe ( repeatedQueryParamMessage ( 'packageId' , 2 ) ) ;
237+ } ) ;
238+
239+ it ( 'is judged only on `_drafts`, and not for a key that merely starts alike' , ( ) => {
240+ expect ( metaQueryArityRefusal ( 'list' , { 'packageId[]' : 'x' , 'type[]' : 'view' } ) ) . toBeUndefined ( ) ;
241+ expect ( metaQueryArityRefusal ( 'book-tree' , { 'type[]' : 'view' } ) ) . toBeUndefined ( ) ;
242+ expect ( metaQueryArityRefusal ( 'drafts' , { 'types[]' : 'view' , 'packageIds[]' : 'x' , 'package[]' : 'x' } ) ) . toBeUndefined ( ) ;
243+ } ) ;
244+
245+ it . each ( DRAFTS_KEYS . filter ( ( [ key ] ) => key . endsWith ( '[]' ) || key . endsWith ( '[0]' ) ) ) (
246+ 'RestServer: `%s` answers the nested 400 before `listDrafts`' ,
247+ async ( key , param ) => {
248+ const { protocol, status, body } = await drive ( 'drafts' , { [ key ] : 'x' } ) ;
249+ expect ( status ) . toBe ( 400 ) ;
250+ expect ( body ) . toEqual ( { error : { code : 'VALIDATION_ERROR' , message : bracketedDraftsMessage ( key , param ) } } ) ;
251+ expect ( protocol . listDrafts ) . not . toHaveBeenCalled ( ) ;
252+ } ,
253+ ) ;
254+
255+ it ( 'RestServer: the plain spellings still narrow, and absent ones read every draft (controls)' , async ( ) => {
256+ expect ( ( await drive ( 'drafts' , { packageId : 'com.acme' , type : 'view' } ) ) . protocol . listDrafts )
257+ . toHaveBeenCalledWith ( { packageId : 'com.acme' , type : 'view' } ) ;
258+ expect ( ( await drive ( 'drafts' , { } ) ) . protocol . listDrafts )
259+ . toHaveBeenCalledWith ( { packageId : undefined , type : undefined } ) ;
260+ } ) ;
261+ } ) ;
0 commit comments