(defpackage #:topic-maps.identifier.tests (:use #:cl #:topic-maps.tmdm) (:import-from #:topic-maps.ctm.tests #:check #:map-state #:cxtm #:binary-file #:read-text) (:import-from #:topic-maps.variant.tests #:condition-from) (:export #:run-tests #:identifier-checks #:write-identifier-report)) (in-package #:topic-maps.identifier.tests) (defvar *last-checks* nil) (defvar *examples* nil) (defvar *ordering-output* nil) (defparameter *kinds* '(topic-map topic topic-name variant occurrence association association-role)) (defparameter *readers* '(topic-map-item-identifiers topic-item-identifiers topic-name-item-identifiers variant-item-identifiers occurrence-item-identifiers association-item-identifiers association-role-item-identifiers)) (defun identifier-checks () *last-checks*) (defun si (map name) (intern-topic map :subject-identifier (concatenate 'string "urn:" name))) (defun fixture (&optional (map (make-topic-map)) (prefix "")) (let* ((owner (si map (concatenate 'string prefix "owner"))) (type (si map (concatenate 'string prefix "type"))) (theme (si map (concatenate 'string prefix "theme"))) (name (add-name owner "Name" :type type)) (variant (add-variant name "Variant" :scope (list theme))) (occurrence (add-occurrence owner type "Value" :scope (list theme))) (association (make-association map type (list (list type owner) (list type theme)) :scope (list theme)))) (values map (list map owner name variant occurrence association (first (association-roles association))) type theme))) (defun current (item) (topic-maps.tmdm::canonical-item item)) (defun raw-handles (items) (mapcar (lambda (i) (list i (copy-tree (mapcar #'copy-seq (topic-maps.tmdm::%item-identifiers i))) (etypecase i (topic-map nil) (topic (topic-maps.tmdm::%topic-forward i)) (topic-name (topic-maps.tmdm::%topic-name-forward i)) (variant (topic-maps.tmdm::%variant-forward i)) (occurrence (topic-maps.tmdm::%occurrence-forward i)) (association (topic-maps.tmdm::%association-forward i)) (association-role (topic-maps.tmdm::%association-role-forward i))))) items)) (defun rejects-without-mutation (label map thunk &optional handles) (let* ((before (map-state map)) (old (raw-handles handles)) (c (condition-from thunk))) (check (concatenate 'string label ":specific") (and (typep c 'item-identifier-error) (eq :duplicate-item-identifier (item-identifier-error-code c)))) (check (concatenate 'string label ":atomic") (and (equal before (map-state map)) (equal old (raw-handles handles)))))) (defun set-tests () (multiple-value-bind (map items) (fixture) (loop for item in items for kind in *kinds* for reader in *readers* do (check (format nil "sets:~A:empty" kind) (and (null (item-identifiers item)) (null (funcall reader item)))) (let ((input (format nil "urn:item:~A" kind))) (check (format nil "sets:~A:return-current" kind) (eq item (add-item-identifier item input))) (let ((saved (copy-seq input))) (setf (char input 0) #\X) (let ((ids (item-identifiers item)) (specific (funcall reader item))) (setf (char (first ids) 0) #\Y (car ids) "replaced" (char (first specific) 0) #\Z) (check (format nil "sets:~A:input-output-strings-and-conses-owned" kind) (equal (list saved) (item-identifiers item)))) (add-item-identifier item saved) (check (format nil "sets:~A:idempotent" kind) (equal (list saved) (funcall reader item))))) (dolist (bad '(nil 42 "" "relative")) (let* ((before (map-state map)) (c (condition-from (lambda () (add-item-identifier item bad))))) (check (format nil "sets:~A:invalid:~S:atomic" kind bad) (and (typep c 'item-identifier-error) (eq :invalid-item-identifiers (item-identifier-error-code c)) (equal before (map-state map))))))) (check "sets:all-seven-items-global-uniqueness" (topic-maps.tmdm::validate-map-item-identifiers map))) ;; Every directed cross-kind collision, including the Topic Map itself. (dotimes (from 7) (dotimes (to 7) (unless (= from to) (multiple-value-bind (map items) (fixture) (add-item-identifier (nth from items) "urn:collision") (rejects-without-mutation (format nil "collision:~A:~A" (nth from *kinds*) (nth to *kinds*)) map (lambda () (add-item-identifier (nth to items) "urn:collision")) items))))) (loop for index from 1 below 7 do (multiple-value-bind (map one) (fixture) (multiple-value-bind (same-map two) (fixture map "other-") (declare (ignore same-map)) (add-item-identifier (nth index one) "urn:same-kind") (if (= index 1) (let ((merged (add-item-identifier (nth index two) "urn:same-kind"))) (check "collision:two-topics-merge-not-reject" (and (eq merged (canonical-topic (nth index one))) (member "urn:owner" (topic-subject-identifiers merged) :test #'string=) (member "urn:other-owner" (topic-subject-identifiers merged) :test #'string=)))) (rejects-without-mutation (format nil "collision:unequal-~A" (nth index *kinds*)) map (lambda () (add-item-identifier (nth index two) "urn:same-kind")) (append one two)))))) ;; Existing constructor/Topic mutation routes must share the global check. (dotimes (index 7) (multiple-value-bind (map items type theme) (fixture) (unless (= index 1) (add-item-identifier (nth index items) "urn:collision") (rejects-without-mutation (format nil "legacy:intern-topic:~D" index) map (lambda () (intern-topic map :item-identifier "urn:collision"))) (rejects-without-mutation (format nil "legacy:add-topic-identifier:~D" index) map (lambda () (add-topic-identifier (second items) :item-identifier "urn:collision")))) (when (= index 1) (add-item-identifier (second items) "urn:collision")) (rejects-without-mutation (format nil "legacy:add-occurrence:~D" index) map (lambda () (add-occurrence (second items) type "Different" :scope (list theme) :item-identifiers '("urn:collision")))))) (let* ((map (make-topic-map)) (owner (si map "owner")) (type (si map "type")) (input (list (copy-seq "urn:owned"))) (o (add-occurrence owner type "x" :item-identifiers input))) (setf (char (first input) 0) #\X (car input) "replaced") (check "legacy:occurrence-input-strings-and-list-owned" (equal '("urn:owned") (item-identifiers o))) (let ((same (add-occurrence owner type "x" :item-identifiers '("urn:owned" "urn:other")))) (check "legacy:equal-occurrence-can-reuse-and-union-identifiers" (and (eq same (canonical-occurrence o)) (= 2 (length (item-identifiers o)))))))) (defun merge-tests () (let* ((map (make-topic-map)) (a (si map "a")) (b (si map "b")) (type (si map "type")) (theme (si map "theme")) (extra (si map "extra")) (n1 (add-name a "N" :type type :scope (list theme))) (n2 (add-name b "N" :type type :scope (list theme))) (v1 (add-variant n1 "V" :scope (list theme extra))) (v2 (add-variant n2 "V" :scope (list extra theme))) (o1 (add-occurrence a type "O")) (o2 (add-occurrence b type "O")) (a1 (make-association map type (list (list type a)))) (a2 (make-association map type (list (list type b)))) (r1 (first (association-roles a1))) (r2 (first (association-roles a2))) (one (list a n1 v1 o1 a1 r1)) (two (list b n2 v2 o2 a2 r2))) (loop for x in one for y in two for index from 1 do (add-item-identifier x (format nil "urn:one:~D" index)) (add-item-identifier y (format nil "urn:two:~D" index))) (let ((c (add-item-identifier b "urn:one:1"))) (loop for x in one for y in two for index from 1 do (check (format nil "merge:kind-~D:identifier-union-and-both-handles" index) (and (eq (current x) (current y)) (equal (sort (item-identifiers x) #'string<) (list (format nil "urn:one:~D" index) (format nil "urn:two:~D" index))))) (add-item-identifier x (format nil "urn:via-old-handle:~D" index)) (check (format nil "merge:kind-~D:old-handle-addition" index) (member (format nil "urn:via-old-handle:~D" index) (item-identifiers y) :test #'string=))) (let ((again (add-name c "N" :type type :scope (list theme)))) (add-item-identifier again "urn:existing-name") (check "merge:existing-equal-name-keeps-identifiers" (and (eq again (canonical-topic-name n1)) (= 4 (length (item-identifiers n2)))))) (check "merge:stored-parent-type-player-and-variant-scope" (and (eq c (topic-maps.tmdm::%topic-name-parent (current n1))) (eq (current n1) (topic-maps.tmdm::%variant-parent (current v1))) (eq c (topic-maps.tmdm::%occurrence-parent (current o1))) (eq (current a1) (topic-maps.tmdm::%association-role-parent (current r1))) (eq c (topic-maps.tmdm::%association-role-player (current r2))) (topic-maps.reference.tests::references-current-p map))) (let ((before (map-state map)) (handles (raw-handles (append one two)))) (check "merge:canonicalization-including-all-retained-handles-is-pure" (and (string= (cxtm map) (cxtm map)) (equal before (map-state map)) (equal handles (raw-handles (append one two)))))))) ;; Roles also merge inside a single Association, independently of Assoc merges. (let* ((map (make-topic-map)) (type (si map "type")) (a (si map "a")) (b (si map "b")) (p (si map "p")) (rel (make-association map type (list (list a p) (list b p)))) (r1 (first (association-roles rel))) (r2 (second (association-roles rel)))) (add-item-identifier r1 "urn:role:1") (add-item-identifier r2 "urn:role:2") (merge-topics a b) (check "merge:role-type-merge-preserves-both-identifiers-and-handles" (and (= 1 (length (association-roles rel))) (eq (current r1) (current r2)) (= 2 (length (item-identifiers r1))) (topic-maps.reference.tests::references-current-p map)))) (dolist (kind '(topic-name occurrence association)) (let* ((map (make-topic-map)) (owner (si map "owner")) (type (si map "type")) (a (si map "a")) (b (si map "b"))) (labels ((new (scope) (ecase kind (topic-name (add-name owner "N" :type type :scope scope)) (occurrence (add-occurrence owner type "O" :scope scope)) (association (make-association map type (list (list type owner)) :scope scope))))) (let ((one (new (list a))) (two (new (list b)))) (add-item-identifier one "urn:scope:1") (add-item-identifier two "urn:scope:2") (merge-topics a b) (check (format nil "merge:theme:~A:identifiers-and-handles" kind) (and (eq (current one) (current two)) (= 2 (length (item-identifiers one))) (topic-maps.reference.tests::references-current-p map))))))) (let* ((map (make-topic-map)) (owner (si map "owner")) (a (si map "a")) (b (si map "b")) (n (add-name owner "N" :scope (list a))) (v (add-variant n "V" :scope (list a b)))) (add-item-identifier a "urn:theme") (add-item-identifier n "urn:name") (add-item-identifier v "urn:variant") (let* ((before (map-state map)) (old (raw-handles (list a b n v))) (e (condition-from (lambda () (add-item-identifier b "urn:theme"))))) (check "merge:identifier-triggered-variant-collapse-fully-atomic" (and (typep e 'variant-error) (eq :variant-scope-collapse (variant-error-code e)) (equal before (map-state map)) (equal old (raw-handles (list a b n v)))))))) (defun boundary-tests () (loop for index from 0 below 7 do (multiple-value-bind (map one) (fixture) (multiple-value-bind (other two) (fixture) (let ((iri (format nil "urn:independent:~D" index))) (add-item-identifier (nth index one) iri) (add-item-identifier (nth index two) iri) (check (format nil "boundary:same-locator-in-separate-maps:~D" index) (and (not (eq map other)) (not (eq (current (nth index one)) (current (nth index two)))) (topic-maps.tmdm::validate-map-item-identifiers map) (topic-maps.tmdm::validate-map-item-identifiers other))))))) (multiple-value-bind (map items type) (fixture) (let ((o (fifth items))) (add-item-identifier o "urn:occurrence-boundary") (let ((subject (intern-topic map :subject-identifier "urn:occurrence-boundary"))) (check "boundary:occurrence-II-then-Topic-SI-no-automatic-reification" (and (null (item-reifier o)) (null (topic-reified subject))))) (add-item-identifier o "urn:type") (check "boundary:Topic-SI-then-occurrence-II-no-automatic-reification" (and (member "urn:type" (item-identifiers o) :test #'string=) (null (item-reifier o)) (null (topic-reified type)))) (add-item-identifier (third items) "urn:theme") (check "boundary:no-invented-reification-on-other-item-kinds" (and (equal '("urn:theme") (item-identifiers (third items))) (null (item-identifiers type)))))) (let* ((map (make-topic-map)) (owner (si map "owner")) (type (si map "type")) (n1 (add-name owner "N1" :type type)) (n2 (add-name owner "N2" :type type)) (decomposed (format nil "urn:e~A" (code-char #x301))) (composed "urn:é")) (add-item-identifier n1 decomposed) (add-item-identifier n2 composed) (let ((before (map-state map))) (check "locators:TMDM-equality-is-not-CXTM-NFC-equality" (and (not (string= decomposed composed)) (= 2 (length (topic-names owner))) (search composed (cxtm map)) (equal before (map-state map))))))) (defun serialization-tests () (let ((expected nil)) (dolist (reverse '(nil t)) (multiple-value-bind (map items) (fixture) (loop for item in items for kind in *kinds* do (dolist (suffix (if reverse '("a" "z") '("z" "a"))) (add-item-identifier item (format nil "urn:~A:~A" kind suffix)))) (let ((before (map-state map)) (actual (cxtm map))) (unless expected (setf expected actual)) (check (format nil "serialization:identifier-order:~A:deterministic-pure" reverse) (and (string= expected actual) (string= actual (cxtm map)) (equal before (map-state map))))))) (setf *ordering-output* expected)) (let ((expected nil)) (dolist (reverse '(nil t)) (let* ((map (make-topic-map)) (type (si map "type")) (theme (si map "theme"))) (add-item-identifier map "urn:map") (dolist (label (if reverse '("b" "a") '("a" "b"))) (let* ((owner (si map label)) (n (add-name owner "N" :type type)) (v (add-variant n "V" :scope (list theme))) (o (add-occurrence owner type "O")) (a (make-association map type (list (list type owner)))) (r (first (association-roles a)))) (loop for item in (list owner n v o a r) for index from 1 do (add-item-identifier item (format nil "urn:item:~A:~D" label index))))) (let ((before (map-state map)) (actual (cxtm map))) (unless expected (setf expected actual)) (check (format nil "serialization:construction-order:~A:pure-and-identical" reverse) (and (string= expected actual) (equal before (map-state map)))))))) (multiple-value-bind (map items) (fixture) ;; Deliberately corrupt private storage to exercise canonicalizer validation. (setf (topic-maps.tmdm::%topic-map-item-identifiers map) '("urn:collision") (topic-maps.tmdm::%topic-name-item-identifiers (third items)) '("urn:collision")) (let* ((before (map-state map)) (e (condition-from (lambda () (cxtm map))))) (check "serialization:global-collision-rejected-without-repair" (and (typep e 'topic-maps.cxtm:cxtm-error) (eq :duplicate-item-identifier (topic-maps.cxtm:error-code e)) (equal before (map-state map))))))) (defun manual-original-models () (setf *examples* nil) (dolist (stem '("itemid-tm" "itemid" "itemid-name" "itemid-variant" "itemid-occurrence" "itemid-association" "itemid-role")) (let* ((map (make-topic-map)) (base "https://example.invalid/xtm/model.xtm") (id (concatenate 'string "xtm2/in/" stem ".xtm")) (baseline (concatenate 'string "xtm2/baseline/" stem ".xtm.cxtm"))) ;; Hand-constructed from the original source. This is NOT an XTM reader. (labels ((topic (local) (intern-topic map :item-identifier (concatenate 'string base "#" local))) (identify (item local) (add-item-identifier item (concatenate 'string base "#" local)))) (cond ((string= stem "itemid-tm") (identify map "the-topic-map")) ((string= stem "itemid") (add-item-identifier (topic "topic") "http://example.org/#topic")) ((string= stem "itemid-name") (identify (add-name (topic "topic") "Topic") "the-name")) ((string= stem "itemid-variant") (identify (add-variant (add-name (topic "topic") "Topic") "TOPIC!" :scope (list (topic "command"))) "the-variant")) ((string= stem "itemid-occurrence") (identify (add-occurrence (topic "topic") (topic "occtype") "Testing resource data.") "the-occurrence")) (t (let ((a (make-association map (topic "assoctype") (list (list (topic "roletype") (topic "topic")))))) (if (string= stem "itemid-role") (identify (first (association-roles a)) "the-role") (identify a "the-association")))))) (let* ((before (map-state map)) (actual (topic-maps.cxtm:canonicalize-octets map :base-locator base)) (expected (binary-file (asdf:system-relative-pathname "topic-maps" (concatenate 'string "vendor/cxtm/" baseline))))) (check (concatenate 'string "manual-model:" stem ":original-CXTM-byte-exact") (equalp actual expected)) (check (concatenate 'string "manual-model:" stem ":mutation-free") (equal before (map-state map))) (push (topic-maps.test-foundation::fields "source" id "baseline" baseline "baseIRI" base "contract" "Manual TMDM construction; NOT XTM deserialization" "actualCXTM" (topic-maps.cxtm:canonicalize map :base-locator base) "bytes" (length actual)) *examples*)))) (setf *examples* (nreverse *examples*))) (defun run-tests () (topic-maps.reference.tests:run-tests) (let ((topic-maps.ctm.tests::*checks* nil)) (set-tests) (merge-tests) (boundary-tests) (serialization-tests) (manual-original-models) (check "identifiers:no-imported-contract-status-changes" (equal '(:pass 93 :fail 2 :unsupported 0 :not-run 178) (getf (topic-maps.test-foundation:summarize-corpus topic-maps.ctm.tests::*cxtm-corpus*) :statuses))) (check "identifiers:reification-now-supported" (null (topic-maps.ctm.tests::ctm-condition "a - \"N\" ~r."))) (setf *last-checks* (nreverse topic-maps.ctm.tests::*checks*)) (format t "~&ITEM-IDENTIFIERS-PASS: ~D checks; 7 manually constructed original XTM2 baseline comparisons; no XTM reader or imported status changes.~%" (length *last-checks*)) t)) (defun write-identifier-report (path) (with-open-file (out path :direction :output :if-exists :supersede :if-does-not-exist :create) (topic-maps.test-foundation::emit-json-value (topic-maps.test-foundation::fields "kind" "general-item-identifier-evidence" "passed" (length *last-checks*) "failed" 0 "checks" (topic-maps.test-foundation::json-array *last-checks*) "examples" (topic-maps.test-foundation::json-array *examples*) "orderingCXTM" *ordering-output*) out) (terpri out)))