]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/project-work.lisp
examples: explore project work through native Topic Maps (slice 22U)
[topic-maps.git] / tests / project-work.lisp
1 (defpackage #:topic-maps.project-work.tests
2 (:use #:cl #:topic-maps.tmdm #:topic-maps.project-work.example)
3 (:import-from #:topic-maps.tests #:check)
4 (:export #:run-tests))
5 (in-package #:topic-maps.project-work.tests)
6 (defun fields (&rest args) (apply #'topic-maps.test-foundation::fields args))
7 (defun bytes-hex (bytes) (format nil "~{~2,'0X~}" (coerce bytes 'list)))
8 (defun player (association role-type)
9 (let ((role (find role-type (association-roles association) :key #'association-role-type :test #'eq)))
10 (when role (association-role-player role))))
11 (defun value-of (map owner-id type-id)
12 (occurrence-value (find (local-topic map type-id) (topic-occurrences (local-topic map owner-id))
13 :key #'occurrence-type :test #'eq)))
14 (defun run-tests ()
15 (let* ((topic-maps.tests::*checks* nil)
16 (path (asdf:system-relative-pathname "topic-maps/project-work-example" "examples/project-work.ctm"))
17 (source-before (read-source path)) (transcript (make-string-output-stream))
18 (observations nil))
19 (multiple-value-bind (map before after before-cx after-cx note source held-question original-history) (demo path transcript)
20 (let* ((question (find-topic map "urn:topic-maps:question:Q-XTM-LOOP"))
21 (old-identities (identity-view question))
22 (history original-history)
23 (cx (lambda () (topic-maps.cxtm:canonicalize-octets map :base-locator *document-iri*))))
24 (check "project-work:actual-productive-CTM-map" (typep map 'topic-map))
25 (check "project-work:complete-source-preserved" (and (string= source-before source) (string= source (read-source path))))
26 (check "project-work:actual-query-identifies-question" (eq question (local-topic map "question")))
27 (check "project-work:SI-is-not-SL" (null (find-topic map "urn:topic-maps:question:Q-XTM-LOOP" :kind :subject-locator)))
28 (check "project-work:II-contains-explicit-document-IRI"
29 (member (concatenate 'string *document-iri* "#question") (item-identifiers question) :test #'string=))
30 (check "project-work:existing-SI-intern-resolves-same-native-Topic"
31 (eq question (intern-topic map :subject-identifier "urn:topic-maps:question:Q-XTM-LOOP")))
32 (check "project-work:existing-SL-intern-resolves-source-Topic"
33 (eq (local-topic map "disposition")
34 (intern-topic map :subject-locator "file:///Users/rgb/workspace/topic-maps/docs/slice-22o-qualified-xtm-corpus-disposition.md")))
35 (check "project-work:identity-lookups-did-not-change-model" (equal after (model-view map)))
36 ;; The answers are Role players of native Associations, never a second graph.
37 (dolist (edge '(("part-of" "part" "slice" "whole" "project")
38 ("result-of" "result" "commit" "work" "slice")
39 ("result-of" "result" "evidence" "work" "slice")
40 ("concerns" "work" "project" "subject" "question")
41 ("documented-by" "subject" "question" "source" "disposition")
42 ("documented-by" "subject" "slice" "source" "evidence")))
43 (destructuring-bind (type role-a topic-a role-b topic-b) edge
44 (check (format nil "project-work:native-relationship:~A/~A/~A" type topic-a topic-b)
45 (some (lambda (association)
46 (and (eq (local-topic map type) (association-type association))
47 (eq (local-topic map topic-a) (player association (local-topic map role-a)))
48 (eq (local-topic map topic-b) (player association (local-topic map role-b)))))
49 (relations (local-topic map topic-a))))))
50 (check "project-work:native-parents-types-rolesPlayed-and-canonical-EQ" (references-current-p map))
51 (check "project-work:exercise-note-parent-value-datatype-scope"
52 (and (eq question (occurrence-parent note)) (eq (local-topic map "note") (occurrence-type note))
53 (string= *exercise-note* (occurrence-value note)) (string= (string-datatype) (occurrence-datatype note))
54 (equal (list (local-topic map "exercise")) (occurrence-scope note))))
55 (check "project-work:documented-status-not-overwritten"
56 (some (lambda (item) (and (eq (local-topic map "status") (occurrence-type item))
57 (string= "offene normative Frage" (occurrence-value item)) (null (occurrence-scope item)))) history))
58 (check "project-work:one-new-annotation-only"
59 (and (equal (getf before :map-ii) (getf after :map-ii))
60 (equal (getf before :associations) (getf after :associations))
61 (= 1 (length (set-difference (getf after :topics) (getf before :topics) :test #'equal)))
62 (= 1 (length (set-difference (getf before :topics) (getf after :topics) :test #'equal)))
63 (= (1+ (length history)) (length (topic-occurrences question)))))
64 (check "project-work:CXTM-changed-with-native-annotation"
65 (and (not (equal before after)) (not (equalp before-cx after-cx)) (mismatch before-cx after-cx)))
66 (let ((held (append (topic-map-topics map) (topic-map-associations map)
67 (mapcan #'association-roles (topic-map-associations map))))
68 (view (model-view map)))
69 (check "project-work:repeated-CXTM-pure-and-deterministic"
70 (and (equalp after-cx (funcall cx)) (equalp after-cx (funcall cx))
71 (equal view (model-view map))
72 (every (lambda (item) (typecase item
73 (topic (eq item (canonical-topic item)))
74 (association (eq item (canonical-association item)))
75 (association-role (eq item (canonical-association-role item))))) held))))
76 (let* ((duplicate (add-exercise-note map)) (canonical (canonical-occurrence note)))
77 (check "project-work:equal-Occurrences-normalize-with-resolvable-old-handle"
78 (and (eq canonical duplicate) (eq question (occurrence-parent canonical))
79 (= 1 (count (local-topic map "note") (topic-occurrences question) :key #'occurrence-type :test #'eq)))))
80 (check "project-work:equal-edit-has-no-second-CXTM-change" (equalp after-cx (funcall cx)))
81 (check "project-work:Topic-EQ-and-identifiers-survive-edit"
82 (and (eq held-question question) (eq question (local-topic map "question")) (equal old-identities (identity-view question))
83 (every (lambda (item) (and (eq item (canonical-occurrence item))
84 (member item (topic-occurrences question) :test #'eq))) history)))
85 (normalize-topic-map map)
86 (check "project-work:public-normalization-is-idempotent" (and (equal after (model-view map)) (equalp after-cx (funcall cx))))
87 (let ((view (model-view map)) (code nil)
88 (foreign-type (intern-topic (make-topic-map) :subject-identifier "urn:exercise:foreign-type")))
89 (handler-case (add-occurrence question foreign-type "invalid exercise")
90 (occurrence-error (c) (setf code (occurrence-error-code c))))
91 (check "project-work:foreign-Type-rejected-before-mutation"
92 (and (eq :foreign-type code) (equal view (model-view map)) (equalp after-cx (funcall cx)))))
93 (multiple-value-bind (fresh fresh-source) (load-map path :source-key "another diagnostic key")
94 (check "project-work:reparse-unchanged-CTM-restores-original-state"
95 (and (not (eq map fresh)) (not (eq question (local-topic fresh "question")))
96 (string= source fresh-source) (equal before (model-view fresh))
97 (equalp before-cx (topic-maps.cxtm:canonicalize-octets fresh :base-locator *document-iri*))
98 (references-current-p fresh))))
99 (check "project-work:no-CTM-source-write-or-implicit-persistence" (string= source-before (read-source path)))
100 (setf observations (fields "documentIRI" *document-iri* "source" source
101 "beforeNativeView" (write-to-string before :readably t) "afterNativeView" (write-to-string after :readably t)
102 "beforeCXTMHex" (bytes-hex before-cx) "afterCXTMHex" (bytes-hex after-cx)
103 "beforeBytes" (length before-cx) "afterBytes" (length after-cx)
104 "firstDifferentByteZeroBased" (mismatch before-cx after-cx)
105 "TopicCountBeforeAndAfter" (length (topic-map-topics map))
106 "associationCountBeforeAndAfter" (length (topic-map-associations map))
107 "questionIdentity" (write-to-string old-identities :readably t)
108 "exerciseValue" (occurrence-value (canonical-occurrence note))
109 "exerciseDatatype" (occurrence-datatype (canonical-occurrence note))
110 "exerciseScope" (write-to-string (mapcar #'identity-view (occurrence-scope (canonical-occurrence note))) :readably t)
111 "oldOccurrenceHandleEQCanonicalAfterDuplicate" (if (eq note (canonical-occurrence note)) :true :false)
112 "nativeReferencesCurrent" (if (references-current-p map) :true :false)
113 "reparsedOriginalMatchesBefore" :true "sourceUnchanged" :true
114 "historicalFactsFromNativeItems" (fields
115 "questionStatus" (value-of map "question" "status")
116 "sliceStatus" (value-of map "slice" "status")
117 "commitHash" (value-of map "commit" "hash")
118 "evidenceSummary" (value-of map "evidence" "result")
119 "disposition" (value-of map "disposition" "result")
120 "questionSource" (value-of map "question" "source")
121 "sliceSource" (value-of map "slice" "source")
122 "evidenceDocument" (first (topic-subject-locators (local-topic map "evidence")) ) )
123 "transcript" (get-output-stream-string transcript)))))
124 (let* ((checks (reverse topic-maps.tests::*checks*)) (prefix (uiop:getenv "TOPIC_MAPS_REPORT")))
125 (when prefix
126 (with-open-file (out (concatenate 'string prefix ".project-work.json") :direction :output :if-exists :supersede)
127 (topic-maps.test-foundation::emit-json-value
128 (fields "kind" "native-project-work-example-not-corpus-registration"
129 "checks" (coerce checks 'vector) "observation" observations
130 "productionChanges" :false "originalRegistrationsAdded" 0) out)
131 (terpri out)))
132 (format t "PROJECT-WORK-PASS: ~D checks~%" (length checks))
133 (length checks))))