]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/map-import.lisp
Implement CTM Mergemap with explicit byte resources and isolated environments
[topic-maps.git] / tests / map-import.lisp
1 (defpackage #:topic-maps.import.tests
2 (:use #:cl #:topic-maps.tmdm)
3 (:import-from #:topic-maps.ctm.tests #:check #:cxtm #:map-state)
4 (:import-from #:topic-maps.variant.tests #:condition-from)
5 (:import-from #:topic-maps.identifier.tests #:si #:current)
6 (:export #:run-tests #:import-checks #:write-import-report))
7 (in-package #:topic-maps.import.tests)
8 (defvar *last-checks* nil)
9 (defvar *witnesses* nil)
10 (defvar *rejections* nil)
11 (defun import-checks () *last-checks*)
12 (defun handles (map) (copy-list (topic-maps.tmdm::map-information-items map)))
13 (defun freeze (value)
14 (typecase value (string (copy-seq value)) (cons (cons (freeze (car value)) (freeze (cdr value)))) (t value)))
15 (defun raw (i)
16 ;; Raw slots of held replacements as well as live items: public readers alone
17 ;; could conceal a modified forwarding link or a noncanonical stored reference.
18 (freeze
19 (list i (topic-maps.tmdm::%item-identifiers i)
20 (when (topic-maps.tmdm::reifiable-p i) (topic-maps.tmdm::%item-reifier i))
21 (etypecase i
22 (topic-map (list (topic-maps.tmdm::%topic-map-topics i) (topic-maps.tmdm::%topic-map-associations i)))
23 (topic (list (topic-maps.tmdm::%topic-parent i) (topic-maps.tmdm::%topic-forward i)
24 (topic-maps.tmdm::%topic-subject-identifiers i) (topic-maps.tmdm::%topic-subject-locators i)
25 (topic-maps.tmdm::%topic-names i) (topic-maps.tmdm::%topic-occurrences i)))
26 (topic-name (list (topic-maps.tmdm::%topic-name-parent i) (topic-maps.tmdm::%topic-name-type i)
27 (topic-maps.tmdm::%topic-name-value i) (topic-maps.tmdm::%topic-name-scope i)
28 (topic-maps.tmdm::%topic-name-variants i) (topic-maps.tmdm::%topic-name-forward i)))
29 (variant (list (topic-maps.tmdm::%variant-parent i) (topic-maps.tmdm::%variant-value i)
30 (topic-maps.tmdm::%variant-datatype i) (topic-maps.tmdm::%variant-scope i) (topic-maps.tmdm::%variant-forward i)))
31 (occurrence (list (topic-maps.tmdm::%occurrence-parent i) (topic-maps.tmdm::%occurrence-type i)
32 (topic-maps.tmdm::%occurrence-value i) (topic-maps.tmdm::%occurrence-datatype i)
33 (topic-maps.tmdm::%occurrence-scope i) (topic-maps.tmdm::%occurrence-forward i)))
34 (association (list (topic-maps.tmdm::%association-parent i) (topic-maps.tmdm::%association-type i)
35 (topic-maps.tmdm::%association-scope i) (topic-maps.tmdm::%association-roles i)
36 (topic-maps.tmdm::%association-forward i)))
37 (association-role (list (topic-maps.tmdm::%association-role-parent i) (topic-maps.tmdm::%association-role-type i)
38 (topic-maps.tmdm::%association-role-player i) (topic-maps.tmdm::%association-role-forward i)))))))
39 (defun state (map held) (mapcar #'raw (remove-duplicates (append (handles map) held) :test #'eq)))
40 (defun valid (map)
41 (and (topic-maps.reference.tests::references-current-p map)
42 (topic-maps.tmdm::validate-import-map map)))
43 (defun fixture (tag &key reverse (identity-prefix ""))
44 (let* ((map (make-topic-map)) (ids '("owner" "type" "theme" "extra")) (table (make-hash-table :test #'equal)))
45 (dolist (id (if reverse (reverse ids) ids))
46 (setf (gethash id table) (si map (concatenate 'string identity-prefix id))))
47 (let* ((owner (gethash "owner" table)) (type (gethash "type" table))
48 (theme (gethash "theme" table)) (extra (gethash "extra" table))
49 (name (add-name owner (copy-seq "Name") :type type :scope (list theme)))
50 (variant (add-variant name (copy-seq "Variant") :scope (list theme extra)))
51 (occ (add-occurrence owner type (copy-seq "+0007") :datatype (integer-datatype) :scope (list theme)))
52 (a (make-association map type (if reverse (list (list type theme) (list type owner))
53 (list (list type owner) (list type theme))) :scope (list theme)))
54 (role (find owner (association-roles a) :key #'association-role-player :test #'eq))
55 (other (find theme (association-roles a) :key #'association-role-player :test #'eq))
56 (items (list map owner name variant occ a role other)))
57 (loop for item in items for label in '("map" "owner" "name" "variant" "occurrence" "association" "role" "other-role") do
58 (add-item-identifier item (format nil "urn:~A:~A" tag label))
59 (when (topic-maps.tmdm::reifiable-p item)
60 (set-item-reifier item (si map (format nil "~A:~A:reifier" tag label)))))
61 (values map items type theme extra))))
62 (defun preserve-source (label source held before bytes)
63 (check (concatenate 'string label ":source-raw-slots-and-held-handles-unchanged") (equal before (state source held)))
64 (check (concatenate 'string label ":source-byte-identical-CXTM") (string= bytes (cxtm source))))
65 (defun reject-import (label target source class code &key target-handles source-handles)
66 (let* ((th (append (handles target) target-handles)) (sh (append (handles source) source-handles)) (tb (state target th)) (sb (state source sh))
67 (tx (cxtm target)) (sx (cxtm source)) (c (condition-from (lambda () (import-map-content target source)))))
68 (check (concatenate 'string label ":specific-condition")
69 (and (typep c class)
70 (eq code (typecase c
71 (core-subject-error (core-subject-error-code c))
72 (reification-error (reification-error-code c))
73 (item-identifier-error (item-identifier-error-code c))
74 (variant-error (variant-error-code c))
75 (map-import-error (map-import-error-code c))))))
76 (check (concatenate 'string label ":both-graphs-and-all-held-handles-unchanged")
77 (and (equal tb (state target th)) (equal sb (state source sh))))
78 (check (concatenate 'string label ":both-CXTM-documents-unchanged") (and (string= tx (cxtm target)) (string= sx (cxtm source))))
79 (push (topic-maps.test-foundation::fields "label" label "class" (symbol-name (type-of c))
80 "code" (symbol-name code) "bothGraphsUnchanged" :true "bothCXTMUnchanged" :true) *rejections*)))
81 (defun transfer-tests ()
82 (multiple-value-bind (source items type theme extra) (fixture "source")
83 (let* ((target (make-topic-map)) (held (handles source)) (before (state source held)) (bytes (cxtm source))
84 (root-r (si target "target-map-reifier")))
85 (add-item-identifier target "urn:target-map") (set-item-reifier target root-r)
86 (multiple-value-bind (result resolve) (import-map-content target source)
87 (check "transfer:return-values-and-target-map-identity" (and (eq result target) (functionp resolve)))
88 (check "transfer:source-map-metadata-excluded-target-map-metadata-retained"
89 (and (equal '("urn:target-map") (item-identifiers target)) (eq root-r (item-reifier target))
90 (eq target (topic-reified root-r))
91 (null (topic-reified (funcall resolve (item-reifier source))))))
92 (loop for item in (rest held) for i from 1 for new = (funcall resolve item) do
93 (check (format nil "transfer:~D:copied-actual-item-identity" i)
94 (and (not (eq item new)) (eq (type-of item) (type-of new))
95 (eq target (topic-maps.tmdm::item-topic-map new))
96 (equal (item-identifiers item) (item-identifiers new))
97 (eq item (current item)) (eq new (current new))))
98 (when (topic-maps.tmdm::reifiable-p item)
99 (check (format nil "transfer:~D:Reifier-and-Reified-EQ" i)
100 (and (eq (funcall resolve (item-reifier item)) (item-reifier new)) (eq new (topic-reified (item-reifier new)))))))
101 (destructuring-bind (m owner name variant occ a role other) items
102 (declare (ignore m))
103 (let ((n (funcall resolve name)) (v (funcall resolve variant)) (o (funcall resolve occ))
104 (as (funcall resolve a)) (r (funcall resolve role)) (p (funcall resolve owner)) (ty (funcall resolve type)))
105 (check "transfer:Name-Parent-Type-and-Scope-EQ"
106 (and (eq p (topic-name-parent n)) (eq ty (topic-name-type n))
107 (eq (funcall resolve theme) (first (topic-name-scope n))) (eq n (first (topic-names p)))))
108 (check "transfer:Variant-Parent-Scope-and-stored-lexical-value"
109 (and (eq n (variant-parent v)) (string= "Variant" (variant-value v))
110 (topic-maps.tmdm::same-scope-p (list (funcall resolve theme) (funcall resolve extra)) (variant-scope v))))
111 (check "transfer:Occurrence-Parent-Type-Value-Datatype-EQ"
112 (and (eq p (occurrence-parent o)) (eq ty (occurrence-type o))
113 (string= "+0007" (occurrence-value o)) (string= (integer-datatype) (occurrence-datatype o))
114 (eq (funcall resolve theme) (first (occurrence-scope o)))))
115 (check "transfer:Association-Role-Parent-Type-Player-Scope-EQ"
116 (and (eq target (association-parent as)) (eq ty (association-type as))
117 (eq as (association-role-parent r)) (eq ty (association-role-type r)) (eq p (association-role-player r))
118 (eq (funcall resolve theme) (first (association-scope as)))
119 (member (funcall resolve other) (association-roles as) :test #'eq)
120 (member r (topic-roles-played p) :test #'eq)))
121 (check "transfer:no-source-objects-in-target-graph" (and (valid target) (null (intersection held (handles target) :test #'eq))))
122 (let ((snapshot (state target (handles target))) (xml (cxtm target)))
123 (check "transfer:deterministic-mutation-free-canonicalization"
124 (and (string= xml (cxtm target)) (equal snapshot (state target (handles target)))))
125 (push (topic-maps.test-foundation::fields "label" "full-six-kind-transfer" "sourceUnchanged" :true
126 "parentTypePlayerScopeReifierEQ" :true "sourceCXTM" bytes "targetCXTM" (cxtm target)) *witnesses*)
127 (preserve-source "transfer" source held before bytes)
128 (loop for bad in (list source target root-r nil 42) for index from 1 do
129 (let ((c (condition-from (lambda () (funcall resolve bad)))))
130 (check (format nil "resolver:not-imported:~D" index)
131 (and (typep c 'map-import-error) (eq :not-imported (map-import-error-code c))))))
132 ;; Known imported objects resolve dynamically after later target merges.
133 (let* ((later (si target "later")) (merged (merge-topics p later)))
134 (check "resolver:later-target-merge-current-canonical-object" (eq merged (funcall resolve owner))))
135 ;; Newly copied strings are independent even of mutable source values.
136 (setf (char (topic-name-value n) 0) #\X (char (occurrence-value o) 0) #\0 (char (variant-value v) 0) #\X)
137 (preserve-source "transfer:mutable-target-strings" source held before bytes))))))
138 (let* ((target (make-topic-map)) (source (make-topic-map)))
139 (add-item-identifier source "urn:source-only-map") (set-item-reifier source (si source "map-only-reifier"))
140 (import-map-content target source)
141 (check "transfer:map-only-source-reifier-is-an-ordinary-imported-Topic"
142 (and (= 1 (length (topic-map-topics target))) (null (item-identifiers target)) (null (item-reifier target))
143 (null (topic-reified (first (topic-map-topics target)))))))
144 (let ((target (make-topic-map)) (source (make-topic-map)))
145 (import-map-content target source)
146 (check "transfer:empty-graphs" (and (null (topic-map-topics target)) (null (topic-map-associations target))))))
147 (defun identity-tests ()
148 (loop for pair in '((:subject-identifier :subject-identifier) (:item-identifier :item-identifier)
149 (:subject-identifier :item-identifier) (:item-identifier :subject-identifier) (:subject-locator :subject-locator))
150 for i from 1 do
151 (let* ((target (make-topic-map)) (source (make-topic-map)) (a (intern-topic target (first pair) "urn:identity"))
152 (b (intern-topic source (second pair) "urn:identity")))
153 (multiple-value-bind (map resolve) (import-map-content target source)
154 (check (format nil "identity:~D:canonical-object-EQ" i)
155 (and (eq target map) (= 1 (length (topic-map-topics map))) (eq (canonical-topic a) (funcall resolve b))
156 (not (eq a (canonical-topic a))) (eq b (canonical-topic b)) (valid map))))))
157 (dolist (pair '((:subject-identifier :subject-locator) (:item-identifier :subject-locator)))
158 (let* ((target (make-topic-map)) (source (make-topic-map)) (a (intern-topic target (first pair) "urn:identity"))
159 (b (intern-topic source (second pair) "urn:identity")))
160 (multiple-value-bind (map resolve) (import-map-content target source)
161 (check (format nil "identity:distinct-properties:~A" (first pair))
162 (and (= 2 (length (topic-map-topics map))) (not (eq a (funcall resolve b))))))))
163 (let* ((target (make-topic-map)) (source (make-topic-map))
164 (a (intern-topic target :item-identifier "https://example.invalid/a.ctm#same"))
165 (b (intern-topic source :item-identifier "https://example.invalid/b.ctm#same")))
166 (multiple-value-bind (map resolve) (import-map-content target source)
167 (check "identity:local-fragments-with-different-Document-IRIs-remain-distinct"
168 (and (= 2 (length (topic-map-topics map))) (not (eq a (funcall resolve b)))))))
169 (let* ((target (make-topic-map)) (source (make-topic-map))
170 (a (intern-topic target :subject-identifier (concatenate 'string "urn:" (string (code-char #xE9)))))
171 (b (intern-topic source :subject-identifier (concatenate 'string "urn:e" (string (code-char #x301))))))
172 (multiple-value-bind (map resolve) (import-map-content target source)
173 (check "identity:locator-equality-is-not-CXTM-NFC-equality"
174 (and (= 2 (length (topic-map-topics map))) (not (eq a (funcall resolve b))))))))
175 (defun merge-tests ()
176 (multiple-value-bind (source sh) (fixture "s")
177 (multiple-value-bind (target th) (fixture "t" :reverse t)
178 (let ((before (state source sh)) (sx (cxtm source)))
179 (multiple-value-bind (map resolve) (import-map-content target source)
180 (declare (ignore map))
181 (loop for s in (rest sh) for old in (rest th) for i from 1 for new = (funcall resolve s) do
182 (check (format nil "merge:~D:all-six-kinds-and-held-target-handles" i)
183 (and (eq new (current old)) (not (eq old new))
184 (= 2 (length (item-identifiers new))) (eq s (current s))))
185 (when (topic-maps.tmdm::reifiable-p s)
186 (check (format nil "merge:~D:merged-Reifier-Topics-and-inverse-EQ" i)
187 (and (eq (item-reifier new) (canonical-topic (item-reifier old)))
188 (eq (item-reifier new) (funcall resolve (item-reifier s))) (eq new (topic-reified (item-reifier new)))))))
189 (check "merge:fixed-point-and-reference-integrity" (valid target))
190 (preserve-source "merge" source sh before sx)
191 (let ((counts (length (handles target))) (xml (cxtm target)))
192 (multiple-value-bind (same resolve2) (import-map-content target source)
193 (check "repeat:identifiers-and-structural-equality-justify-no-additional-items"
194 (and (eq target same) (= counts (length (handles same))) (string= xml (cxtm same))))
195 (check "repeat:old-and-new-resolvers-follow-replacements"
196 (every (lambda (s) (eq (funcall resolve s) (funcall resolve2 s))) (rest (handles source))))))
197 (push (topic-maps.test-foundation::fields "label" "six-kind-merge-and-repeat" "sourceUnchanged" :true
198 "retainedTargetHandlesCanonical" :true "targetCXTM" (cxtm target)) *witnesses*))))))
199 ;; One target Topic bridges two distinct source Topics, collapsing parent,
200 ;; Type/Player/Scope references, Names, Variants, Occurrences and Roles.
201 (let* ((source (make-topic-map)) (target (make-topic-map))
202 (a (si source "a")) (b (si source "b")) (type (si source "type")) (theme (si source "theme"))
203 (na (add-name a "N" :type type)) (nb (add-name b "N" :type type))
204 (va (add-variant na "V" :scope (list theme))) (vb (add-variant nb "V" :scope (list theme)))
205 (oa (add-occurrence a type "O")) (ob (add-occurrence b type "O"))
206 (as (make-association source type (list (list type a) (list type b)))) (roles (association-roles as))
207 (bridge (si target "a")))
208 (add-topic-identifier bridge :subject-identifier "urn:b")
209 (multiple-value-bind (map resolve) (import-map-content target source)
210 (check "many-to-one:Topics-Names-Variants-Occurrences-Roles"
211 (every (lambda (pair) (eq (funcall resolve (first pair)) (funcall resolve (second pair))))
212 (list (list a b) (list na nb) (list va vb) (list oa ob) roles)))
213 (check "many-to-one:Role-set-cardinality-and-invariants"
214 (and (= 1 (length (association-roles (funcall resolve as)))) (valid map)))))
215 ;; An equal Name merges its Reifiers, which are themselves the Types and
216 ;; Players of further items. This must continue until the model is stable.
217 (let* ((target (make-topic-map)) (source (make-topic-map)) (a (si target "owner")) (b (si source "owner"))
218 (ty (si target "name-type")) (sy (si source "name-type")) (r (si target "r")) (s (si source "s"))
219 (n (add-name a "N" :type ty)) (m (add-name b "N" :type sy))
220 (o (add-occurrence r r "O" :scope (list r))) (p (add-occurrence s s "O" :scope (list s)))
221 (as (make-association target r (list (list r r)) :scope (list r)))
222 (bs (make-association source s (list (list s s)) :scope (list s))))
223 (set-item-reifier n r) (set-item-reifier m s)
224 (multiple-value-bind (map resolve) (import-map-content target source)
225 (check "cascade:Name-Reifiers-become-same-Topic-without-locator-identity"
226 (and (eq (canonical-topic r) (funcall resolve s)) (eq (canonical-topic-name n) (funcall resolve m))))
227 (check "cascade:subsequent-parent-type-player-scope-and-item-merging"
228 (and (eq (canonical-occurrence o) (funcall resolve p)) (eq (canonical-association as) (funcall resolve bs)) (valid map))))))
229 (defun conflict-tests ()
230 ;; Six imported item kinds versus all seven target kinds. Only Topic/Topic
231 ;; identifier overlap may establish identity; no non-Topic II merge shortcut.
232 (loop for from from 1 to 6 do
233 (dotimes (to 7)
234 (multiple-value-bind (target ti) (topic-maps.identifier.tests::fixture)
235 (multiple-value-bind (source si) (topic-maps.identifier.tests::fixture (make-topic-map) "source-")
236 (add-item-identifier (nth from si) "urn:collision") (add-item-identifier (nth to ti) "urn:collision")
237 (if (and (= from 1) (= to 1))
238 (multiple-value-bind (map resolve) (import-map-content target source)
239 (check "collision:Topic-Topic-is-a-merge"
240 (and (eq (current (second ti)) (funcall resolve (second si))) (valid map))))
241 (reject-import (format nil "collision:source-~D-target-~D" from to) target source
242 'item-identifier-error :duplicate-item-identifier))))))
243 ;; Equal structural items may share an identifier because they are merged.
244 (multiple-value-bind (source sh) (topic-maps.identifier.tests::fixture)
245 (multiple-value-bind (target th) (topic-maps.identifier.tests::fixture)
246 (loop for s in (rest sh) for tpc in (rest th) for i from 1 do
247 (add-item-identifier s (format nil "urn:shared:~D" i)) (add-item-identifier tpc (format nil "urn:shared:~D" i)))
248 (multiple-value-bind (map resolve) (import-map-content target source)
249 (check "collision:equal-structural-items-merge-before-uniqueness-validation"
250 (and (valid map) (every (lambda (s old) (eq (funcall resolve s) (current old))) (rest sh) (rest th)))))))
251 (let* ((target (make-topic-map)) (source (make-topic-map)) (iri "urn:root-identifier"))
252 (add-item-identifier source iri) (add-item-identifier (si target "target") iri)
253 (import-map-content target source)
254 (check "collision:excluded-source-root-identifiers-do-not-collide" (= 1 (length (topic-map-topics target)))))
255 (let* ((target (make-topic-map)) (source (make-topic-map))
256 (a (intern-topic target :subject-identifier (first (core-subject-identifiers))))
257 (b (intern-topic source :subject-identifier (second (core-subject-identifiers)))))
258 (add-item-identifier a "urn:core:bridge") (add-item-identifier b "urn:core:bridge")
259 (reject-import "conflict:Core-Subjects" target source 'core-subject-error :core-subject-conflict))
260 (let* ((target (make-topic-map)) (source (make-topic-map))
261 (a (intern-topic target :subject-identifier (first (core-subject-identifiers))))
262 (b (intern-topic target :subject-identifier (second (core-subject-identifiers)))) (bridge (si source "bridge")))
263 (add-item-identifier a "urn:a") (add-item-identifier b "urn:b")
264 (add-topic-identifier bridge :subject-identifier "urn:a") (add-topic-identifier bridge :subject-identifier "urn:b")
265 (reject-import "conflict:transitive-Core-Subjects" target source 'core-subject-error :core-subject-conflict))
266 (let* ((target (make-topic-map)) (source (make-topic-map)) (a (si target "owner")) (b (si source "owner"))
267 (ty (si target "type")) (sy (si source "type")) (r (si target "r")) (s (si source "r")))
268 (set-item-reifier (add-name a "Different" :type ty) r) (set-item-reifier (add-name b "Name" :type sy) s)
269 (reject-import "conflict:Reifier-reifies-different-items" target source 'reification-error :reifier-conflict))
270 (let* ((target (make-topic-map)) (source (make-topic-map)) (r (si target "r"))
271 (s (si source "r")) (owner (si source "owner")))
272 (set-item-reifier target r) (set-item-reifier (add-default-name owner "N") s)
273 (reject-import "conflict:retained-target-root-Reifier" target source 'reification-error :reifier-conflict))
274 (let* ((target (make-topic-map)) (source (make-topic-map)) (a (si source "a")) (b (si source "b"))
275 (owner (si source "owner")) (type (si source "type")) (n (add-name owner "N" :type type :scope (list a)))
276 (bridge (si target "a")))
277 (add-variant n "V" :scope (list a b)) (add-topic-identifier bridge :subject-identifier "urn:b")
278 (reject-import "conflict:Variant-scope-collapse" target source 'variant-error :variant-scope-collapse))
279 ;; A merge required solely by equal Names can itself discover a Core conflict.
280 (let* ((target (make-topic-map)) (source (make-topic-map)) (a (si target "owner")) (b (si source "owner"))
281 (ty (si target "type")) (sy (si source "type"))
282 (r (intern-topic target :subject-identifier (first (core-subject-identifiers))))
283 (s (intern-topic source :subject-identifier (second (core-subject-identifiers)))))
284 (set-item-reifier (add-name a "N" :type ty) r) (set-item-reifier (add-name b "N" :type sy) s)
285 (reject-import "conflict:Reifier-induced-Core-conflict" target source 'core-subject-error :core-subject-conflict))
286 (let ((map (make-topic-map))) (si map "owner")
287 (reject-import "conflict:self-import-policy" map map 'map-import-error :same-map)))
288 (defun reifier-order-test ()
289 ;; Reifier Topics precede owners/types in both maps. The reified Names become
290 ;; equal after their owner/type Topics merge; this is not a Reifier conflict.
291 (let* ((target (make-topic-map)) (source (make-topic-map))
292 (r (si target "r")) (s (si source "r"))
293 (a (si target "owner")) (b (si source "owner"))
294 (ty (si target "type")) (sy (si source "type"))
295 (n (add-name a "N" :type ty)) (m (add-name b "N" :type sy)))
296 (set-item-reifier n r) (set-item-reifier m s)
297 (multiple-value-bind (map resolve) (import-map-content target source)
298 (check "order:Reifier-first-does-not-reject-before-equal-items-can-merge"
299 (and (eq (canonical-topic r) (funcall resolve s))
300 (eq (canonical-topic-name n) (funcall resolve m)) (valid map))))))
301 (defun alias-tests ()
302 ;; Start with retained handles to all six replaced content kinds.
303 (multiple-value-bind (source old type theme) (topic-maps.identifier.tests::fixture)
304 (multiple-value-bind (same later type2 theme2) (topic-maps.identifier.tests::fixture source "later-")
305 (declare (ignore same))
306 (merge-topics type type2) (merge-topics theme theme2) (merge-topics (second old) (second later))
307 (let* ((target (make-topic-map)) (held (append old later)) (before (state source held)) (xml (cxtm source)))
308 (multiple-value-bind (map resolve) (import-map-content target source)
309 (loop for item in (rest old) for i from 1 do
310 (check (format nil "resolver:preexisting-source-alias:~D" i)
311 (eq (funcall resolve item) (funcall resolve (current item)))))
312 (preserve-source "resolver:preexisting-source-aliases" source held before xml)
313 (check "resolver:preexisting-source-aliases-valid-target" (valid map))
314 ;; Snapshot members survive later SOURCE forwarding too. New content
315 ;; does not become part of an old import through a hidden global list.
316 (let* ((original (current (second old))) (target-object (funcall resolve original))
317 (new (si source "new-after-import")) (merged (merge-topics original new))
318 (c (condition-from (lambda () (funcall resolve merged)))))
319 (check "resolver:snapshot-member-after-source-merge"
320 (and (eq target-object (funcall resolve original))
321 (typep c 'map-import-error) (eq :not-imported (map-import-error-code c))))))))))
322 (defun cascade-conflict-test ()
323 ;; Reifier-driven merging, without locator equality, can collapse a Variant
324 ;; scope. The rejection must happen on the disposable graph, not on originals.
325 (let* ((target (make-topic-map)) (source (make-topic-map))
326 (owner (si target "owner")) (other (si source "owner"))
327 (type (si target "type")) (stype (si source "type"))
328 (r (si target "r")) (s (si source "s"))
329 (name (add-name owner "merge" :type type)) (sname (add-name other "merge" :type stype))
330 (local-s (si target "s")) (vname (add-name owner "variant-parent" :type type :scope (list r))))
331 (add-variant vname "V" :scope (list r local-s))
332 (set-item-reifier name r) (set-item-reifier sname s)
333 (reject-import "conflict:Reifier-induced-transitive-Variant-collapse" target source 'variant-error :variant-scope-collapse)))
334
335 (defun boundary-tests ()
336 (let* ((source (make-topic-map)) (target (make-topic-map))
337 (a (si source "a")) (b (si source "b")) (c (si source "c"))
338 (owner (si source "owner")) (type (si source "type"))
339 (name (add-name owner "N" :type type :scope (list a b)))
340 (variant (add-variant name "V" :scope (list a b c))) (bridge (si target "a"))
341 (held (handles source)) (before (state source held)) (xml (cxtm source)))
342 (add-topic-identifier bridge :subject-identifier "urn:b")
343 (multiple-value-bind (map resolve) (import-map-content target source)
344 (let ((n (funcall resolve name)) (v (funcall resolve variant)))
345 (check "scope:import-reduces-cardinality-without-violating-Variant-superset"
346 (and (= 1 (length (topic-name-scope n))) (= 2 (length (variant-scope v)))
347 (eq (funcall resolve a) (funcall resolve b))
348 (eq (funcall resolve a) (first (topic-name-scope n)))
349 (topic-maps.tmdm::variant-scope-valid-p (variant-scope v) (topic-name-scope n)) (valid map)))
350 (preserve-source "scope:valid-cardinality-reduction" source held before xml))))
351 ;; Keep stale handles across a rejected transitive Core conflict, including
352 ;; raw forward links that canonical public readers would otherwise conceal.
353 (let* ((target (make-topic-map)) (source (make-topic-map))
354 (a (intern-topic target :subject-identifier (first (core-subject-identifiers))))
355 (b (intern-topic target :subject-identifier (second (core-subject-identifiers))))
356 (old-target (si target "old-target")) (bridge (si source "bridge")) (old-source (si source "old-source")))
357 (merge-topics a old-target) (merge-topics bridge old-source)
358 (add-item-identifier a "urn:a") (add-item-identifier b "urn:b")
359 (add-topic-identifier bridge :subject-identifier "urn:a") (add-topic-identifier bridge :subject-identifier "urn:b")
360 (reject-import "conflict:transitive-with-preexisting-stale-handles" target source 'core-subject-error :core-subject-conflict
361 :target-handles (list a old-target) :source-handles (list bridge old-source)))
362 (let* ((target (make-topic-map)) (source (make-topic-map)) (topic (si source "t"))
363 (th (handles target)) (sh (handles source)) (tb (state target th)) (sb (state source sh)))
364 (loop for args in (list (list nil source) (list target nil) (list topic source) (list target topic)) for i from 1 do
365 (let ((condition (condition-from (lambda () (apply #'import-map-content args)))))
366 (check (format nil "arguments:~D:TYPE-ERROR-before-either-map-mutation" i)
367 (and (typep condition 'type-error) (equal tb (state target th)) (equal sb (state source sh))))))))
368
369 (defun order-tests ()
370 (let ((outputs nil))
371 (dolist (reverse '(nil t))
372 (dolist (order '(nil t))
373 (let ((target (make-topic-map)))
374 (dolist (tag (if order '("b" "a" "c") '("a" "b" "c")))
375 (multiple-value-bind (source sh) (fixture tag :reverse reverse)
376 (let ((before (state source sh)) (xml (cxtm source)))
377 (import-map-content target source)
378 (preserve-source (format nil "order:~A:~A:~A" reverse order tag) source sh before xml))))
379 (check (format nil "order:~A:~A:fixed-point" reverse order) (valid target))
380 (push (cxtm target) outputs))))
381 (check "order:construction-and-import-order-byte-identical-CXTM" (every (lambda (x) (string= x (first outputs))) outputs))))
382 (defun run-tests ()
383 (topic-maps.template.tests:run-tests)
384 (let ((topic-maps.ctm.tests::*checks* nil))
385 (setf *witnesses* nil *rejections* nil)
386 (transfer-tests) (identity-tests) (merge-tests) (conflict-tests) (reifier-order-test) (alias-tests) (cascade-conflict-test) (boundary-tests) (order-tests)
387 (check "imports:no-promoted-CTM-or-XTM-contracts"
388 (equal '(:pass 187 :fail 2 :unsupported 0 :not-run 84)
389 (getf (topic-maps.test-foundation:summarize-corpus topic-maps.ctm.tests::*cxtm-corpus*) :statuses)))
390 (setf *last-checks* (nreverse topic-maps.ctm.tests::*checks*) *witnesses* (nreverse *witnesses*) *rejections* (nreverse *rejections*))
391 (format t "~&MAP-IMPORT-PASS: ~D checks; no CTM import contracts executed.~%" (length *last-checks*)) t))
392 (defun write-import-report (path)
393 (with-open-file (out path :direction :output :if-exists :supersede :if-does-not-exist :create)
394 (topic-maps.test-foundation::emit-json-value
395 (topic-maps.test-foundation::fields "kind" "map-content-import-model-evidence" "passed" (length *last-checks*) "failed" 0
396 "checks" (topic-maps.test-foundation::json-array *last-checks*)
397 "witnesses" (topic-maps.test-foundation::json-array *witnesses*)
398 "rejections" (topic-maps.test-foundation::json-array *rejections*)) out)
399 (terpri out)))