(defpackage #:topic-maps.project-work.tests (:use #:cl #:topic-maps.tmdm #:topic-maps.project-work.example) (:import-from #:topic-maps.tests #:check) (:export #:run-tests)) (in-package #:topic-maps.project-work.tests) (defun fields (&rest args) (apply #'topic-maps.test-foundation::fields args)) (defun bytes-hex (bytes) (format nil "~{~2,'0X~}" (coerce bytes 'list))) (defun player (association role-type) (let ((role (find role-type (association-roles association) :key #'association-role-type :test #'eq))) (when role (association-role-player role)))) (defun value-of (map owner-id type-id) (occurrence-value (find (local-topic map type-id) (topic-occurrences (local-topic map owner-id)) :key #'occurrence-type :test #'eq))) (defun run-tests () (let* ((topic-maps.tests::*checks* nil) (path (asdf:system-relative-pathname "topic-maps/project-work-example" "examples/project-work.ctm")) (source-before (read-source path)) (transcript (make-string-output-stream)) (observations nil)) (multiple-value-bind (map before after before-cx after-cx note source held-question original-history) (demo path transcript) (let* ((question (find-topic map "urn:topic-maps:question:Q-XTM-LOOP")) (old-identities (identity-view question)) (history original-history) (cx (lambda () (topic-maps.cxtm:canonicalize-octets map :base-locator *document-iri*)))) (check "project-work:actual-productive-CTM-map" (typep map 'topic-map)) (check "project-work:complete-source-preserved" (and (string= source-before source) (string= source (read-source path)))) (check "project-work:actual-query-identifies-question" (eq question (local-topic map "question"))) (check "project-work:SI-is-not-SL" (null (find-topic map "urn:topic-maps:question:Q-XTM-LOOP" :kind :subject-locator))) (check "project-work:II-contains-explicit-document-IRI" (member (concatenate 'string *document-iri* "#question") (item-identifiers question) :test #'string=)) (check "project-work:existing-SI-intern-resolves-same-native-Topic" (eq question (intern-topic map :subject-identifier "urn:topic-maps:question:Q-XTM-LOOP"))) (check "project-work:existing-SL-intern-resolves-source-Topic" (eq (local-topic map "disposition") (intern-topic map :subject-locator "file:///Users/rgb/workspace/topic-maps/docs/slice-22o-qualified-xtm-corpus-disposition.md"))) (check "project-work:identity-lookups-did-not-change-model" (equal after (model-view map))) ;; The answers are Role players of native Associations, never a second graph. (dolist (edge '(("part-of" "part" "slice" "whole" "project") ("result-of" "result" "commit" "work" "slice") ("result-of" "result" "evidence" "work" "slice") ("concerns" "work" "project" "subject" "question") ("documented-by" "subject" "question" "source" "disposition") ("documented-by" "subject" "slice" "source" "evidence"))) (destructuring-bind (type role-a topic-a role-b topic-b) edge (check (format nil "project-work:native-relationship:~A/~A/~A" type topic-a topic-b) (some (lambda (association) (and (eq (local-topic map type) (association-type association)) (eq (local-topic map topic-a) (player association (local-topic map role-a))) (eq (local-topic map topic-b) (player association (local-topic map role-b))))) (relations (local-topic map topic-a)))))) (check "project-work:native-parents-types-rolesPlayed-and-canonical-EQ" (references-current-p map)) (check "project-work:exercise-note-parent-value-datatype-scope" (and (eq question (occurrence-parent note)) (eq (local-topic map "note") (occurrence-type note)) (string= *exercise-note* (occurrence-value note)) (string= (string-datatype) (occurrence-datatype note)) (equal (list (local-topic map "exercise")) (occurrence-scope note)))) (check "project-work:documented-status-not-overwritten" (some (lambda (item) (and (eq (local-topic map "status") (occurrence-type item)) (string= "offene normative Frage" (occurrence-value item)) (null (occurrence-scope item)))) history)) (check "project-work:one-new-annotation-only" (and (equal (getf before :map-ii) (getf after :map-ii)) (equal (getf before :associations) (getf after :associations)) (= 1 (length (set-difference (getf after :topics) (getf before :topics) :test #'equal))) (= 1 (length (set-difference (getf before :topics) (getf after :topics) :test #'equal))) (= (1+ (length history)) (length (topic-occurrences question))))) (check "project-work:CXTM-changed-with-native-annotation" (and (not (equal before after)) (not (equalp before-cx after-cx)) (mismatch before-cx after-cx))) (let ((held (append (topic-map-topics map) (topic-map-associations map) (mapcan #'association-roles (topic-map-associations map)))) (view (model-view map))) (check "project-work:repeated-CXTM-pure-and-deterministic" (and (equalp after-cx (funcall cx)) (equalp after-cx (funcall cx)) (equal view (model-view map)) (every (lambda (item) (typecase item (topic (eq item (canonical-topic item))) (association (eq item (canonical-association item))) (association-role (eq item (canonical-association-role item))))) held)))) (let* ((duplicate (add-exercise-note map)) (canonical (canonical-occurrence note))) (check "project-work:equal-Occurrences-normalize-with-resolvable-old-handle" (and (eq canonical duplicate) (eq question (occurrence-parent canonical)) (= 1 (count (local-topic map "note") (topic-occurrences question) :key #'occurrence-type :test #'eq))))) (check "project-work:equal-edit-has-no-second-CXTM-change" (equalp after-cx (funcall cx))) (check "project-work:Topic-EQ-and-identifiers-survive-edit" (and (eq held-question question) (eq question (local-topic map "question")) (equal old-identities (identity-view question)) (every (lambda (item) (and (eq item (canonical-occurrence item)) (member item (topic-occurrences question) :test #'eq))) history))) (normalize-topic-map map) (check "project-work:public-normalization-is-idempotent" (and (equal after (model-view map)) (equalp after-cx (funcall cx)))) (let ((view (model-view map)) (code nil) (foreign-type (intern-topic (make-topic-map) :subject-identifier "urn:exercise:foreign-type"))) (handler-case (add-occurrence question foreign-type "invalid exercise") (occurrence-error (c) (setf code (occurrence-error-code c)))) (check "project-work:foreign-Type-rejected-before-mutation" (and (eq :foreign-type code) (equal view (model-view map)) (equalp after-cx (funcall cx))))) (multiple-value-bind (fresh fresh-source) (load-map path :source-key "another diagnostic key") (check "project-work:reparse-unchanged-CTM-restores-original-state" (and (not (eq map fresh)) (not (eq question (local-topic fresh "question"))) (string= source fresh-source) (equal before (model-view fresh)) (equalp before-cx (topic-maps.cxtm:canonicalize-octets fresh :base-locator *document-iri*)) (references-current-p fresh)))) (check "project-work:no-CTM-source-write-or-implicit-persistence" (string= source-before (read-source path))) (setf observations (fields "documentIRI" *document-iri* "source" source "beforeNativeView" (write-to-string before :readably t) "afterNativeView" (write-to-string after :readably t) "beforeCXTMHex" (bytes-hex before-cx) "afterCXTMHex" (bytes-hex after-cx) "beforeBytes" (length before-cx) "afterBytes" (length after-cx) "firstDifferentByteZeroBased" (mismatch before-cx after-cx) "TopicCountBeforeAndAfter" (length (topic-map-topics map)) "associationCountBeforeAndAfter" (length (topic-map-associations map)) "questionIdentity" (write-to-string old-identities :readably t) "exerciseValue" (occurrence-value (canonical-occurrence note)) "exerciseDatatype" (occurrence-datatype (canonical-occurrence note)) "exerciseScope" (write-to-string (mapcar #'identity-view (occurrence-scope (canonical-occurrence note))) :readably t) "oldOccurrenceHandleEQCanonicalAfterDuplicate" (if (eq note (canonical-occurrence note)) :true :false) "nativeReferencesCurrent" (if (references-current-p map) :true :false) "reparsedOriginalMatchesBefore" :true "sourceUnchanged" :true "historicalFactsFromNativeItems" (fields "questionStatus" (value-of map "question" "status") "sliceStatus" (value-of map "slice" "status") "commitHash" (value-of map "commit" "hash") "evidenceSummary" (value-of map "evidence" "result") "disposition" (value-of map "disposition" "result") "questionSource" (value-of map "question" "source") "sliceSource" (value-of map "slice" "source") "evidenceDocument" (first (topic-subject-locators (local-topic map "evidence")) ) ) "transcript" (get-output-stream-string transcript))))) (let* ((checks (reverse topic-maps.tests::*checks*)) (prefix (uiop:getenv "TOPIC_MAPS_REPORT"))) (when prefix (with-open-file (out (concatenate 'string prefix ".project-work.json") :direction :output :if-exists :supersede) (topic-maps.test-foundation::emit-json-value (fields "kind" "native-project-work-example-not-corpus-registration" "checks" (coerce checks 'vector) "observation" observations "productionChanges" :false "originalRegistrationsAdded" 0) out) (terpri out))) (format t "PROJECT-WORK-PASS: ~D checks~%" (length checks)) (length checks))))