1- import { describe , expect , test } from " vitest" ;
2- import { normalize } from " ../../src/presentation-4" ;
1+ import { describe , expect , test } from ' vitest' ;
2+ import { normalize , serialize , serializeConfigPresentation4 } from ' ../../src/presentation-4' ;
33
4- describe ( " presentation-4 specific resource parity" , ( ) => {
5- test ( " coerces start, range items, and annotation target for v3 input and keeps selectors" , ( ) => {
4+ describe ( ' presentation-4 specific resource parity' , ( ) => {
5+ test ( ' coerces start, range items, and annotation target for v3 input and keeps selectors' , ( ) => {
66 const manifest = {
7- " @context" : " http://iiif.io/api/presentation/3/context.json" ,
8- id : " https://example.org/manifest/p3-upgrade" ,
9- type : " Manifest" ,
10- label : { en : [ " p3 specific resource parity" ] } ,
11- start : " https://example.org/canvas/1#t=5,15" ,
7+ ' @context' : ' http://iiif.io/api/presentation/3/context.json' ,
8+ id : ' https://example.org/manifest/p3-upgrade' ,
9+ type : ' Manifest' ,
10+ label : { en : [ ' p3 specific resource parity' ] } ,
11+ start : ' https://example.org/canvas/1#t=5,15' ,
1212 items : [
1313 {
14- id : " https://example.org/canvas/1" ,
15- type : " Canvas" ,
14+ id : ' https://example.org/canvas/1' ,
15+ type : ' Canvas' ,
1616 width : 1000 ,
1717 height : 1000 ,
1818 items : [
1919 {
20- id : " https://example.org/canvas/1/page/1" ,
21- type : " AnnotationPage" ,
20+ id : ' https://example.org/canvas/1/page/1' ,
21+ type : ' AnnotationPage' ,
2222 items : [
2323 {
24- id : " https://example.org/canvas/1/annotation/1" ,
25- type : " Annotation" ,
26- motivation : " painting" ,
24+ id : ' https://example.org/canvas/1/annotation/1' ,
25+ type : ' Annotation' ,
26+ motivation : ' painting' ,
2727 body : {
28- id : " https://example.org/image/1.jpg" ,
29- type : " Image" ,
30- format : " image/jpeg" ,
28+ id : ' https://example.org/image/1.jpg' ,
29+ type : ' Image' ,
30+ format : ' image/jpeg' ,
3131 } ,
32- target : " https://example.org/canvas/1#xywh=10,20,30,40" ,
32+ target : ' https://example.org/canvas/1#xywh=10,20,30,40' ,
3333 } ,
3434 ] ,
3535 } ,
@@ -38,17 +38,17 @@ describe("presentation-4 specific resource parity", () => {
3838 ] ,
3939 structures : [
4040 {
41- id : " https://example.org/range/1" ,
42- type : " Range" ,
43- items : [ " https://example.org/canvas/1#t=0,10" ] ,
41+ id : ' https://example.org/range/1' ,
42+ type : ' Range' ,
43+ items : [ ' https://example.org/canvas/1#t=0,10' ] ,
4444 } ,
4545 ] ,
4646 } ;
4747
4848 const result = normalize ( manifest as any ) ;
49- const normalizedManifest = result . entities . Manifest [ " https://example.org/manifest/p3-upgrade" ] as any ;
50- const normalizedRange = result . entities . Range [ " https://example.org/range/1" ] as any ;
51- const normalizedAnnotation = result . entities . Annotation [ " https://example.org/canvas/1/annotation/1" ] as any ;
49+ const normalizedManifest = result . entities . Manifest [ ' https://example.org/manifest/p3-upgrade' ] as any ;
50+ const normalizedRange = result . entities . Range [ ' https://example.org/range/1' ] as any ;
51+ const normalizedAnnotation = result . entities . Annotation [ ' https://example.org/canvas/1/annotation/1' ] as any ;
5252 const startSelector = Array . isArray ( normalizedManifest . start . selector )
5353 ? normalizedManifest . start . selector [ 0 ]
5454 : normalizedManifest . start . selector ;
@@ -59,24 +59,91 @@ describe("presentation-4 specific resource parity", () => {
5959 ? normalizedAnnotation . target [ 0 ] . selector [ 0 ]
6060 : normalizedAnnotation . target [ 0 ] . selector ;
6161
62- expect ( normalizedManifest . start . type ) . toBe ( " SpecificResource" ) ;
63- expect ( normalizedManifest . start . source . id ) . toBe ( " https://example.org/canvas/1" ) ;
64- expect ( startSelector . type ) . toBe ( " FragmentSelector" ) ;
65- expect ( startSelector . value ) . toBe ( " t=5,15" ) ;
62+ expect ( normalizedManifest . start . type ) . toBe ( ' SpecificResource' ) ;
63+ expect ( normalizedManifest . start . source . id ) . toBe ( ' https://example.org/canvas/1' ) ;
64+ expect ( startSelector . type ) . toBe ( ' FragmentSelector' ) ;
65+ expect ( startSelector . value ) . toBe ( ' t=5,15' ) ;
6666
67- expect ( normalizedRange . items [ 0 ] . type ) . toBe ( " SpecificResource" ) ;
68- expect ( normalizedRange . items [ 0 ] . source . id ) . toBe ( " https://example.org/canvas/1" ) ;
69- expect ( rangeSelector . type ) . toBe ( " FragmentSelector" ) ;
70- expect ( rangeSelector . value ) . toBe ( " t=0,10" ) ;
67+ expect ( normalizedRange . items [ 0 ] . type ) . toBe ( ' SpecificResource' ) ;
68+ expect ( normalizedRange . items [ 0 ] . source . id ) . toBe ( ' https://example.org/canvas/1' ) ;
69+ expect ( rangeSelector . type ) . toBe ( ' FragmentSelector' ) ;
70+ expect ( rangeSelector . value ) . toBe ( ' t=0,10' ) ;
7171
72- expect ( normalizedAnnotation . target [ 0 ] . type ) . toBe ( " SpecificResource" ) ;
73- expect ( normalizedAnnotation . target [ 0 ] . source . id ) . toBe ( " https://example.org/canvas/1" ) ;
74- expect ( targetSelector . type ) . toBe ( " FragmentSelector" ) ;
75- expect ( targetSelector . value ) . toBe ( " xywh=10,20,30,40" ) ;
72+ expect ( normalizedAnnotation . target [ 0 ] . type ) . toBe ( ' SpecificResource' ) ;
73+ expect ( normalizedAnnotation . target [ 0 ] . source . id ) . toBe ( ' https://example.org/canvas/1' ) ;
74+ expect ( targetSelector . type ) . toBe ( ' FragmentSelector' ) ;
75+ expect ( targetSelector . value ) . toBe ( ' xywh=10,20,30,40' ) ;
7676
7777 const selectorId = targetSelector . id ;
7878 expect ( selectorId ) . toBeTruthy ( ) ;
7979 expect ( result . entities . Selector [ selectorId ] ) . toBeTruthy ( ) ;
80- expect ( result . mapping [ selectorId ] ) . toBe ( "Selector" ) ;
80+ expect ( result . mapping [ selectorId ] ) . toBe ( 'Selector' ) ;
81+ } ) ;
82+
83+ test ( 'preserves selector through normalize and serialize for native p4 fragment targets' , ( ) => {
84+ const manifest = {
85+ '@context' : 'http://iiif.io/api/presentation/4/context.json' ,
86+ id : 'https://example.org/manifest/p4-fragment-target' ,
87+ type : 'Manifest' ,
88+ label : { en : [ 'native p4 selector parity' ] } ,
89+ items : [
90+ {
91+ id : 'https://example.org/canvas/1' ,
92+ type : 'Canvas' ,
93+ width : 1000 ,
94+ height : 1000 ,
95+ items : [
96+ {
97+ id : 'https://example.org/canvas/1/page/1' ,
98+ type : 'AnnotationPage' ,
99+ items : [
100+ {
101+ id : 'https://example.org/canvas/1/annotation/1' ,
102+ type : 'Annotation' ,
103+ motivation : [ 'commenting' ] ,
104+ body : [
105+ {
106+ type : 'Text' ,
107+ id : 'https://example.org/body/1' ,
108+ format : 'text/plain' ,
109+ } ,
110+ ] ,
111+ target : [ 'https://example.org/canvas/1#xywh=11,22,33,44' ] ,
112+ } ,
113+ ] ,
114+ } ,
115+ ] ,
116+ } ,
117+ ] ,
118+ } ;
119+
120+ const normalized = normalize ( manifest as any ) ;
121+ const annotation = normalized . entities . Annotation [ 'https://example.org/canvas/1/annotation/1' ] as any ;
122+ const target = annotation . target [ 0 ] ;
123+ const targetSelector = Array . isArray ( target . selector ) ? target . selector [ 0 ] : target . selector ;
124+
125+ expect ( target . type ) . toBe ( 'SpecificResource' ) ;
126+ expect ( target . source . id ) . toBe ( 'https://example.org/canvas/1' ) ;
127+ expect ( targetSelector . type ) . toBe ( 'FragmentSelector' ) ;
128+ expect ( targetSelector . value ) . toBe ( 'xywh=11,22,33,44' ) ;
129+
130+ const serialized = serialize < any > (
131+ {
132+ entities : normalized . entities as any ,
133+ mapping : normalized . mapping as any ,
134+ requests : { } ,
135+ } ,
136+ normalized . resource ,
137+ serializeConfigPresentation4
138+ ) ;
139+ const serializedTarget = serialized . items [ 0 ] . items [ 0 ] . items [ 0 ] . target [ 0 ] ;
140+ const serializedSelector = Array . isArray ( serializedTarget . selector )
141+ ? serializedTarget . selector [ 0 ]
142+ : serializedTarget . selector ;
143+
144+ expect ( serializedTarget . type ) . toBe ( 'SpecificResource' ) ;
145+ expect ( serializedTarget . source . id ) . toBe ( 'https://example.org/canvas/1' ) ;
146+ expect ( serializedSelector . type ) . toBe ( 'FragmentSelector' ) ;
147+ expect ( serializedSelector . value ) . toBe ( 'xywh=11,22,33,44' ) ;
81148 } ) ;
82149} ) ;
0 commit comments