iocaine/tests/test_app.rs
Gergely Nagy 6fa438ebcb
Make the metrics always available
Previously, metrics were behind a feature flag (which was enabled by
default), now it's always available, but still disabled by default.

Signed-off-by: Gergely Nagy <me@gergo.csillger.hu>
2025-02-07 15:07:40 +01:00

104 lines
3.3 KiB
Rust

// 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)),
},
metrics: MetricsConfig::default(),
};
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);
}
#[test]
fn test_templates_builtin() {
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::default(),
templates: TemplatesConfig::default(),
metrics: MetricsConfig::default(),
};
let iocaine = Iocaine::new(config).unwrap();
let result = AssembledStatisticalSequences::generate(
&iocaine,
"test.example.com",
"/builtin-templates/",
);
assert!(result.contains("/builtin-templates/"));
assert!(result.contains("a href=\"../\""));
assert!(result.contains("<p>"));
assert!(result.contains("<li>"));
}