1 (in-package #:topic-maps.xtm.tests)
3 (defun name-followup-source-checks (root number label)
4 (let* ((topics (topic-maps.xtm::elements root)) (topic (first topics))
5 (names (topic-maps.xtm::elements topic))
6 (kinds (mapcar (lambda (node) (mapcar #'topic-maps.xtm::node-name (topic-maps.xtm::elements node))) names))
7 (expected (ecase number (2 '(("value" "variant") ("value")))
8 (3 '(("itemIdentity" "value") ("value")))
9 (4 '(("value") ("itemIdentity" "value"))))))
10 (check (concatenate 'string label ":original-two-names-exact-child-order-and-properties")
11 (and (= 1 (length topics)) (equal "topic" (topic-maps.xtm::attr topic "id")) (= 2 (length names))
12 (equal expected kinds) (null (topic-maps.xtm::attr root "reifier"))
14 (let ((value (find "value" (topic-maps.xtm::elements node) :key #'topic-maps.xtm::node-name :test #'equal)))
15 (and (equal "name" (topic-maps.xtm::node-name node))
16 (equal "#reifier1" (topic-maps.xtm::attr node "reifier"))
17 (equal '("Topic") (topic-maps.xtm::node-children value))))) names)))
19 (let* ((variant (second (topic-maps.xtm::elements (first names))))
20 (children (topic-maps.xtm::elements variant))
21 (refs (topic-maps.xtm::elements (first children))) (resource (second children)))
22 (check (concatenate 'string label ":original-first-name-variant-resource-and-theme")
23 (and (equal '("scope" "resourceData") (mapcar #'topic-maps.xtm::node-name children))
24 (= 1 (length refs)) (equal "topicRef" (topic-maps.xtm::node-name (first refs)))
25 (equal "#ahwell" (topic-maps.xtm::attr (first refs) "href"))
26 (null (topic-maps.xtm::node-attributes resource))
27 (equal '("topic") (topic-maps.xtm::node-children resource)))))
28 (let* ((index (if (= number 3) 0 1))
29 (identity (first (topic-maps.xtm::elements (nth index names)))))
30 (check (concatenate 'string label ":original-name-item-identifier-position")
31 (and (equal "itemIdentity" (topic-maps.xtm::node-name identity))
32 (equal "#name" (topic-maps.xtm::attr identity "href"))
33 (null (topic-maps.xtm::elements identity))))))
36 (defun name-followup-model-checks (map number label)
37 (let* ((owner (by-id map "topic")) (name (first (topic-names owner)))
38 (r (by-id map "reifier1")) (type (by-si map (default-name-type-identifier)))
39 (theme (when (= number 2) (by-id map "ahwell"))) (variants (all-variants map))
40 (name-iri (concatenate 'string *document* "#name")))
41 (check (concatenate 'string label ":native-one-name-canonical-parent-type-value-scope-eq")
42 (and (= (if (= number 2) 4 3) (length (topic-map-topics map)))
43 (null (topic-map-associations map)) (= 1 (length (name-reifier-names map)))
44 (= 1 (length (topic-names owner))) (eq name (canonical-topic-name name))
45 (eq owner (topic-name-parent name)) (eq type (topic-name-type name))
46 (equal "Topic" (topic-name-value name)) (null (topic-name-scope name))
47 (name-reifier-current-p map)))
48 (check (concatenate 'string label ":native-name-reifier-inverse-eq-distinct-from-map-parent-type")
49 (and (eq r (item-reifier name)) (eq r (canonical-topic r)) (eq name (topic-reified r))
50 (eq map (topic-parent r)) (null (item-reifier map))
51 (null (topic-reified owner)) (null (topic-reified type))))
52 (check (concatenate 'string label ":complete-separated-identifier-sets-and-global-uniqueness")
53 (and (null (item-identifiers map))
54 (equal (list (concatenate 'string *document* "#topic")) (item-identifiers owner))
55 (equal (list (concatenate 'string *document* "#reifier1")) (item-identifiers r))
56 (null (item-identifiers type)) (equal (list (default-name-type-identifier)) (topic-subject-identifiers type))
57 (null (topic-subject-identifiers owner)) (null (topic-subject-identifiers r))
58 (null (topic-subject-locators owner)) (null (topic-subject-locators r)) (null (topic-subject-locators type))
59 (null (topic-names r)) (null (topic-names type))
60 (every (lambda (topic) (and (null (topic-occurrences topic)) (null (topic-roles-played topic)))) (topic-map-topics map))
61 (topic-maps.tmdm::validate-map-item-identifiers map)))
63 (let ((variant (first variants)))
64 (check (concatenate 'string label ":native-variant-preserved-parent-value-datatype-scope-eq")
65 (and (= 1 (length variants)) (= 1 (length (topic-name-variants name)))
66 (eq variant (first (topic-name-variants name))) (eq variant (canonical-variant variant))
67 (eq name (variant-parent variant)) (equal "topic" (variant-value variant))
68 (equal (string-datatype) (variant-datatype variant))
69 (scope-eq-p (variant-scope variant) (list theme)) (eq theme (canonical-topic theme))
70 (equal (list (concatenate 'string *document* "#ahwell")) (item-identifiers theme))
71 (null (topic-subject-identifiers theme)) (null (topic-subject-locators theme))
72 (null (topic-names theme)) (null (topic-reified theme))
73 (null (item-identifiers name)) (null (item-identifiers variant)) (null (item-reifier variant)))))
74 (check (concatenate 'string label ":native-name-only-owner-of-resolved-name-II")
75 (and (null variants) (null (topic-name-variants name))
76 (equal (list name-iri) (item-identifiers name))
77 ;; Reuse the existing query over native information items; no index.
78 (equal (list name) (variant-iid-owners map name-iri)))))))
80 (defun name-followup-order-trace (root primary number label)
81 ;; A separate counterprobe replays the ORIGINAL Name nodes with the existing
82 ;; Reader operations. It never creates or records an additional Case Record.
83 (let* ((topic-maps.xtm::*document-iri* *document*)
84 (topic-maps.xtm::*source-key* (concatenate 'string "local:normalization-trace:" label))
85 (map (make-topic-map)) (topic-node (first (topic-maps.xtm::elements root)))
86 (nodes (topic-maps.xtm::elements topic-node))
87 (owner (intern-topic map :item-identifier (concatenate 'string *document* "#"
88 (topic-maps.xtm::collapse-space (topic-maps.xtm::attr topic-node "id"))))))
89 (topic-maps.xtm::validate-source root)
90 (let* ((first-name (topic-maps.xtm::read-name (first nodes) owner map))
91 (r (item-reifier first-name)) (variant (first (topic-name-variants first-name)))
92 (after-first (name-reifier-model-data map))
93 (name-iri (concatenate 'string *document* "#name")))
94 (check (concatenate 'string label ":trace-after-first-name-II-and-variant-follow-source-order")
95 (and (= 1 (length (name-reifier-names map))) (eq first-name (topic-reified r))
96 (equal (if (= number 3) (list name-iri) nil) (item-identifiers first-name))
97 (= (if (= number 2) 1 0) (length (topic-name-variants first-name)))))
98 (let* ((second-name (topic-maps.xtm::read-name (second nodes) owner map))
99 (current (first (name-reifier-names map))))
100 (check (concatenate 'string label ":trace-both-reader-returns-and-retained-name-reifier-eq")
101 (and (= 1 (length (name-reifier-names map))) (eq first-name second-name)
102 (eq current first-name) (eq current (canonical-topic-name first-name))
103 (eq r (item-reifier current)) (eq r (canonical-topic r)) (eq current (topic-reified r))
105 (and (eq variant (first (topic-name-variants current))) (eq current (variant-parent variant))
106 (null (item-identifiers current)))
107 (equal (list name-iri) (item-identifiers current)))))
108 (check (concatenate 'string label ":trace-native-model-equals-public-original-model")
109 (equalp (name-reifier-model-data primary) (name-reifier-model-data map)))
110 (name-reifier-pure (concatenate 'string label ":trace") map (remove nil (list owner first-name second-name r variant)))
111 (topic-maps.test-foundation::fields "kind" "counterprobe-not-additional-original-registration"
112 "afterFirstName" after-first "afterSecondName" (name-reifier-model-data map)
113 "firstAndSecondNameReturnsEQ" (if (eq first-name second-name) :true :false)
114 "retainedReifierEQ" (if (eq r (item-reifier current)) :true :false)
115 "firstVariantPreservedEQ" (when variant (if (eq variant (first (topic-name-variants current))) :true :false)))))))
117 (defun name-reifier-followup-original-contracts ()
118 (setf *name-followup-outputs* nil)
119 (dolist (spec '((2 397 701) (3 286 497) (4 286 497)))
120 (destructuring-bind (number source-size baseline-size) spec
121 (let* ((stem (format nil "name-duplicate-reified~D" number)) (id (format nil "xtm2/in/~A.xtm" stem))
122 (*document* (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id))
123 (label (concatenate 'string "name-followup-original:" stem))
124 (raw (file-bytes (corpus-path id))) (copy (copy-seq raw))
125 (baseline (corpus-path (format nil "xtm2/baseline/~A.xtm.cxtm" stem))) (expected (file-bytes baseline))
126 (case (topic-maps.test-foundation:make-test-case :id id :category :valid :source-pathname (corpus-path id)
127 :document-iri *document* :expected-outcome :cxtm-equal :expected-baseline baseline
128 :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")))
129 (map nil) (actual nil) (c nil) (trace nil) (parser-calls 0))
130 (setf *cases* (append *cases* (list case)))
133 (let ((saved (symbol-function 'topic-maps.xml:parse-octets)) (same nil))
135 (progn (setf (symbol-function 'topic-maps.xml:parse-octets)
136 (lambda (source handler) (incf parser-calls) (setf same (eq raw source)) (funcall saved source handler)))
137 (setf map (deserialize-octets raw :document-iri *document* :source-key id))
138 (check (concatenate 'string label ":primary-qualified-parser-once-original-bytes-eq")
139 (and (= 1 parser-calls) same)))
140 (setf (symbol-function 'topic-maps.xml:parse-octets) saved)))
141 (let ((root (topic-maps.xml:parse-octets raw (make-instance 'topic-maps.xtm::source-handler))))
142 (name-followup-source-checks root number label)
143 (name-followup-model-checks map number label)
144 (setf trace (name-followup-order-trace root map number label)))
145 (setf actual (name-reifier-pure label map
146 (append (topic-map-topics map) (name-reifier-names map) (all-variants map))))
147 (check (concatenate 'string label ":source-preserved-and-pinned-byte-lengths")
148 (and (equalp raw copy) (= source-size (length raw)) (= baseline-size (length expected))))
149 (let ((other (deserialize-octets raw :document-iri *document* :source-key "local:independent-name-followup")))
150 (check (concatenate 'string label ":source-key-independent-complete-native-model-and-cxtm")
151 (and (equalp (name-reifier-model-data map) (name-reifier-model-data other))
152 (equalp actual (topic-maps.cxtm:canonicalize-octets other :base-locator *document*))))))
153 (error (condition) (setf c condition)))
154 (record-original-result case actual expected c)
155 (push (topic-maps.test-foundation::fields "id" id "documentIRI" *document* "sourceKey" id
156 "sourceBytes" (length raw) "sourceHex" (format nil "~{~2,'0X~}" (coerce raw 'list))
157 "sourceText" (babel:octets-to-string raw :encoding :utf-8) "sourceNameCount" 2
158 "sourceNameIIPositions" (coerce (case number (3 '(1)) (4 '(2))) 'vector)
159 "primaryQualifiedParserCalls" parser-calls "stagedNormalizationTrace" trace
160 "reifierLocator" (concatenate 'string *document* "#reifier1")
161 "model" (when map (name-reifier-model-data map)) "condition" (map-reifier-condition-data c)
162 "actualCXTM" (when actual (babel:octets-to-string actual :encoding :utf-8))
163 "expectedCXTM" (babel:octets-to-string expected :encoding :utf-8)
164 "actualBytes" (when actual (length actual)) "expectedBytes" (length expected)
165 "firstDifferentByte" (when actual (mismatch actual expected))
166 "status" (string-downcase (string (topic-maps.test-foundation:test-case-status case)))) *name-followup-outputs*)
168 (check (concatenate 'string label ":byte-exact") (equalp actual expected)))))
169 (setf *name-followup-outputs* (nreverse *name-followup-outputs*))
170 (check "name-followup-originals:68-unique-records-three-new-positives"
171 (and (= 68 (length *cases*)) (= 60 (count :valid *cases* :key #'topic-maps.test-foundation:test-case-category))
172 (= 68 (length (remove-duplicates *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal)))))
173 (check "name-followup-originals:name-II-order-equivalent-complete-cxtm"
174 (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)))))