Created
October 25, 2025 14:20
-
-
Save CFiggers/fded023ccab207ba5bcd2b29792c95bb to your computer and use it in GitHub Desktop.
checkout.janet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (use judge) | |
| (use ./dataclasses) | |
| (use ./deep-clone) | |
| (dataclass User | |
| :name :string | |
| :permissions :array) | |
| (def Nobody (user :name "" :permissions @[])) | |
| (dataclass Document | |
| :title :string | |
| :owner [User Nobody] | |
| :req-perm :array | |
| :derivation :number | |
| {:derive |(++ ($ :derivation))}) | |
| (def Nothing (document :title "" :owner Nobody :req-perm @[] :derivation 0)) | |
| ######### | |
| (defn has-perm? [user document] | |
| (assert-user user) | |
| (assert-document document) | |
| (any? (seq [p :in (user :permissions) r :in (document :req-perm)] (= p r)))) | |
| (defn nobody-owns [document] | |
| (assert-document document) | |
| (= (document :owner) Nobody)) | |
| (defn checkout [user document] | |
| (assert-user user) | |
| (assert-document document) | |
| (def ret (deep-clone document)) | |
| (when (and (has-perm? user document) (nobody-owns document)) | |
| (:derive (put ret :owner user))) | |
| (assert-document ret)) | |
| (defn checkin [user document] | |
| (assert-user user) | |
| (assert-document document) | |
| (def ret (deep-clone document)) | |
| (when (= user (document :owner)) | |
| (:derive (put ret :owner Nobody))) | |
| (assert-document ret)) | |
| ######### | |
| (test (assert-user Nobody) @{:name "" :permissions @[]}) | |
| (test-error (try (assert-user @{}) ([_] (error "Errored"))) "Errored") | |
| (test (assert-document Nothing) | |
| @{:derivation 0 | |
| :owner @{:name "" :permissions @[]} | |
| :req-perm @[] | |
| :title ""}) | |
| (test-error (try (assert-document @{}) ([_] (error "Errored"))) "Errored") | |
| ######### | |
| (test (has-perm? | |
| (user :name "Alice" :permissions @["read" "write"]) | |
| (document :title "Doc1" :owner Nobody :req-perm @["read"] :derivation 1)) | |
| true) | |
| (test (has-perm? | |
| (user :name "Bob" :permissions @["write"]) | |
| (document :title "Doc1" :owner Nobody :req-perm @["read"] :derivation 1)) | |
| false) | |
| (test (has-perm? | |
| (user :name "Alice" :permissions @["admin" "user"]) | |
| (document :title "todos.txt" :owner Nobody :req-perm @["user" "admin"] :derivation 0)) | |
| true) | |
| ######### | |
| (deftest-type users-and-docs | |
| :setup (fn [] | |
| {:root (user :name "root" :permissions @["admin"]) | |
| :alice (user :name "Alice" :permissions @["admin" "user"]) | |
| :bob (user :name "Bob" :permissions @["user"]) | |
| :passwords (document :title "passwords.txt" :owner Nobody :req-perm @["admin"] :derivation 0) | |
| :todos (document :title "todos.txt" :owner Nobody :req-perm @["user"] :derivation 0)}) | |
| :reset (fn [context] context) | |
| :teardown (fn [context])) | |
| (deftest: users-and-docs "checking test type" [context] | |
| (def {:root root :alice alice :bob bob | |
| :passwords passwords :todos todos} context) | |
| (test (assert-user root) @{:name "root" :permissions @["admin"]}) | |
| (test (assert-user alice) | |
| @{:name "Alice" | |
| :permissions @["admin" "user"]}) | |
| (test (assert-user bob) @{:name "Bob" :permissions @["user"]}) | |
| (test (assert-document passwords) | |
| @{:derivation 0 | |
| :owner @{:name "" :permissions @[]} | |
| :req-perm @["admin"] | |
| :title "passwords.txt"}) | |
| (test (assert-document todos) | |
| @{:derivation 0 | |
| :owner @{:name "" :permissions @[]} | |
| :req-perm @["user"] | |
| :title "todos.txt"})) | |
| (deftest: users-and-docs "checkout" [context] | |
| (def {:root root :alice alice :bob bob | |
| :passwords passwords :todos todos} context) | |
| (test (checkout root passwords) | |
| @{:derivation 1 | |
| :owner @{:name "root" :permissions @["admin"]} | |
| :req-perm @["admin"] | |
| :title "passwords.txt"}) | |
| (test (checkout root todos) | |
| @{:derivation 0 | |
| :owner @{:name "" :permissions @[]} | |
| :req-perm @["user"] | |
| :title "todos.txt"}) | |
| (test (checkout alice passwords) | |
| @{:derivation 1 | |
| :owner @{:name "Alice" | |
| :permissions @["admin" "user"]} | |
| :req-perm @["admin"] | |
| :title "passwords.txt"}) | |
| (test (checkout alice todos) | |
| @{:derivation 1 | |
| :owner @{:name "Alice" | |
| :permissions @["admin" "user"]} | |
| :req-perm @["user"] | |
| :title "todos.txt"}) | |
| (test (checkout bob passwords) | |
| @{:derivation 0 | |
| :owner @{:name "" :permissions @[]} | |
| :req-perm @["admin"] | |
| :title "passwords.txt"}) | |
| (test (checkout bob todos) | |
| @{:derivation 1 | |
| :owner @{:name "Bob" :permissions @["user"]} | |
| :req-perm @["user"] | |
| :title "todos.txt"}) | |
| (test (->> todos (checkout bob) (checkout alice)) | |
| @{:derivation 1 | |
| :owner @{:name "Bob" :permissions @["user"]} | |
| :req-perm @["user"] | |
| :title "todos.txt"})) | |
| ######### | |
| (deftest: users-and-docs "checkin" [context] | |
| (def {:root root :alice alice :bob bob | |
| :passwords passwords :todos todos} context) | |
| (test (->> todos (checkout bob) (checkin bob) (checkout alice)) | |
| @{:derivation 3 | |
| :owner @{:name "Alice" | |
| :permissions @["admin" "user"]} | |
| :req-perm @["user"] | |
| :title "todos.txt"})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment