(in-package #:topic-maps.xtm.tests) (defun name-followup-source-checks (root number label) (let* ((topics (topic-maps.xtm::elements root)) (topic (first topics)) (names (topic-maps.xtm::elements topic)) (kinds (mapcar (lambda (node) (mapcar #'topic-maps.xtm::node-name (topic-maps.xtm::elements node))) names)) (expected (ecase number (2 '(("value" "variant") ("value"))) (3 '(("itemIdentity" "value") ("value"))) (4 '(("value") ("itemIdentity" "value")))))) (check (concatenate 'string label ":original-two-names-exact-child-order-and-properties") (and (= 1 (length topics)) (equal "topic" (topic-maps.xtm::attr topic "id")) (= 2 (length names)) (equal expected kinds) (null (topic-maps.xtm::attr root "reifier")) (every (lambda (node) (let ((value (find "value" (topic-maps.xtm::elements node) :key #'topic-maps.xtm::node-name :test #'equal))) (and (equal "name" (topic-maps.xtm::node-name node)) (equal "#reifier1" (topic-maps.xtm::attr node "reifier")) (equal '("Topic") (topic-maps.xtm::node-children value))))) names))) (if (= number 2) (let* ((variant (second (topic-maps.xtm::elements (first names)))) (children (topic-maps.xtm::elements variant)) (refs (topic-maps.xtm::elements (first children))) (resource (second children))) (check (concatenate 'string label ":original-first-name-variant-resource-and-theme") (and (equal '("scope" "resourceData") (mapcar #'topic-maps.xtm::node-name children)) (= 1 (length refs)) (equal "topicRef" (topic-maps.xtm::node-name (first refs))) (equal "#ahwell" (topic-maps.xtm::attr (first refs) "href")) (null (topic-maps.xtm::node-attributes resource)) (equal '("topic") (topic-maps.xtm::node-children resource))))) (let* ((index (if (= number 3) 0 1)) (identity (first (topic-maps.xtm::elements (nth index names))))) (check (concatenate 'string label ":original-name-item-identifier-position") (and (equal "itemIdentity" (topic-maps.xtm::node-name identity)) (equal "#name" (topic-maps.xtm::attr identity "href")) (null (topic-maps.xtm::elements identity)))))) names)) (defun name-followup-model-checks (map number label) (let* ((owner (by-id map "topic")) (name (first (topic-names owner))) (r (by-id map "reifier1")) (type (by-si map (default-name-type-identifier))) (theme (when (= number 2) (by-id map "ahwell"))) (variants (all-variants map)) (name-iri (concatenate 'string *document* "#name"))) (check (concatenate 'string label ":native-one-name-canonical-parent-type-value-scope-eq") (and (= (if (= number 2) 4 3) (length (topic-map-topics map))) (null (topic-map-associations map)) (= 1 (length (name-reifier-names map))) (= 1 (length (topic-names owner))) (eq name (canonical-topic-name name)) (eq owner (topic-name-parent name)) (eq type (topic-name-type name)) (equal "Topic" (topic-name-value name)) (null (topic-name-scope name)) (name-reifier-current-p map))) (check (concatenate 'string label ":native-name-reifier-inverse-eq-distinct-from-map-parent-type") (and (eq r (item-reifier name)) (eq r (canonical-topic r)) (eq name (topic-reified r)) (eq map (topic-parent r)) (null (item-reifier map)) (null (topic-reified owner)) (null (topic-reified type)))) (check (concatenate 'string label ":complete-separated-identifier-sets-and-global-uniqueness") (and (null (item-identifiers map)) (equal (list (concatenate 'string *document* "#topic")) (item-identifiers owner)) (equal (list (concatenate 'string *document* "#reifier1")) (item-identifiers r)) (null (item-identifiers type)) (equal (list (default-name-type-identifier)) (topic-subject-identifiers type)) (null (topic-subject-identifiers owner)) (null (topic-subject-identifiers r)) (null (topic-subject-locators owner)) (null (topic-subject-locators r)) (null (topic-subject-locators type)) (null (topic-names r)) (null (topic-names type)) (every (lambda (topic) (and (null (topic-occurrences topic)) (null (topic-roles-played topic)))) (topic-map-topics map)) (topic-maps.tmdm::validate-map-item-identifiers map))) (if (= number 2) (let ((variant (first variants))) (check (concatenate 'string label ":native-variant-preserved-parent-value-datatype-scope-eq") (and (= 1 (length variants)) (= 1 (length (topic-name-variants name))) (eq variant (first (topic-name-variants name))) (eq variant (canonical-variant variant)) (eq name (variant-parent variant)) (equal "topic" (variant-value variant)) (equal (string-datatype) (variant-datatype variant)) (scope-eq-p (variant-scope variant) (list theme)) (eq theme (canonical-topic theme)) (equal (list (concatenate 'string *document* "#ahwell")) (item-identifiers theme)) (null (topic-subject-identifiers theme)) (null (topic-subject-locators theme)) (null (topic-names theme)) (null (topic-reified theme)) (null (item-identifiers name)) (null (item-identifiers variant)) (null (item-reifier variant))))) (check (concatenate 'string label ":native-name-only-owner-of-resolved-name-II") (and (null variants) (null (topic-name-variants name)) (equal (list name-iri) (item-identifiers name)) ;; Reuse the existing query over native information items; no index. (equal (list name) (variant-iid-owners map name-iri))))))) (defun name-followup-order-trace (root primary number label) ;; A separate counterprobe replays the ORIGINAL Name nodes with the existing ;; Reader operations. It never creates or records an additional Case Record. (let* ((topic-maps.xtm::*document-iri* *document*) (topic-maps.xtm::*source-key* (concatenate 'string "local:normalization-trace:" label)) (map (make-topic-map)) (topic-node (first (topic-maps.xtm::elements root))) (nodes (topic-maps.xtm::elements topic-node)) (owner (intern-topic map :item-identifier (concatenate 'string *document* "#" (topic-maps.xtm::collapse-space (topic-maps.xtm::attr topic-node "id")))))) (topic-maps.xtm::validate-source root) (let* ((first-name (topic-maps.xtm::read-name (first nodes) owner map)) (r (item-reifier first-name)) (variant (first (topic-name-variants first-name))) (after-first (name-reifier-model-data map)) (name-iri (concatenate 'string *document* "#name"))) (check (concatenate 'string label ":trace-after-first-name-II-and-variant-follow-source-order") (and (= 1 (length (name-reifier-names map))) (eq first-name (topic-reified r)) (equal (if (= number 3) (list name-iri) nil) (item-identifiers first-name)) (= (if (= number 2) 1 0) (length (topic-name-variants first-name))))) (let* ((second-name (topic-maps.xtm::read-name (second nodes) owner map)) (current (first (name-reifier-names map)))) (check (concatenate 'string label ":trace-both-reader-returns-and-retained-name-reifier-eq") (and (= 1 (length (name-reifier-names map))) (eq first-name second-name) (eq current first-name) (eq current (canonical-topic-name first-name)) (eq r (item-reifier current)) (eq r (canonical-topic r)) (eq current (topic-reified r)) (if (= number 2) (and (eq variant (first (topic-name-variants current))) (eq current (variant-parent variant)) (null (item-identifiers current))) (equal (list name-iri) (item-identifiers current))))) (check (concatenate 'string label ":trace-native-model-equals-public-original-model") (equalp (name-reifier-model-data primary) (name-reifier-model-data map))) (name-reifier-pure (concatenate 'string label ":trace") map (remove nil (list owner first-name second-name r variant))) (topic-maps.test-foundation::fields "kind" "counterprobe-not-additional-original-registration" "afterFirstName" after-first "afterSecondName" (name-reifier-model-data map) "firstAndSecondNameReturnsEQ" (if (eq first-name second-name) :true :false) "retainedReifierEQ" (if (eq r (item-reifier current)) :true :false) "firstVariantPreservedEQ" (when variant (if (eq variant (first (topic-name-variants current))) :true :false))))))) (defun name-reifier-followup-original-contracts () (setf *name-followup-outputs* nil) (dolist (spec '((2 397 701) (3 286 497) (4 286 497))) (destructuring-bind (number source-size baseline-size) spec (let* ((stem (format nil "name-duplicate-reified~D" number)) (id (format nil "xtm2/in/~A.xtm" stem)) (*document* (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id)) (label (concatenate 'string "name-followup-original:" stem)) (raw (file-bytes (corpus-path id))) (copy (copy-seq raw)) (baseline (corpus-path (format nil "xtm2/baseline/~A.xtm.cxtm" stem))) (expected (file-bytes baseline)) (case (topic-maps.test-foundation:make-test-case :id id :category :valid :source-pathname (corpus-path id) :document-iri *document* :expected-outcome :cxtm-equal :expected-baseline baseline :clauses '("XTM 2.0 4.3.2,4.6,4.10,4.12,4.13,4.16" "TMDM 5.4,5.5,6.3,6.4" "CXTM 3,4"))) (map nil) (actual nil) (c nil) (trace nil) (parser-calls 0)) (setf *cases* (append *cases* (list case))) (handler-case (progn (let ((saved (symbol-function 'topic-maps.xml:parse-octets)) (same nil)) (unwind-protect (progn (setf (symbol-function 'topic-maps.xml:parse-octets) (lambda (source handler) (incf parser-calls) (setf same (eq raw source)) (funcall saved source handler))) (setf map (deserialize-octets raw :document-iri *document* :source-key id)) (check (concatenate 'string label ":primary-qualified-parser-once-original-bytes-eq") (and (= 1 parser-calls) same))) (setf (symbol-function 'topic-maps.xml:parse-octets) saved))) (let ((root (topic-maps.xml:parse-octets raw (make-instance 'topic-maps.xtm::source-handler)))) (name-followup-source-checks root number label) (name-followup-model-checks map number label) (setf trace (name-followup-order-trace root map number label))) (setf actual (name-reifier-pure label map (append (topic-map-topics map) (name-reifier-names map) (all-variants map)))) (check (concatenate 'string label ":source-preserved-and-pinned-byte-lengths") (and (equalp raw copy) (= source-size (length raw)) (= baseline-size (length expected)))) (let ((other (deserialize-octets raw :document-iri *document* :source-key "local:independent-name-followup"))) (check (concatenate 'string label ":source-key-independent-complete-native-model-and-cxtm") (and (equalp (name-reifier-model-data map) (name-reifier-model-data other)) (equalp actual (topic-maps.cxtm:canonicalize-octets other :base-locator *document*)))))) (error (condition) (setf c condition))) (record-original-result case actual expected c) (push (topic-maps.test-foundation::fields "id" id "documentIRI" *document* "sourceKey" id "sourceBytes" (length raw) "sourceHex" (format nil "~{~2,'0X~}" (coerce raw 'list)) "sourceText" (babel:octets-to-string raw :encoding :utf-8) "sourceNameCount" 2 "sourceNameIIPositions" (coerce (case number (3 '(1)) (4 '(2))) 'vector) "primaryQualifiedParserCalls" parser-calls "stagedNormalizationTrace" trace "reifierLocator" (concatenate 'string *document* "#reifier1") "model" (when map (name-reifier-model-data map)) "condition" (map-reifier-condition-data c) "actualCXTM" (when actual (babel:octets-to-string actual :encoding :utf-8)) "expectedCXTM" (babel:octets-to-string expected :encoding :utf-8) "actualBytes" (when actual (length actual)) "expectedBytes" (length expected) "firstDifferentByte" (when actual (mismatch actual expected)) "status" (string-downcase (string (topic-maps.test-foundation:test-case-status case)))) *name-followup-outputs*) (when c (error c)) (check (concatenate 'string label ":byte-exact") (equalp actual expected))))) (setf *name-followup-outputs* (nreverse *name-followup-outputs*)) (check "name-followup-originals:68-unique-records-three-new-positives" (and (= 68 (length *cases*)) (= 60 (count :valid *cases* :key #'topic-maps.test-foundation:test-case-category)) (= 68 (length (remove-duplicates *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal))))) (check "name-followup-originals:name-II-order-equivalent-complete-cxtm" (equalp (cdr (assoc "actualCXTM" (topic-maps.test-foundation::json-object-members (second *name-followup-outputs*)) :test #'equal)) (cdr (assoc "actualCXTM" (topic-maps.test-foundation::json-object-members (third *name-followup-outputs*)) :test #'equal)))))