]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/item-identifiers.lisp
Implement CTM Mergemap with explicit byte resources and isolated environments
[topic-maps.git] / tests / item-identifiers.lisp
1 (defpackage #:topic-maps.identifier.tests
2 (:use #:cl #:topic-maps.tmdm)
3 (:import-from #:topic-maps.ctm.tests #:check #:map-state #:cxtm #:binary-file #:read-text)
4 (:import-from #:topic-maps.variant.tests #:condition-from)
5 (:export #:run-tests #:identifier-checks #:write-identifier-report))
6 (in-package #:topic-maps.identifier.tests)
7
8 (defvar *last-checks* nil)
9 (defvar *examples* nil)
10 (defvar *ordering-output* nil)
11 (defparameter *kinds* '(topic-map topic topic-name variant occurrence association association-role))
12 (defparameter *readers* '(topic-map-item-identifiers topic-item-identifiers topic-name-item-identifiers
13 variant-item-identifiers occurrence-item-identifiers association-item-identifiers
14 association-role-item-identifiers))
15 (defun identifier-checks () *last-checks*)
16 (defun si (map name) (intern-topic map :subject-identifier (concatenate 'string "urn:" name)))
17 (defun fixture (&optional (map (make-topic-map)) (prefix ""))
18 (let* ((owner (si map (concatenate 'string prefix "owner")))
19 (type (si map (concatenate 'string prefix "type")))
20 (theme (si map (concatenate 'string prefix "theme")))
21 (name (add-name owner "Name" :type type))
22 (variant (add-variant name "Variant" :scope (list theme)))
23 (occurrence (add-occurrence owner type "Value" :scope (list theme)))
24 (association (make-association map type (list (list type owner) (list type theme)) :scope (list theme))))
25 (values map (list map owner name variant occurrence association (first (association-roles association))) type theme)))
26 (defun current (item) (topic-maps.tmdm::canonical-item item))
27 (defun raw-handles (items)
28 (mapcar (lambda (i)
29 (list i (copy-tree (mapcar #'copy-seq (topic-maps.tmdm::%item-identifiers i)))
30 (etypecase i
31 (topic-map nil) (topic (topic-maps.tmdm::%topic-forward i))
32 (topic-name (topic-maps.tmdm::%topic-name-forward i)) (variant (topic-maps.tmdm::%variant-forward i))
33 (occurrence (topic-maps.tmdm::%occurrence-forward i)) (association (topic-maps.tmdm::%association-forward i))
34 (association-role (topic-maps.tmdm::%association-role-forward i))))) items))
35 (defun rejects-without-mutation (label map thunk &optional handles)
36 (let* ((before (map-state map)) (old (raw-handles handles)) (c (condition-from thunk)))
37 (check (concatenate 'string label ":specific")
38 (and (typep c 'item-identifier-error) (eq :duplicate-item-identifier (item-identifier-error-code c))))
39 (check (concatenate 'string label ":atomic")
40 (and (equal before (map-state map)) (equal old (raw-handles handles))))))
41
42 (defun set-tests ()
43 (multiple-value-bind (map items) (fixture)
44 (loop for item in items for kind in *kinds* for reader in *readers* do
45 (check (format nil "sets:~A:empty" kind) (and (null (item-identifiers item)) (null (funcall reader item))))
46 (let ((input (format nil "urn:item:~A" kind)))
47 (check (format nil "sets:~A:return-current" kind) (eq item (add-item-identifier item input)))
48 (let ((saved (copy-seq input)))
49 (setf (char input 0) #\X)
50 (let ((ids (item-identifiers item)) (specific (funcall reader item)))
51 (setf (char (first ids) 0) #\Y (car ids) "replaced" (char (first specific) 0) #\Z)
52 (check (format nil "sets:~A:input-output-strings-and-conses-owned" kind)
53 (equal (list saved) (item-identifiers item))))
54 (add-item-identifier item saved)
55 (check (format nil "sets:~A:idempotent" kind) (equal (list saved) (funcall reader item)))))
56 (dolist (bad '(nil 42 "" "relative"))
57 (let* ((before (map-state map)) (c (condition-from (lambda () (add-item-identifier item bad)))))
58 (check (format nil "sets:~A:invalid:~S:atomic" kind bad)
59 (and (typep c 'item-identifier-error) (eq :invalid-item-identifiers (item-identifier-error-code c))
60 (equal before (map-state map)))))))
61 (check "sets:all-seven-items-global-uniqueness" (topic-maps.tmdm::validate-map-item-identifiers map)))
62 ;; Every directed cross-kind collision, including the Topic Map itself.
63 (dotimes (from 7)
64 (dotimes (to 7)
65 (unless (= from to)
66 (multiple-value-bind (map items) (fixture)
67 (add-item-identifier (nth from items) "urn:collision")
68 (rejects-without-mutation (format nil "collision:~A:~A" (nth from *kinds*) (nth to *kinds*)) map
69 (lambda () (add-item-identifier (nth to items) "urn:collision")) items)))))
70 (loop for index from 1 below 7 do
71 (multiple-value-bind (map one) (fixture)
72 (multiple-value-bind (same-map two) (fixture map "other-")
73 (declare (ignore same-map))
74 (add-item-identifier (nth index one) "urn:same-kind")
75 (if (= index 1)
76 (let ((merged (add-item-identifier (nth index two) "urn:same-kind")))
77 (check "collision:two-topics-merge-not-reject"
78 (and (eq merged (canonical-topic (nth index one)))
79 (member "urn:owner" (topic-subject-identifiers merged) :test #'string=)
80 (member "urn:other-owner" (topic-subject-identifiers merged) :test #'string=))))
81 (rejects-without-mutation (format nil "collision:unequal-~A" (nth index *kinds*)) map
82 (lambda () (add-item-identifier (nth index two) "urn:same-kind")) (append one two))))))
83 ;; Existing constructor/Topic mutation routes must share the global check.
84 (dotimes (index 7)
85 (multiple-value-bind (map items type theme) (fixture)
86 (unless (= index 1)
87 (add-item-identifier (nth index items) "urn:collision")
88 (rejects-without-mutation (format nil "legacy:intern-topic:~D" index) map
89 (lambda () (intern-topic map :item-identifier "urn:collision")))
90 (rejects-without-mutation (format nil "legacy:add-topic-identifier:~D" index) map
91 (lambda () (add-topic-identifier (second items) :item-identifier "urn:collision"))))
92 (when (= index 1) (add-item-identifier (second items) "urn:collision"))
93 (rejects-without-mutation (format nil "legacy:add-occurrence:~D" index) map
94 (lambda () (add-occurrence (second items) type "Different" :scope (list theme)
95 :item-identifiers '("urn:collision"))))))
96 (let* ((map (make-topic-map)) (owner (si map "owner")) (type (si map "type"))
97 (input (list (copy-seq "urn:owned")))
98 (o (add-occurrence owner type "x" :item-identifiers input)))
99 (setf (char (first input) 0) #\X (car input) "replaced")
100 (check "legacy:occurrence-input-strings-and-list-owned" (equal '("urn:owned") (item-identifiers o)))
101 (let ((same (add-occurrence owner type "x" :item-identifiers '("urn:owned" "urn:other"))))
102 (check "legacy:equal-occurrence-can-reuse-and-union-identifiers"
103 (and (eq same (canonical-occurrence o)) (= 2 (length (item-identifiers o))))))))
104
105 (defun merge-tests ()
106 (let* ((map (make-topic-map)) (a (si map "a")) (b (si map "b")) (type (si map "type"))
107 (theme (si map "theme")) (extra (si map "extra"))
108 (n1 (add-name a "N" :type type :scope (list theme))) (n2 (add-name b "N" :type type :scope (list theme)))
109 (v1 (add-variant n1 "V" :scope (list theme extra))) (v2 (add-variant n2 "V" :scope (list extra theme)))
110 (o1 (add-occurrence a type "O")) (o2 (add-occurrence b type "O"))
111 (a1 (make-association map type (list (list type a)))) (a2 (make-association map type (list (list type b))))
112 (r1 (first (association-roles a1))) (r2 (first (association-roles a2)))
113 (one (list a n1 v1 o1 a1 r1)) (two (list b n2 v2 o2 a2 r2)))
114 (loop for x in one for y in two for index from 1 do
115 (add-item-identifier x (format nil "urn:one:~D" index))
116 (add-item-identifier y (format nil "urn:two:~D" index)))
117 (let ((c (add-item-identifier b "urn:one:1")))
118 (loop for x in one for y in two for index from 1 do
119 (check (format nil "merge:kind-~D:identifier-union-and-both-handles" index)
120 (and (eq (current x) (current y))
121 (equal (sort (item-identifiers x) #'string<)
122 (list (format nil "urn:one:~D" index) (format nil "urn:two:~D" index)))))
123 (add-item-identifier x (format nil "urn:via-old-handle:~D" index))
124 (check (format nil "merge:kind-~D:old-handle-addition" index)
125 (member (format nil "urn:via-old-handle:~D" index) (item-identifiers y) :test #'string=)))
126 (let ((again (add-name c "N" :type type :scope (list theme))))
127 (add-item-identifier again "urn:existing-name")
128 (check "merge:existing-equal-name-keeps-identifiers"
129 (and (eq again (canonical-topic-name n1)) (= 4 (length (item-identifiers n2))))))
130 (check "merge:stored-parent-type-player-and-variant-scope"
131 (and (eq c (topic-maps.tmdm::%topic-name-parent (current n1)))
132 (eq (current n1) (topic-maps.tmdm::%variant-parent (current v1)))
133 (eq c (topic-maps.tmdm::%occurrence-parent (current o1)))
134 (eq (current a1) (topic-maps.tmdm::%association-role-parent (current r1)))
135 (eq c (topic-maps.tmdm::%association-role-player (current r2)))
136 (topic-maps.reference.tests::references-current-p map)))
137 (let ((before (map-state map)) (handles (raw-handles (append one two))))
138 (check "merge:canonicalization-including-all-retained-handles-is-pure"
139 (and (string= (cxtm map) (cxtm map)) (equal before (map-state map))
140 (equal handles (raw-handles (append one two))))))))
141 ;; Roles also merge inside a single Association, independently of Assoc merges.
142 (let* ((map (make-topic-map)) (type (si map "type")) (a (si map "a")) (b (si map "b"))
143 (p (si map "p")) (rel (make-association map type (list (list a p) (list b p))))
144 (r1 (first (association-roles rel))) (r2 (second (association-roles rel))))
145 (add-item-identifier r1 "urn:role:1") (add-item-identifier r2 "urn:role:2")
146 (merge-topics a b)
147 (check "merge:role-type-merge-preserves-both-identifiers-and-handles"
148 (and (= 1 (length (association-roles rel))) (eq (current r1) (current r2))
149 (= 2 (length (item-identifiers r1))) (topic-maps.reference.tests::references-current-p map))))
150 (dolist (kind '(topic-name occurrence association))
151 (let* ((map (make-topic-map)) (owner (si map "owner")) (type (si map "type"))
152 (a (si map "a")) (b (si map "b")))
153 (labels ((new (scope)
154 (ecase kind
155 (topic-name (add-name owner "N" :type type :scope scope))
156 (occurrence (add-occurrence owner type "O" :scope scope))
157 (association (make-association map type (list (list type owner)) :scope scope)))))
158 (let ((one (new (list a))) (two (new (list b))))
159 (add-item-identifier one "urn:scope:1") (add-item-identifier two "urn:scope:2")
160 (merge-topics a b)
161 (check (format nil "merge:theme:~A:identifiers-and-handles" kind)
162 (and (eq (current one) (current two)) (= 2 (length (item-identifiers one)))
163 (topic-maps.reference.tests::references-current-p map)))))))
164 (let* ((map (make-topic-map)) (owner (si map "owner")) (a (si map "a")) (b (si map "b"))
165 (n (add-name owner "N" :scope (list a))) (v (add-variant n "V" :scope (list a b))))
166 (add-item-identifier a "urn:theme") (add-item-identifier n "urn:name") (add-item-identifier v "urn:variant")
167 (let* ((before (map-state map)) (old (raw-handles (list a b n v)))
168 (e (condition-from (lambda () (add-item-identifier b "urn:theme")))))
169 (check "merge:identifier-triggered-variant-collapse-fully-atomic"
170 (and (typep e 'variant-error) (eq :variant-scope-collapse (variant-error-code e))
171 (equal before (map-state map)) (equal old (raw-handles (list a b n v))))))))
172
173 (defun boundary-tests ()
174 (loop for index from 0 below 7 do
175 (multiple-value-bind (map one) (fixture)
176 (multiple-value-bind (other two) (fixture)
177 (let ((iri (format nil "urn:independent:~D" index)))
178 (add-item-identifier (nth index one) iri) (add-item-identifier (nth index two) iri)
179 (check (format nil "boundary:same-locator-in-separate-maps:~D" index)
180 (and (not (eq map other)) (not (eq (current (nth index one)) (current (nth index two))))
181 (topic-maps.tmdm::validate-map-item-identifiers map)
182 (topic-maps.tmdm::validate-map-item-identifiers other)))))))
183 (multiple-value-bind (map items type) (fixture)
184 (let ((o (fifth items)))
185 (add-item-identifier o "urn:occurrence-boundary")
186 (let ((subject (intern-topic map :subject-identifier "urn:occurrence-boundary")))
187 (check "boundary:occurrence-II-then-Topic-SI-no-automatic-reification"
188 (and (null (item-reifier o)) (null (topic-reified subject)))))
189 (add-item-identifier o "urn:type")
190 (check "boundary:Topic-SI-then-occurrence-II-no-automatic-reification"
191 (and (member "urn:type" (item-identifiers o) :test #'string=)
192 (null (item-reifier o)) (null (topic-reified type))))
193 (add-item-identifier (third items) "urn:theme")
194 (check "boundary:no-invented-reification-on-other-item-kinds"
195 (and (equal '("urn:theme") (item-identifiers (third items))) (null (item-identifiers type))))))
196 (let* ((map (make-topic-map)) (owner (si map "owner")) (type (si map "type"))
197 (n1 (add-name owner "N1" :type type)) (n2 (add-name owner "N2" :type type))
198 (decomposed (format nil "urn:e~A" (code-char #x301))) (composed "urn:é"))
199 (add-item-identifier n1 decomposed) (add-item-identifier n2 composed)
200 (let ((before (map-state map)))
201 (check "locators:TMDM-equality-is-not-CXTM-NFC-equality"
202 (and (not (string= decomposed composed)) (= 2 (length (topic-names owner)))
203 (search composed (cxtm map)) (equal before (map-state map)))))))
204
205 (defun serialization-tests ()
206 (let ((expected nil))
207 (dolist (reverse '(nil t))
208 (multiple-value-bind (map items) (fixture)
209 (loop for item in items for kind in *kinds* do
210 (dolist (suffix (if reverse '("a" "z") '("z" "a")))
211 (add-item-identifier item (format nil "urn:~A:~A" kind suffix))))
212 (let ((before (map-state map)) (actual (cxtm map)))
213 (unless expected (setf expected actual))
214 (check (format nil "serialization:identifier-order:~A:deterministic-pure" reverse)
215 (and (string= expected actual) (string= actual (cxtm map)) (equal before (map-state map)))))))
216 (setf *ordering-output* expected))
217 (let ((expected nil))
218 (dolist (reverse '(nil t))
219 (let* ((map (make-topic-map)) (type (si map "type")) (theme (si map "theme")))
220 (add-item-identifier map "urn:map")
221 (dolist (label (if reverse '("b" "a") '("a" "b")))
222 (let* ((owner (si map label)) (n (add-name owner "N" :type type))
223 (v (add-variant n "V" :scope (list theme))) (o (add-occurrence owner type "O"))
224 (a (make-association map type (list (list type owner)))) (r (first (association-roles a))))
225 (loop for item in (list owner n v o a r) for index from 1 do
226 (add-item-identifier item (format nil "urn:item:~A:~D" label index)))))
227 (let ((before (map-state map)) (actual (cxtm map)))
228 (unless expected (setf expected actual))
229 (check (format nil "serialization:construction-order:~A:pure-and-identical" reverse)
230 (and (string= expected actual) (equal before (map-state map))))))))
231 (multiple-value-bind (map items) (fixture)
232 ;; Deliberately corrupt private storage to exercise canonicalizer validation.
233 (setf (topic-maps.tmdm::%topic-map-item-identifiers map) '("urn:collision")
234 (topic-maps.tmdm::%topic-name-item-identifiers (third items)) '("urn:collision"))
235 (let* ((before (map-state map)) (e (condition-from (lambda () (cxtm map)))))
236 (check "serialization:global-collision-rejected-without-repair"
237 (and (typep e 'topic-maps.cxtm:cxtm-error) (eq :duplicate-item-identifier (topic-maps.cxtm:error-code e))
238 (equal before (map-state map)))))))
239
240 (defun manual-original-models ()
241 (setf *examples* nil)
242 (dolist (stem '("itemid-tm" "itemid" "itemid-name" "itemid-variant" "itemid-occurrence" "itemid-association" "itemid-role"))
243 (let* ((map (make-topic-map)) (base "https://example.invalid/xtm/model.xtm")
244 (id (concatenate 'string "xtm2/in/" stem ".xtm"))
245 (baseline (concatenate 'string "xtm2/baseline/" stem ".xtm.cxtm")))
246 ;; Hand-constructed from the original source. This is NOT an XTM reader.
247 (labels ((topic (local) (intern-topic map :item-identifier (concatenate 'string base "#" local)))
248 (identify (item local) (add-item-identifier item (concatenate 'string base "#" local))))
249 (cond ((string= stem "itemid-tm") (identify map "the-topic-map"))
250 ((string= stem "itemid") (add-item-identifier (topic "topic") "http://example.org/#topic"))
251 ((string= stem "itemid-name") (identify (add-name (topic "topic") "Topic") "the-name"))
252 ((string= stem "itemid-variant")
253 (identify (add-variant (add-name (topic "topic") "Topic") "TOPIC!" :scope (list (topic "command"))) "the-variant"))
254 ((string= stem "itemid-occurrence")
255 (identify (add-occurrence (topic "topic") (topic "occtype") "Testing resource data.") "the-occurrence"))
256 (t (let ((a (make-association map (topic "assoctype") (list (list (topic "roletype") (topic "topic"))))))
257 (if (string= stem "itemid-role") (identify (first (association-roles a)) "the-role")
258 (identify a "the-association"))))))
259 (let* ((before (map-state map)) (actual (topic-maps.cxtm:canonicalize-octets map :base-locator base))
260 (expected (binary-file (asdf:system-relative-pathname "topic-maps" (concatenate 'string "vendor/cxtm/" baseline)))))
261 (check (concatenate 'string "manual-model:" stem ":original-CXTM-byte-exact") (equalp actual expected))
262 (check (concatenate 'string "manual-model:" stem ":mutation-free") (equal before (map-state map)))
263 (push (topic-maps.test-foundation::fields "source" id "baseline" baseline "baseIRI" base
264 "contract" "Manual TMDM construction; NOT XTM deserialization"
265 "actualCXTM" (topic-maps.cxtm:canonicalize map :base-locator base)
266 "bytes" (length actual)) *examples*))))
267 (setf *examples* (nreverse *examples*)))
268
269 (defun run-tests ()
270 (topic-maps.reference.tests:run-tests)
271 (let ((topic-maps.ctm.tests::*checks* nil))
272 (set-tests) (merge-tests) (boundary-tests) (serialization-tests) (manual-original-models)
273 (check "identifiers:no-imported-contract-status-changes"
274 (equal '(:pass 93 :fail 2 :unsupported 0 :not-run 178)
275 (getf (topic-maps.test-foundation:summarize-corpus topic-maps.ctm.tests::*cxtm-corpus*) :statuses)))
276 (check "identifiers:reification-now-supported"
277 (null (topic-maps.ctm.tests::ctm-condition "a - \"N\" ~r.")))
278 (setf *last-checks* (nreverse topic-maps.ctm.tests::*checks*))
279 (format t "~&ITEM-IDENTIFIERS-PASS: ~D checks; 7 manually constructed original XTM2 baseline comparisons; no XTM reader or imported status changes.~%"
280 (length *last-checks*)) t))
281 (defun write-identifier-report (path)
282 (with-open-file (out path :direction :output :if-exists :supersede :if-does-not-exist :create)
283 (topic-maps.test-foundation::emit-json-value
284 (topic-maps.test-foundation::fields "kind" "general-item-identifier-evidence" "passed" (length *last-checks*) "failed" 0
285 "checks" (topic-maps.test-foundation::json-array *last-checks*)
286 "examples" (topic-maps.test-foundation::json-array *examples*) "orderingCXTM" *ordering-output*) out)
287 (terpri out)))