(defpackage #:topic-maps.xtm-mergemap.tests
(:use #:cl #:topic-maps.tmdm)
(:import-from #:topic-maps.variant.tests #:condition-from)
(:import-from #:topic-maps.mergemap.tests #:resources #:original-resources #:local #:si #:valid #:observe #:call-with-import-observer)
(:import-from #:topic-maps.ctm.tests #:binary-file)
(:export #:run-tests #:write-report #:write-reports #:checks))
(in-package #:topic-maps.xtm-mergemap.tests)
(defparameter *doc* "https://example.invalid/maps/main.ctm")
(defparameter *xtm* "http://psi.topicmaps.org/iso13250/xtm")
(defparameter *ctm* "http://psi.topicmaps.org/iso13250/ctm")
(defparameter *child* "https://example.invalid/maps/child.xtm")
(defparameter *xml* "")
(defvar *checks* nil)
(defvar *observations* nil)
(defvar *rejections* nil)
(defvar *example-map* nil)
(defvar *example-child* nil)
(defvar *original-result* nil)
(defun checks () *checks*)
(defun check (label truth)
(unless truth (error "XTM Mergemap check failed: ~A" label))
(push label *checks*) (format t "XTM-MERGEMAP-CHECK-PASS ~A~%" label))
(defun fields (&rest args) (apply #'topic-maps.test-foundation::fields args))
(defun bytes (text &optional (encoding :utf-8) bom)
(concatenate '(vector (unsigned-byte 8))
(if bom (ecase encoding (:utf-8 #(239 187 191)) (:utf-16le #(255 254)) (:utf-16be #(254 255))) #())
(babel:string-to-octets text :encoding encoding :use-bom nil)))
(defun directive (resource &optional (notation *xtm*)) (format nil "%mergemap <~A> <~A>" resource notation))
(defun read-map (text &rest keys)
(apply #'topic-maps.ctm:deserialize-string text :document-iri *doc* :source-name "memory:parent" keys))
(defun current (item) (topic-maps.tmdm::canonical-item item))
(defun handles (map) (topic-maps.import.tests::handles map))
(defun state (map held) (topic-maps.import.tests::state map held))
(defun xml (map &optional (base *doc*)) (topic-maps.cxtm:canonicalize map :base-locator base))
(defun core (map name) (si map (concatenate 'string "http://psi.topicmaps.org/iso13250/model/" name)))
(defun graph (label map owner player)
(let* ((instance (core map "instance")) (type (core map "type"))
(a (find-if (lambda (a) (some (lambda (r) (and (eq instance (association-role-type r))
(eq owner (association-role-player r)))) (association-roles a))) (topic-map-associations map)))
(roles (when a (association-roles a))))
(check (concatenate 'string label ":association-type-eq") (and a (eq (association-type a) (core map "type-instance"))))
(check (concatenate 'string label ":role-types-and-players-eq")
(and (= 2 (length roles)) (every (lambda (pair) (some (lambda (r) (and (eq (first pair) (association-role-type r))
(eq (second pair) (association-role-player r)))) roles))
(list (list instance owner) (list type player)))))
(check (concatenate 'string label ":parents-and-roles-played-eq")
(and (eq map (association-parent a)) (eq map (topic-parent owner)) (eq map (topic-parent player))
(every (lambda (r) (and (eq a (association-role-parent r)) (member r (topic-roles-played (association-role-player r)) :test #'eq))) roles)))
a))
(defun separate-locals (label map parent child)
(check (concatenate 'string label ":seven-topics-two-associations-four-roles")
(and (= 7 (length (topic-map-topics map))) (= 2 (length (topic-map-associations map)))
(= 4 (loop for a in (topic-map-associations map) sum (length (association-roles a))))))
(let ((po (local map (concatenate 'string parent "#lars"))) (pt (local map (concatenate 'string parent "#person")))
(co (local map (concatenate 'string child "#lars"))) (ct (local map (concatenate 'string child "#person"))))
(check (concatenate 'string label ":distinct-local-identities-no-include-aliases")
(and po pt co ct (= 4 (length (remove-duplicates (list po pt co ct) :test #'eq)))
(every (lambda (topic) (= 1 (length (topic-item-identifiers topic)))) (list po pt co ct))))
(graph (concatenate 'string label ":parent") map po pt)
(graph (concatenate 'string label ":child") map co ct))
(check (concatenate 'string label ":three-shared-core-subjects")
(and (every (lambda (name) (let ((topic (core map name))) (and topic (null (topic-item-identifiers topic))
(= 1 (length (topic-subject-identifiers topic)))))) '("type-instance" "instance" "type")) (valid map))))
(defun pure (label map &optional retained)
(let* ((held (append retained (handles map))) (before (state map held)) (output (xml map)))
(check (concatenate 'string label ":deterministic") (string= output (xml map)))
(check (concatenate 'string label ":raw-slots-and-retained-handles-unchanged") (equal before (state map held)))))
(defun call-with-xtm-observer (observer thunk)
(let ((original (symbol-function 'topic-maps.xtm:deserialize-octets)))
(unwind-protect
(progn (setf (symbol-function 'topic-maps.xtm:deserialize-octets)
(lambda (bytes &rest keys) (funcall observer original bytes keys)))
(funcall thunk))
(setf (symbol-function 'topic-maps.xtm:deserialize-octets) original))))
(defun original-case ()
(unless topic-maps.ctm.tests::*cxtm-corpus*
(setf topic-maps.ctm.tests::*cxtm-corpus* (topic-maps.test-foundation:discover-corpus)))
(let* ((id "ctm/in/mergemap-xtm.ctm")
(case (first (topic-maps.test-foundation:select-cases topic-maps.ctm.tests::*cxtm-corpus* :id id)))
(doc (topic-maps.test-foundation:test-case-document-iri case))
(child (concatenate 'string (subseq doc 0 (1+ (position #\/ doc :from-end t))) "mergemap-xtm.sub"))
(source-bytes (binary-file (merge-pathnames "mergemap-xtm.sub" (topic-maps.test-foundation:test-case-source-pathname case))))
(child-map nil) (imports 0) (reader-calls 0))
(multiple-value-bind (resolver calls) (resources (list (list child source-bytes "ctm/in/mergemap-xtm.sub")))
(let* ((map (call-with-xtm-observer
(lambda (saved bytes keys)
(incf reader-calls)
(check "original:raw-vector-eq-at-XTM-reader" (eq bytes source-bytes))
(check "original:XTM-document-and-source-key" (and (equal child (getf keys :document-iri))
(equal "ctm/in/mergemap-xtm.sub" (getf keys :source-key))))
(apply saved bytes keys))
(lambda () (call-with-import-observer
(lambda (saved target source)
(incf imports) (setf child-map source)
(let* ((held (handles source)) (before (state source held)) (output (xml source child)))
(multiple-value-bind (target resolve) (funcall saved target source)
(check "original:actual-child-map-and-import-operation" (and (= 5 (length (topic-map-topics source))) (= 1 (length (topic-map-associations source))) (not (eq source target))))
(check "original:source-preserved-after-import" (and (equal before (state source held)) (string= output (xml source child))))
(check "original:imported-content-is-copied-and-resolves-eq"
(every (lambda (item) (and (not (eq item source)) (not (eq item (funcall resolve item)))
(member (funcall resolve item) (handles target) :test #'eq))) (remove source held)))
(values target resolve))))
(lambda () (topic-maps.ctm:deserialize-file (topic-maps.test-foundation:test-case-source-pathname case)
:document-iri doc :source-name id :resource-resolver resolver))))))
(actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc))
(expected (binary-file (topic-maps.test-foundation:test-case-expected-baseline case))))
(separate-locals "original" map doc child) (pure "original" map)
(check "original:one-request-one-reader-one-import" (and (= 1 reader-calls imports) (= 1 (length (funcall calls)))))
(check "original:1519-byte-original-baseline-exact" (and (= 1519 (length expected) (length actual)) (equalp actual expected)))
(topic-maps.test-foundation:record-result case :cxtm-match)
(setf (topic-maps.test-foundation:test-case-clauses case) '("CTM 3.16.3" "XTM 2.0 4.3-4.9,4.20" "TMDM 6.2,6.6-6.7,7.2" "CXTM 3.18"))
(setf *original-result* (list :id id :base doc :actual (xml map doc) :expected (babel:octets-to-string expected :encoding :utf-8) :match t :bytes (length actual)))
(setf topic-maps.ctm.tests::*cxtm-results* (append (remove id topic-maps.ctm.tests::*cxtm-results* :key (lambda (r) (getf r :id)) :test #'equal) (list *original-result*)))
(setf *example-map* map *example-child* child-map)
(push (fields "id" id "documentIRI" doc "resourceIRI" child "sourceKey" "ctm/in/mergemap-xtm.sub"
"requests" (coerce (funcall calls) 'vector) "model" (observe map) "childModel" (observe child-map)
"actualCXTM" (getf *original-result* :actual) "expectedCXTM" (getf *original-result* :expected) "bytes" 1519) *observations*)))))
(defun dispatch-tests ()
(dolist (spec (list (list "relative-full-IRI" (directive "child.xtm"))
(list "absolute-full-IRI" (directive *child*))
(list "notation-QName" (format nil "%prefix n ~%%mergemap n:xtm"))
(list "resource-and-notation-QName" (format nil "%prefix r <./>~%%prefix n ~%%mergemap r:child.xtm n:xtm"))))
(destructuring-bind (label text) spec
(multiple-value-bind (resolver calls) (resources (list (list *child* (bytes *xml*) "test:source")))
(let ((map (read-map (format nil "~A~%lars isa person." text) :resource-resolver resolver)))
(separate-locals label map *doc* *child*)
(check (concatenate 'string label ":exact-resolver-contract")
(equal (topic-maps.test-foundation::json-object-members (first (funcall calls)))
(topic-maps.test-foundation::json-object-members
(fields "resourceIRI" *child* "purpose" "MERGEMAP" "fromDocumentIRI" *doc* "notation" *xtm* "sourceKey" "test:source"))))))))
(let ((outputs nil))
(dolist (key '("unrelated:diagnostic-label" "different:diagnostic-label" nil))
(multiple-value-bind (resolver calls) (resources (list (list *child* (bytes *xml*) key)))
(declare (ignore calls))
(let ((map (read-map (format nil "~A~%lars isa person." (directive "child.xtm")) :resource-resolver resolver)))
(push (xml map) outputs))))
(check "source-key:does-not-change-topic-identifiers-or-model" (every (lambda (x) (equal x (first outputs))) outputs)))
(let* ((encoded (bytes (concatenate 'string ""
(substitute #\Space #\Newline *xml*)) :utf-16le t))
(before (copy-seq encoded)) (decoder-calls 0) (xml-seen nil)
(saved-decoder (symbol-function 'topic-maps.ctm::decode-ctm-resource))
(saved-boundary (symbol-function 'topic-maps.xml:parse-octets)))
(check "UTF16:negative-control-CTM-UTF8-cannot-decode"
(typep (condition-from (lambda () (topic-maps.ctm::decode-utf8 encoded))) 'topic-maps.ctm:ctm-rejection))
(multiple-value-bind (resolver calls) (resources (list (list *child* encoded "UTF16:child")))
(declare (ignore calls))
(unwind-protect
(progn
(setf (symbol-function 'topic-maps.ctm::decode-ctm-resource)
(lambda (&rest args) (incf decoder-calls) (apply saved-decoder args)))
(setf (symbol-function 'topic-maps.xml:parse-octets)
(lambda (bytes handler) (setf xml-seen (eq bytes encoded)) (funcall saved-boundary bytes handler)))
(let ((map (read-map (format nil "~A~%lars isa person." (directive "child.xtm")) :resource-resolver resolver)))
(separate-locals "UTF16" map *doc* *child*)
(check "UTF16:original-bytes-eq-at-qualified-XML-boundary" xml-seen)
(check "UTF16:no-CTM-decoding-no-byte-mutation" (and (zerop decoder-calls) (equalp before encoded)))))
(setf (symbol-function 'topic-maps.ctm::decode-ctm-resource) saved-decoder
(symbol-function 'topic-maps.xml:parse-octets) saved-boundary))))
(multiple-value-bind (resolver calls) (resources (list (list *child* (map 'vector #'identity (bytes *xml*)) "generic-vector")))
(declare (ignore calls))
(let ((map (read-map (directive "child.xtm") :resource-resolver resolver)))
(check "resolver:generic-byte-vector-storage-supported-without-decoding" (= 5 (length (topic-map-topics map)))))))
(defun repeat-and-mixed-tests ()
(let ((old-items nil) (imports 0))
(multiple-value-bind (resolver calls) (resources (list (list *child* (bytes *xml*) "repeat:child")))
(let ((map (call-with-import-observer
(lambda (saved target source)
(incf imports)
(let* ((held (handles source)) (before (state source held)))
(multiple-value-bind (result resolve) (funcall saved target source)
(check (format nil "repeat:~D:source-raw-state-preserved" imports) (equal before (state source held)))
(if (= imports 1)
(setf old-items (mapcar (lambda (item) (funcall resolve item)) (remove source held)))
(check "repeat:all-retained-target-handles-canonical" (every (lambda (item) (member (current item) (handles target) :test #'eq)) old-items)))
(values result resolve))))
(lambda () (read-map (format nil "~A~%~A~%lars isa person." (directive "child.xtm") (directive "child.xtm")) :resource-resolver resolver)))))
(check "repeat:two-actual-resolver-and-import-calls" (and (= 2 imports) (= 2 (length (funcall calls)))))
(separate-locals "repeat" map *doc* *child*) (pure "repeat" map old-items))))
(let ((results nil))
(dolist (reverse '(nil t))
(multiple-value-bind (resolver calls)
(resources (list (list *child* (bytes *xml*) "mixed:xtm")
(list "https://example.invalid/maps/child.ctm" (bytes "lars isa person.") "mixed:ctm")))
(let* ((directives (list (directive "child.xtm") (directive "child.ctm" *ctm*)))
(text (format nil "~{~A~%~}lars isa person." (if reverse (reverse directives) directives)))
(map (read-map text :resource-resolver resolver)))
(check (format nil "mixed:~A:actual-model-counts-and-canonical-refs" reverse)
(and (= 9 (length (topic-map-topics map))) (= 3 (length (topic-map-associations map))) (valid map)))
(check (format nil "mixed:~A:notation-dispatch-order" reverse)
(equal (mapcar (lambda (r) (cdr (assoc "notation" (topic-maps.test-foundation::json-object-members r) :test #'equal))) (funcall calls))
(if reverse (list *ctm* *xtm*) (list *xtm* *ctm*))))
(pure (format nil "mixed:~A" reverse) map) (push (xml map) results))))
(check "mixed:construction-order-independent-CXTM" (equal (first results) (second results))))
(multiple-value-bind (resolver calls)
(resources (list (list "https://example.invalid/maps/sub/child.ctm"
(bytes (format nil "~A~%lars isa person." (directive "../child.xtm"))) "nested:ctm")
(list *child* (bytes *xml*) "nested:xtm")))
(let* ((map (read-map (format nil "~A~%lars isa person." (directive "sub/child.ctm" *ctm*)) :resource-resolver resolver))
(second-request (topic-maps.test-foundation::json-object-members (second (funcall calls)))))
(check "nested:relative-XTM-resource-and-child-caller-context"
(and (= 9 (length (topic-map-topics map))) (= 3 (length (topic-map-associations map)))
(equal *child* (cdr (assoc "resourceIRI" second-request :test #'equal)))
(equal "https://example.invalid/maps/sub/child.ctm" (cdr (assoc "fromDocumentIRI" second-request :test #'equal))) (valid map)))))
(let ((source ""))
(multiple-value-bind (resolver calls) (resources (list (list *child* (bytes source) "forward:child")))
(declare (ignore calls))
(let* ((map (read-map (format nil "person .~%~A~%lars isa person." (directive "child.xtm")) :resource-resolver resolver))
(player (local map (concatenate 'string *doc* "#person"))))
(check "forward:identity-merge-shared-player-EQ" (and (= 6 (length (topic-map-topics map)))
(eq player (local map (concatenate 'string *child* "#person"))) (eq player (si map "urn:person")) (= 2 (length (topic-item-identifiers player)))))
(graph "forward:child" map (local map (concatenate 'string *child* "#lars")) player)
(graph "forward:parent" map (local map (concatenate 'string *doc* "#lars")) player)))))
(defun metadata-tests ()
;; Model API instrumentation is explicitly separate from the XTM reader:
;; XTM Reification is still unsupported. Test the actual import boundary's
;; exclusion of the source Map item, including its existing Reifier edge.
(let ((seen nil))
(multiple-value-bind (resolver calls)
(resources (list (list *child* (bytes "") "metadata:child")))
(declare (ignore calls))
(let ((map (call-with-import-observer
(lambda (saved target source)
(add-item-identifier target "urn:target-map")
(set-item-reifier target (intern-topic target :subject-identifier "urn:target-reifier"))
(set-item-reifier source (first (topic-map-topics source)))
(let* ((held (handles source)) (before (state source held))
(source-reifier (item-reifier source)) (target-reifier (item-reifier target)))
(multiple-value-bind (result resolve) (funcall saved target source)
(setf seen t)
(check "metadata:source-map-state-and-reifier-EQ-preserved" (and (equal before (state source held)) (eq source (topic-reified source-reifier))))
(check "metadata:map-identifiers-and-map-reifier-not-copied"
(and (equal '("urn:target-map") (item-identifiers target)) (eq target-reifier (item-reifier target))
(null (topic-reified (funcall resolve source-reifier)))))
(check "metadata:source-map-not-a-content-item" (typep (condition-from (lambda () (funcall resolve source))) 'map-import-error))
(values result resolve))))
(lambda () (read-map (directive "child.xtm") :resource-resolver resolver)))))
(check "metadata:actual-import-was-reached" (and seen (valid map)))))))
(define-condition resolver-denied (error) ())
(defun failure (label text class code &key resolver expected-cause)
(let* ((c (condition-from (lambda () (read-map text :resource-resolver resolver))))
(frame (when (typep c 'topic-maps.ctm:ctm-error) (first (topic-maps.ctm:error-context c)))))
(check (concatenate 'string label ":condition-and-code") (and (eq (type-of c) class) (eq code (topic-maps.ctm:error-code c))))
(check (concatenate 'string label ":parent-directive-location")
(and (equal "memory:parent" (topic-maps.ctm:error-source c))
(= (search "%mergemap" text) (topic-maps.ctm:error-position c))
(plusp (topic-maps.ctm:error-line c)) (plusp (topic-maps.ctm:error-column c))))
(check (concatenate 'string label ":resource-and-notation-context")
(and (eq :mergemap (getf frame :purpose)) (equal *child* (getf frame :resource-iri)) (equal *doc* (getf frame :document-iri))
(equal *xtm* (getf frame :notation))))
(when expected-cause (check (concatenate 'string label ":original-condition-retained") (typep (topic-maps.ctm:error-cause c) expected-cause)))
(push (fields "label" label "class" (symbol-name (type-of c)) "code" (symbol-name (topic-maps.ctm:error-code c))
"source" (topic-maps.ctm:error-source c) "position" (topic-maps.ctm:error-position c)
"line" (topic-maps.ctm:error-line c) "column" (topic-maps.ctm:error-column c)
"resourceIRI" (getf frame :resource-iri) "sourceKey" (getf frame :resource-source)
"causeClass" (when (topic-maps.ctm:error-cause c) (symbol-name (type-of (topic-maps.ctm:error-cause c))))) *rejections*)
c))
(defun negative-tests ()
(let ((text (format nil "before.~%~A" (directive "child.xtm"))))
(failure "missing-resolver" text 'topic-maps.ctm:ctm-unsupported :resource-resolver-required)
(failure "unknown-resource" text 'topic-maps.ctm:ctm-resource-error :resource-unavailable :resolver (lambda (&rest args) (declare (ignore args)) nil))
(failure "resolver-refusal" text 'topic-maps.ctm:ctm-resource-error :resource-resolver-failure
:resolver (lambda (&rest args) (declare (ignore args)) (error 'resolver-denied)) :expected-cause 'resolver-denied)
(dolist (spec '(("text" "source") (#(256) "source") (#(60) 42)))
(failure (format nil "invalid-result:~S" spec) text 'topic-maps.ctm:ctm-resource-error :invalid-resource-result
:resolver (lambda (&rest args) (declare (ignore args)) (values (first spec) (second spec)))))
(dolist (spec (list
(list "malformed-XML" (bytes "") 'topic-maps.xtm:xtm-xml-error :xml-not-well-formed)
(list "invalid-UTF8" (concatenate '(vector (unsigned-byte 8)) (bytes "")) 'topic-maps.xtm:xtm-xml-error :xml-not-well-formed)
(list "DOCTYPE-file-denied" (bytes (concatenate 'string "" *xml*)) 'topic-maps.xtm:xtm-xml-error :doctype-forbidden)
(list "bad-version" (bytes "") 'topic-maps.xtm:xtm-rejection :invalid-version)
(list "bad-namespace" (bytes "") 'topic-maps.xtm:xtm-rejection :invalid-namespace)
(list "bad-id" (bytes "") 'topic-maps.xtm:xtm-rejection :invalid-id)
(list "unsupported-XTM-mergeMap" (bytes "") 'topic-maps.xtm:xtm-unsupported :resource-resolver-required)
(list "unsupported-occurrence-reification" (bytes "O") 'topic-maps.xtm:xtm-unsupported :resource-data-datatype)
(list "unsupported-name-variant-resourceData-datatype" (bytes "N42") 'topic-maps.xtm:xtm-unsupported :resource-data-datatype)))
(destructuring-bind (label raw cause code) spec
(multiple-value-bind (resolver calls) (resources (list (list *child* raw "bad:child-source")))
(let* ((c (failure label text (if (eq cause 'topic-maps.xtm:xtm-unsupported) 'topic-maps.ctm:mergemap-source-unsupported 'topic-maps.ctm:mergemap-source-error)
code :resolver resolver :expected-cause cause))
(original (topic-maps.ctm:error-cause c)))
(check (concatenate 'string label ":child-source-document-and-XML-position")
(and (= 1 (length (funcall calls))) (equal "bad:child-source" (topic-maps.xtm:error-source-key original))
(equal *child* (topic-maps.xtm:error-document-iri original)) (integerp (topic-maps.xtm:error-line original))
(integerp (topic-maps.xtm:error-column original))
(not (typep c 'topic-maps.ctm:ctm-rejection))))))))
(multiple-value-bind (resolver calls) (resources (list (list *child* (bytes "") "bad:child-model")))
(declare (ignore calls))
(failure "TMDM-child-core-conflict" text 'topic-maps.ctm:mergemap-source-error :core-subject-conflict :resolver resolver :expected-cause 'core-subject-error)))
(let ((requests 0))
(let ((c (condition-from (lambda () (read-map (directive "child.xtm" "urn:unknown") :resource-resolver (lambda (&rest args) (declare (ignore args)) (incf requests)))))))
(check "unknown-notation:is-CTM-rejection-before-fetch" (and (typep c 'topic-maps.ctm:ctm-rejection) (eq :unknown-mergemap-notation (topic-maps.ctm:error-code c)) (zerop requests))))))
(defun conflict-tests ()
(dolist (kind '(:item :core))
(multiple-value-bind (resolver calls) (resources (list (list *child* (bytes *xml*) "conflict:xtm-child")))
(declare (ignore calls))
(let ((reached nil))
(let* ((text (format nil "before.~%~A" (directive "child.xtm")))
(c (condition-from (lambda () (call-with-import-observer
(lambda (saved target source)
(setf reached t)
;; Prepare conflicting target metadata using the public model
;; API; the child is the actual untouched XTM reader output.
(ecase kind
(:item (add-item-identifier target (concatenate 'string *child* "#lars")))
(:core (let ((topic (intern-topic target :subject-identifier "http://psi.topicmaps.org/iso13250/model/instance")))
(add-item-identifier topic "http://psi.topicmaps.org/iso13250/model/type"))))
(let* ((th (handles target)) (sh (handles source)) (tb (state target th)) (sb (state source sh))
(tx (xml target)) (sx (xml source))
(cause (condition-from (lambda () (funcall saved target source)))))
(check (format nil "conflict:~A:actual-import-precise-cause" kind)
(ecase kind (:item (typep cause 'item-identifier-error)) (:core (typep cause 'core-subject-error))))
(check (format nil "conflict:~A:both-models-and-raw-handles-unchanged" kind)
(and (equal tb (state target th)) (equal sb (state source sh)) (equal tx (xml target)) (equal sx (xml source))))
(error cause)))
(lambda () (read-map text :resource-resolver resolver)))))))
(check (format nil "conflict:~A:import-condition-preserved-at-directive" kind)
(and reached (typep c 'topic-maps.ctm:ctm-rejection)
(= (search "%mergemap" text) (topic-maps.ctm:error-position c))
(ecase kind (:item (typep (topic-maps.ctm:error-cause c) 'item-identifier-error))
(:core (typep (topic-maps.ctm:error-cause c) 'core-subject-error))))))))))
(defun retained-contract-tests ()
;; Repeat the actual old sources with the new adapter already installed.
;; Do not register duplicate contract results or reset prior observations.
(let ((corpus (or topic-maps.ctm.tests::*cxtm-corpus* (topic-maps.test-foundation:discover-corpus))))
(dolist (name '("mergemap" "mergemap-syntax-qname" "mergemap-template" "mergemap-tm-reifier"
"include" "include-prefix" "include-prefix2" "include-template" "include-template-wildcard"
"include-tm-reifier" "include-topicref" "include-wildcard" "include-wildcard2"))
(let* ((id (format nil "ctm/in/~A.ctm" name))
(case (first (topic-maps.test-foundation:select-cases corpus :id id))))
(multiple-value-bind (resolver calls) (resources
(loop for auxiliary in (topic-maps.test-foundation:corpus-auxiliaries corpus)
collect (list (getf auxiliary :document-iri) (binary-file (getf auxiliary :source-pathname)) (getf auxiliary :id))))
(declare (ignore calls))
(let* ((map (topic-maps.ctm:deserialize-file (topic-maps.test-foundation:test-case-source-pathname case)
:document-iri (topic-maps.test-foundation:test-case-document-iri case) :source-name id :resource-resolver resolver))
(actual (topic-maps.cxtm:canonicalize-octets map :base-locator (topic-maps.test-foundation:test-case-document-iri case)))
(expected (binary-file (topic-maps.test-foundation:test-case-expected-baseline case)))
(old (find id topic-maps.ctm.tests::*cxtm-results* :key (lambda (r) (getf r :id)) :test #'equal)))
(check (concatenate 'string "adapter-loaded:" id ":model-and-original-comparison")
(and (valid map) (if (member name '("include-template" "include-wildcard2") :test #'equal)
(not (equalp actual expected)) (equalp actual expected))))
(when old
(check (concatenate 'string "adapter-loaded:" id ":unchanged-executed-output")
(equalp actual (bytes (getf old :actual)))))))))
(dolist (spec '(("mergemap-missing-syntax-iri" topic-maps.ctm:ctm-rejection :expected-mergemap-notation)
("mergemap-template" topic-maps.ctm:undefined-template :undefined-template)
("mergemap-unknown-syntax" topic-maps.ctm:ctm-rejection :unknown-mergemap-notation)
("include-prefix" topic-maps.ctm:undefined-prefix :undefined-prefix)
("include-prefix2" topic-maps.ctm:undefined-prefix :undefined-prefix)
("include-xtm" topic-maps.ctm:ctm-rejection :invalid-iri)))
(destructuring-bind (name class code) spec
(let* ((id (format nil "ctm/invalid/~A.ctm" name))
(case (first (topic-maps.test-foundation:select-cases corpus :id id))))
(multiple-value-bind (resolver calls) (resources
(loop for auxiliary in (topic-maps.test-foundation:corpus-auxiliaries corpus)
collect (list (getf auxiliary :document-iri) (binary-file (getf auxiliary :source-pathname)) (getf auxiliary :id))))
(declare (ignore calls))
(let ((condition (condition-from (lambda () (topic-maps.ctm:deserialize-file (topic-maps.test-foundation:test-case-source-pathname case)
:document-iri (topic-maps.test-foundation:test-case-document-iri case) :source-name id :resource-resolver resolver)))))
(check (concatenate 'string "adapter-loaded:" id ":same-intended-rejection")
(and (eq class (type-of condition)) (eq code (topic-maps.ctm:error-code condition)))))))))))
(defun run-tests ()
(setf *checks* nil *observations* nil *rejections* nil)
(original-case) (dispatch-tests) (repeat-and-mixed-tests) (metadata-tests) (negative-tests) (conflict-tests) (retained-contract-tests)
(check "only-one-new-original-contract" (eq :pass (topic-maps.test-foundation:test-case-status
(first (topic-maps.test-foundation:select-cases topic-maps.ctm.tests::*cxtm-corpus* :id "ctm/in/mergemap-xtm.ctm")))))
(check "unique-check-names" (= (length *checks*) (length (remove-duplicates *checks* :test #'equal))))
(setf *checks* (nreverse *checks*) *observations* (nreverse *observations*) *rejections* (nreverse *rejections*))
(format t "XTM-MERGEMAP-PASS: ~D checks; actual 1519-byte original contract PASS~%" (length *checks*)) t)
(defun write-report (path)
(with-open-file (out path :direction :output :if-exists :supersede)
(topic-maps.test-foundation::emit-json-value
(fields "kind" "ctm-xtm-mergemap-execution" "passed" (length *checks*) "failed" 0 "checks" (coerce *checks* 'vector)
"cases" (coerce *observations* 'vector) "rejections" (coerce *rejections* 'vector)) out) (terpri out)))
(defun write-reports (prefix)
(write-report (concatenate 'string prefix ".integration.json"))
(topic-maps.ctm.tests::write-cxtm-report
(concatenate 'string prefix ".validation.json") (concatenate 'string prefix ".cxtm.json")
:additional-checks *checks*
:scope "Slice 19D2: one actual XTM Mergemap original added; CTM 205 PASS / 4 known FAIL / 64 NOT-RUN after retained regression. Canonicalizer and integration checks here; full retained semantic checks reported separately."))