Add a number of tests
Some checks are pending
build / binary (push) Waiting to run
build / binary-static (push) Waiting to run
build / container (push) Waiting to run
build / clippy (push) Waiting to run
lint / linting (push) Waiting to run

These are quite... simple, and cover little, but it's more than nothing!

While there, enable tests in the Nix package, so they're run in CI too.

Signed-off-by: Gergely Nagy <me@gergo.csillger.hu>
This commit is contained in:
Gergely Nagy 2025-01-30 09:18:35 +01:00
parent a039cf2310
commit aa4c387605
No known key found for this signature in database
19 changed files with 204 additions and 7 deletions

View file

@ -21,7 +21,12 @@ SPDX-PackageDownloadLocation = "https://git.madhouse-project.org/algernon/iocain
SPDX-License-Identifier = "MIT"
[[annotations]]
path = ["docs/templates/**", "docs/sass/**", "templates/*.hbs"]
path = [
"docs/templates/**",
"docs/sass/**",
"templates/*.hbs",
"tests/data/**",
]
precedence = "aggregate"
SPDX-FileCopyrightText = "2025 Gergely Nagy"
SPDX-License-Identifier = "MIT"

View file

@ -21,7 +21,6 @@ pkgs.rustPlatform.buildRustPackage {
cargoLock = {
lockFile = ../Cargo.lock;
};
doCheck = false;
meta = with lib; {
description = "The deadliest poison known to AI";
license = licenses.mit;

View file

@ -11,7 +11,7 @@ use axum::{
Router,
};
use crate::iocaine::{
use crate::{
assembled_statistical_sequences::AssembledStatisticalSequences, config::Config,
garglebargle::GargleBargle, wurstsalat_generator_pro::WurstsalatGeneratorPro,
};

View file

@ -8,9 +8,7 @@ use rand::{seq::IndexedRandom, Rng};
use rust_embed::Embed;
use serde::Serialize;
use crate::iocaine::{
app::Iocaine, gobbledygook::GobbledyGook, wurstsalat_generator_pro::join_words,
};
use crate::{app::Iocaine, gobbledygook::GobbledyGook, wurstsalat_generator_pro::join_words};
#[derive(Embed)]
#[folder = "templates/"]

View file

@ -11,7 +11,6 @@ use figment::{
};
use figment_file_provider_adapter::FileAdapter;
mod iocaine;
use iocaine::{app::Iocaine, cli, config::Config};
#[tokio::main]

View file

@ -18,3 +18,20 @@ impl GargleBargle {
Ok(Self(reader.lines().filter_map(|word| word.ok()).collect()))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_load_error() {
let result = GargleBargle::load_words("/does-not-exist");
assert!(result.is_err());
}
#[test]
fn test_load_ok() {
let result = GargleBargle::load_words("README.md");
assert!(result.is_ok());
}
}

View file

@ -20,3 +20,23 @@ impl GobbledyGook {
ChaCha20Rng::seed_from_u64(seed)
}
}
#[cfg(test)]
mod tests {
use super::*;
use rand::Rng;
#[test]
fn test_stable_rng() {
let mut rng_test1 = GobbledyGook::for_url("/test");
let mut rng_test2 = GobbledyGook::for_url("/test");
let mut rng_other = GobbledyGook::for_url("/other");
let t1: u8 = rng_test1.random();
let t2: u8 = rng_test2.random();
let t3: u8 = rng_other.random();
assert_eq!(t1, t2);
assert_ne!(t2, t3);
}
}

View file

@ -154,3 +154,37 @@ pub fn join_words<I: Iterator<Item = String>>(mut words: I) -> String {
}
}
}
#[cfg(test)]
mod tests {
use super::super::gobbledygook::GobbledyGook;
use super::*;
#[test]
fn test_load_error() {
let mut wurstsalat = WurstsalatGeneratorPro::new();
let result = wurstsalat.learn_from_files(&["/does-not-exist".to_string()]);
assert!(result.is_err());
}
#[test]
fn test_load_ok() {
let mut wurstsalat = WurstsalatGeneratorPro::new();
let result = wurstsalat.learn_from_files(&["README.md".to_string()]);
assert!(result.is_ok());
}
#[test]
fn test_generate() {
let mut wurstsalat = WurstsalatGeneratorPro::new();
wurstsalat
.learn_from_files(&["tests/data/lorem-ipsum.txt".to_string()])
.unwrap();
let mut rng = GobbledyGook::for_url("/test");
let words = wurstsalat.generate(&mut rng).take(1);
let output = join_words(words);
assert_eq!(output, "Voluptate.");
}
}

