2025-01-30 09:18:35 +01:00
|
|
|
// 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)),
|
|
|
|
},
|
2025-02-05 02:33:57 +01:00
|
|
|
metrics: MetricsConfig::default(),
|
2025-01-30 09:18:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2025-01-30 21:29:55 +01:00
|
|
|
|
|
|
|
#[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(),
|
2025-02-05 02:33:57 +01:00
|
|
|
metrics: MetricsConfig::default(),
|
2025-01-30 21:29:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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>"));
|
|
|
|
}
|