cosmic_applet_avestruz/guide/testing.rs
1// SPDX-License-Identifier: MIT
2
3//! How it is tested, what is not, and every test by name. Documentation
4//! only.
5//!
6//! 1. [How much](#how-much)
7//! 2. [Where the tests are](#where-the-tests-are)
8//! 3. [What a test is named](#what-a-test-is-named)
9//! 4. [What the tests stand on](#what-the-tests-stand-on)
10//! 5. [What `just check` holds to](#what-just-check-holds-to)
11//! 6. [Fuzzing](#fuzzing)
12//! 7. [What is not tested](#what-is-not-tested)
13//! 8. [Trying it by hand](#trying-it-by-hand)
14//! 9. [Every test](#every-test)
15//!
16//! # How much
17//!
18#![doc = include_str!(concat!(env!("OUT_DIR"), "/test_summary.md"))]
19//! The number is counted from the sources when the crate is built (see
20//! `build.rs`), so it is the number there are. Two of them are held
21//! against the real system keyring, with throwaway keys that are taken
22//! out again however the test ends: they run wherever a keyring answers,
23//! and have nothing to hold to where none does.
24//!
25//! ```sh
26//! just test # all of them
27//! just check # everything else that is held to: see below
28//! just coverage # which lines the tests run, as pages (cargo-llvm-cov)
29//! just fuzz # what reads untrusted input, fed whatever a fuzzer makes up
30//! ```
31//!
32//! # Where the tests are
33//!
34//! Every test is beside the code it tests, in a `tests` module at the
35//! foot of the file. There is no `tests/` directory: what is tested is
36//! mostly not public in any sense that matters, and a test beside the
37//! code can reach what is private to it.
38//!
39//! ```mermaid
40//! flowchart TB
41//! subgraph pure["Pure: something in, something out"]
42//! events["Events made and read: compose, content, thread, feed, bookmarks, contacts, relay lists, drafts"]
43//! choosing["Choosing: which relays, which rank, what was typed"]
44//! layout["Layout: row heights, the virtual list, what a picture takes"]
45//! end
46//! subgraph inprocess["Against something real, inside the test process"]
47//! relays["The relay client, against relays run in the test"]
48//! signer["Signing in, against a remote signer run in the test"]
49//! lmdb["The event store, in a directory of its own"]
50//! end
51//! subgraph outside["Against the machine"]
52//! keyring["The keyring round trip: wherever a keyring answers"]
53//! end
54//! pure --> inprocess --> outside
55//! ```
56//!
57//! Most tests are of the first kind, because most of the code is: the
58//! modules that make and read events do no I/O (see
59//! [`crate::guide::protocol`]), so they are tested by giving them an event
60//! and looking at what comes out.
61//!
62//! | What | How it is tested | Where |
63//! |---|---|---|
64//! | Making events: notes, replies, comments, quotes, lists, drafts | The unsigned event is built and its tags looked at | [`crate::nostr::compose`], [`crate::nostr::contacts`], [`crate::nostr::bookmarks`], [`crate::nostr::relay_lists`], [`crate::nostr::kept_drafts`] |
65//! | Attaching a file: what a picture's file is left with, the server list, the authorization | Files are put together in the test with everything a camera and an editor leave in them, made bare, and decoded | [`crate::media`], [`crate::nostr::blossom`] |
66//! | Sending a file to media servers | Servers that listen on this machine, each behaving its own way: taking the file, refusing it, not knowing the question, reading the authorization the old way, changing the file | [`crate::nostr::blossom`] |
67//! | Reading events: text taken apart, threads put in order, counts | An event is made and signed in the test, and read | [`crate::nostr::content`], [`crate::nostr::models`], [`crate::nostr::thread`], [`crate::nostr::engagement`] |
68//! | Lists that stay put: what waits, what joins up, what leaves a hole | Pages are offered to a list, and what it shows looked at | [`crate::nostr::feed`] |
69//! | Which relays are asked | The plan for given people and lists | [`crate::nostr::outbox`] |
70//! | The relay client: reading, publishing, lists changed safely | Against relays started in the test | [`crate::nostr::client`] |
71//! | Signing in three ways, signing, encrypting | Against a remote signer started in the test | [`crate::nostr::account`] |
72//! | The store letting go of what is old | Events saved, swept, and counted | [`crate::nostr::sweep`] |
73//! | What is fetched from the web: caps, what is read of a page | Parsing, with no network | [`crate::http`], [`crate::previews`], [`crate::nostr::nip05`], [`crate::nostr::zap`], [`crate::nostr::relay_info`] |
74//! | Pictures: the budget, the shapes, what is dropped first | Pictures made in the test | [`crate::images`] |
75//! | Rows: how tall one is estimated, against how tall it measures | Rows laid out without a window | [`crate::views::components::note_row`](mod@crate::views::components::note_row) |
76//! | The list: which rows are built, where they sit, what a jump does | The arithmetic of the list, without a window | [`crate::views::components::virtual_list`] |
77//!
78//! # What a test is named
79//!
80//! A test is named for what it holds to be so, as a sentence:
81//! `a_note_the_store_let_go_of_is_asked_of_the_relays_again`,
82//! `a_list_that_cannot_be_read_is_not_written_over`,
83//! `the_estimate_is_close_to_the_measured_row`. Not for the function it
84//! calls, and not `test_` anything.
85//!
86//! Read one after the other, the names are an account of what the code
87//! is held to, which is how [the list at the foot of this
88//! page](#every-test) is meant to be read. A name that can't be written
89//! as a sentence that is true or false is usually a test of nothing in
90//! particular.
91//!
92//! A test that fails says which sentence stopped being true.
93//!
94//! # What the tests stand on
95//!
96//! Nothing outside the test process, but for the two keyring tests. Each
97//! test that needs one makes its own, and takes it away after:
98//!
99//! ```mermaid
100//! flowchart LR
101//! test["A test of the relay client"] --> helper["with_relays: so many relays, and a client whose own relay is the first"]
102//! helper --> relays["LocalRelay, on a port of this machine, in this process"]
103//! helper --> store["An event store in a temporary directory named for the test"]
104//! helper --> client["A NostrService that takes anyone's word for a relay"]
105//! client --> relays
106//! client --> store
107//! ```
108//!
109//! - **Relays.** `nostr-sdk` has a relay that runs in-process, the same
110//! one Avestruz runs for its drafts. A test of
111//! [`crate::nostr::client`] starts as many as it needs, puts events on
112//! them, and asks the client what it finds. What is asked of which
113//! relay is then a fact that can be looked at: which relays are in the
114//! pool afterwards, which relay holds what was published.
115//! - **A remote signer.** The tests of [`crate::nostr::account`] run a
116//! NIP-46 signer in the test, over one of those relays: one that
117//! agrees, one that refuses, one that will not decrypt.
118//! - **An event store.** A real LMDB store in a temporary directory with
119//! the test's name and the process's id in it, removed when the test
120//! is done.
121//! - **Time.** What depends on when an event was written is given events
122//! written at chosen times. Timeouts in the client's tests are the real
123//! ones, which is why a few of those tests take a second or two.
124//!
125//! The client's real rule about which relays may be connected to would
126//! refuse every relay a test can start, since they are all on this
127//! machine. So the client takes that rule as a value
128//! ([`Reach`](crate::nostr::client::Reach)), the tests give it one that
129//! accepts anything, and the rule itself
130//! ([`public`](crate::nostr::outbox::public)) is tested on its own.
131//!
132//! # What `just check` holds to
133//!
134//! Not everything that is held to is a test. `just check` runs, in
135//! order:
136//!
137//! | Step | What it holds to |
138//! |---|---|
139//! | `cargo fmt --check` | One way of laying the code out (`rustfmt.toml`) |
140//! | `cargo clippy`, warnings denied | Nothing in `src/` that can panic, no dead code, no wildcard imports (see [`crate::guide::decisions`]) |
141//! | `cargo test --no-run` | That the tests compile, which clippy, run without them, doesn't see |
142//! | `cargo machete` | No dependency that isn't used |
143//! | `cog check` | That every commit since the last release is a Conventional Commit, of a type the release notes have a section for (`cog.toml`, `cliff.toml`, `CONTRIBUTING.md`) |
144//! | `just doc` | That the documentation builds, with no link that leads nowhere |
145//! | `just mermaid-check` | That every diagram in it parses |
146//! | `cargo deny` | No dependency under a licence the project can't take |
147//! | `cargo audit` | No dependency with a known vulnerability; what is only warned of is listed in the `justfile`, each with why it is there |
148//!
149//! The tools that are not part of Rust are skipped, with a line saying
150//! so, where they are not installed.
151//!
152//! On GitHub every push to `main`, and every pull request to it, is put
153//! through `just check` and `just test` (`.github/workflows/ci.yml`),
154//! with all of those tools installed, so that nothing is skipped there.
155//! A second job measures coverage. The two tests of the system keyring
156//! find none on a runner and hold to nothing; the rest run as they do
157//! here.
158//!
159//! # Fuzzing
160//!
161//! A test holds a parser to the inputs somebody thought of. What reads
162//! what a stranger sent is also held to the ones nobody did: `just fuzz`
163//! feeds each of these whatever [cargo-fuzz](https://github.com/rust-fuzz/cargo-fuzz)
164//! makes up, for a minute each, and a panic, or anything the address
165//! sanitizer catches, fails it and leaves the input that did it under
166//! `fuzz/artifacts/`.
167//!
168//! | Target | What it reads | Whose it is |
169//! |---|---|---|
170//! | `note_content` | A note's text, the Markdown in it included, and an `imeta` tag ([`parse_attached`](crate::nostr::content::parse_attached), [`runs`](crate::nostr::markdown::runs), [`imeta`](crate::nostr::content::imeta)) | Anyone's who writes a note |
171//! | `link_preview` | The head of a linked page ([`previews::parse`](crate::previews::parse)) | Whatever server a note links to |
172//! | `relay_info` | The document a relay says what it is in ([`relay_info::parse`](crate::nostr::relay_info::parse)) | Any relay's |
173//! | `attached_file` | A file to attach, taken apart to leave out what it says beside the picture ([`media::prepare`](crate::media::prepare)) | Whoever made the file |
174//! | `youtube_link` | A link that may be to a YouTube video, and what a video is called ([`video_at`](crate::youtube::video_at), [`title`](crate::youtube::title)) | A note's author, and YouTube |
175//!
176//! ```sh
177//! just fuzz # every target, a minute each
178//! just fuzz note_content # one target, for a minute
179//! just fuzz note_content 0 # one target, until it is stopped
180//! ```
181//!
182//! It takes a nightly toolchain (`rustup toolchain install nightly`) and
183//! `cargo install cargo-fuzz`, and is no part of `just check`: it builds
184//! the whole program a second time, with the sanitizer in it. The targets
185//! are a crate of their own in `fuzz/`, with its own lock file, which
186//! holds the dependencies to the versions the program is built with.
187//!
188//! What a fuzzer has found so far it keeps in `fuzz/corpus/`, on the
189//! machine it ran on: it starts from nothing on another. Decoding a
190//! picture is not among the targets: that is the `image` crate's, which
191//! is fuzzed where it is made.
192//!
193//! # What is not tested
194//!
195//! - **The views, as pictures.** Nothing renders a view and compares it
196//! with anything. What is tested of them is their arithmetic: how tall
197//! a row is, which rows a list builds.
198//! - **The handlers.** They are thin on purpose: what a handler decides
199//! is mostly in a function of [`crate::nostr`] or of the state, which is
200//! tested. The handlers themselves, and the order messages arrive in,
201//! are not.
202//! - **Real relays, real signers, a real wallet.** The relay in the tests
203//! is one implementation, and a patient one.
204//! - **The panel.** Whether the popup opens where it should and closes
205//! when it should is the desktop's doing.
206//! - **Anything over the network**: pictures, link previews, NIP-05,
207//! LNURL. Their parsing is tested; their fetching is not.
208//! - **Videos from the web, and their sound.** The tests play a second of
209//! a test picture from a file, through the same pipeline, where
210//! GStreamer and the elements for it are installed, and pass for
211//! nothing where they are not. Fetching one, the formats notes really
212//! link to, and what is heard are tried by hand.
213//! - **Fonts.** What a font file is told to have of colour glyphs and of
214//! emoji is tested, on files made for the tests, and so is which fonts
215//! are then put first. What the fonts of a system draw of an emoji is
216//! looked at by hand.
217//! - **yt-dlp, and YouTube.** Which links are videos, what is handed to
218//! yt-dlp and how what it writes is read are tested. Running it is not:
219//! whether it finds a video depends on the day, the machine and where
220//! the machine is.
221//!
222//! # Trying it by hand
223//!
224//! What the tests don't reach is tried by running the applet against a
225//! relay on this machine, with a config and a store of its own, so that
226//! nothing of the real account is touched:
227//!
228//! ```sh
229//! # A relay to talk to (https://github.com/fiatjaf/nak).
230//! nak serve --hostname 127.0.0.1 --port 10999 &
231//!
232//! # A throwaway account, someone for it to follow, and something to read.
233//! key=$(nak key generate) other=$(nak key generate)
234//! nak event --sec "$other" -c "hello" ws://127.0.0.1:10999
235//! nak event --sec "$key" -k 3 -p "$(nak key public "$other")" ws://127.0.0.1:10999
236//! nak encode nsec "$key" # what to sign in with
237//!
238//! # A config that names only that relay, and a store nobody else uses.
239//! export XDG_CONFIG_HOME=$(mktemp -d) XDG_DATA_HOME=$(mktemp -d)
240//! config="$XDG_CONFIG_HOME/cosmic/io.github.cosmic-applet-avestruz/v1"
241//! mkdir -p "$config" && printf '["ws://127.0.0.1:10999"]' > "$config/relays"
242//!
243//! # Outside the panel the button is a small window of its own; the popup opens by the screen's edge.
244//! RUST_LOG=cosmic_applet_avestruz=debug just run
245//! ```
246//!
247//! Signing in with an `nsec` there writes to the real keyring, under the
248//! throwaway account's `npub`: sign out again, or remove it with
249//! `secret-tool clear service cosmic-applet-avestruz username <npub>`.
250//!
251//! A relay on this machine is used whatever the account's relay list
252//! says, which is what makes this work: see [`crate::guide::relays`].
253//!
254//! # Every test
255//!
256#![doc = include_str!(concat!(env!("OUT_DIR"), "/test_inventory.md"))]