View file

@ -0,0 +1 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

View file

@ -0,0 +1,11 @@
request_uri: {{ request_uri }}
is_root: {{ is-root }}
paragraphs:
{{#each paragraphs}}
{{ markov-gen "garbage" this.index this.value }}
{{/each}}
links:
{{#each links}}
href: {{ href-gen "links" this.index this.value.href_words }}
title: {{ markov-gen "titles" this.index this.value.title_words }}
{{/each}}

View file

@ -0,0 +1 @@
{{ is-root }}

View file

@ -0,0 +1,4 @@
{{#each links}}
href: {{ href-gen "links" this.index this.value.href_words }}
title: {{ markov-gen "titles" this.index this.value.title_words }}
{{/each}}

View file

@ -0,0 +1,5 @@
{{#each paragraphs}}
<paragraph>
{{ markov-gen "garbage" this.index this.value }}
</paragraph>
{{/each}}

View file

@ -0,0 +1 @@
{{ request_uri }}

25
tests/data/words.txt Normal file
View file

@ -0,0 +1,25 @@
formations
honked
reshipping
bilinguals
beakers
Alyson's
Rodrigo's
duplicate
rejuvenating
unconscionable
mashups
rhymester's
genders
mealymouthed
regrettable
grottoes
Astaire's
sweepings's
Nevadan's
Chartres's
morticians
biochemically
anted
UNICEF
Missouri's

77
tests/test_app.rs Normal file
View file

@ -0,0 +1,77 @@
// SPDX-FileCopyrightText: 2025 Gergely Nagy
// SPDX-FileContributor: Gergely Nagy
//
// SPDX-License-Identifier: MIT
use iocaine::{
app::Iocaine, assembled_statistical_sequences::AssembledStatisticalSequences, config::*,
};
fn generate(templates: &str, host: &str, url: &str) -> String {
let config = Config {
sources: SourceConfig {
words: "tests/data/words.txt".to_string(),
markov: vec!["tests/data/lorem-ipsum.txt".to_string()],
},
server: ServerConfig::default(),
generator: GeneratorConfig {
markov: MarkovGeneratorConfig {
paragraphs: MarkovParagraphsConfig { min: 2, max: 2 },
words: MarkovWordsConfig { min: 2, max: 2 },
},
links: LinkGeneratorConfig {
min: 2,
max: 2,
href_words: LinkGeneratorHrefWordsConfig { min: 2, max: 2 },
title_words: LinkGeneratorTitleWordsConfig { min: 2, max: 2 },
},
initial_seed: String::new(),
},
templates: TemplatesConfig {
directory: Some(format!("tests/data/templates/{}", templates)),
},
};
let iocaine = Iocaine::new(config).unwrap();
AssembledStatisticalSequences::generate(&iocaine, host, url)
}
#[test]
fn test_templates_request_uri() {
let result = generate("request_uri", "test.example.com", "/test/");
assert_eq!(result, "/test/\n");
}
#[test]
fn test_templates_is_root() {
let root = generate("is_root", "test.example.com", "/");
assert_eq!(root, "true\n");
let not_root = generate("is_root", "test.example.com", "/not-root/");
assert_eq!(not_root, "\n");
}
#[test]
fn test_templates_paragraphs() {
let paragraphs = generate("paragraphs", "test.example.com", "/paragraphs/");
assert_eq!(
paragraphs,
"<paragraph>\n Qui officia.\n</paragraph>\n<paragraph>\n Elit, sed.\n</paragraph>\n"
);
}
#[test]
fn test_templates_links() {
let paragraphs = generate("links", "test.example.com", "/links/");
assert_eq!(paragraphs, "href: unconscionable-reshipping\ntitle: Sint occaecat.\nhref: Astaire's-duplicate\ntitle: Aute irure.\n");
}
#[test]
fn test_templates_seeding() {
let root_request1 = generate("complete", "test.example.com", "/");
let root_request2 = generate("complete", "test.example.com", "/");
let other_request = generate("complete", "test.example.com", "/other/");
assert_eq!(root_request1, root_request2);
assert_ne!(root_request2, other_request);
}