refactor: reorganize source and tests
This commit is contained in:
1 parent
15d76c18a0
commit
c6205a337c
139 files changed
+5532
-5548
No files matched your search
@@ -0,0 +1,44 @@
|
||||
use super::add_offline_account;
|
||||
use crate::auth::{Account, AccountStore, AccountType};
|
||||
|
||||
fn microsoft_account() -> Account {
|
||||
Account {
|
||||
uuid: "00000000-0000-0000-0000-000000000001".to_owned(),
|
||||
username: "Owner".to_owned(),
|
||||
account_type: AccountType::Microsoft,
|
||||
active: false,
|
||||
refresh_token: Some("refresh".to_owned()),
|
||||
cached_mc_token: None,
|
||||
cached_mc_token_expires_at: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn creates_offline_account_after_microsoft_account_exists() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let mut store = AccountStore::empty_for_test(temp.path().join("accounts.json"));
|
||||
store.add(microsoft_account());
|
||||
add_offline_account(&mut store, "Steve").expect("offline account should be added");
|
||||
|
||||
assert_eq!(store.accounts.len(), 2);
|
||||
assert_eq!(store.accounts[1].username, "Steve");
|
||||
assert_eq!(store.accounts[1].account_type, AccountType::Offline);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_offline_account_before_microsoft_account_exists() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let mut store = AccountStore::empty_for_test(temp.path().join("accounts.json"));
|
||||
let err = add_offline_account(&mut store, "Steve")
|
||||
.expect_err("offline account should require a microsoft account");
|
||||
|
||||
assert!(err.to_string().contains("Microsoft account"));
|
||||
assert!(store.accounts.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_empty_offline_username() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let mut store = AccountStore::empty_for_test(temp.path().join("accounts.json"));
|
||||
assert!(add_offline_account(&mut store, " ").is_err());
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
use super::build_command;
|
||||
|
||||
#[test]
|
||||
fn parses_instance_list_subcommand() {
|
||||
let matches = build_command()
|
||||
.try_get_matches_from(["rmcl", "instance", "list"])
|
||||
.expect("command should parse");
|
||||
let instance = matches
|
||||
.subcommand_matches("instance")
|
||||
.expect("instance subcommand should be present");
|
||||
assert_eq!(
|
||||
instance.subcommand_name(),
|
||||
Some("list"),
|
||||
"instance list should resolve to the list subcommand"
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
use super::find_entry_by_stem;
|
||||
use crate::instance::ContentEntry;
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn entry(file_stem: &str) -> ContentEntry {
|
||||
ContentEntry {
|
||||
file_stem: file_stem.to_string(),
|
||||
name: file_stem.to_string(),
|
||||
source_slug: None,
|
||||
installed_path: None,
|
||||
provider_project: None,
|
||||
title_suffix: None,
|
||||
footer_label: None,
|
||||
description: String::new(),
|
||||
enabled: true,
|
||||
icon_bytes: None,
|
||||
provider_icon: false,
|
||||
provider_description: false,
|
||||
path: PathBuf::from(file_stem),
|
||||
icon_lines: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn matches_by_stem_case_insensitively() {
|
||||
let entries = vec![entry("Sodium"), entry("Lithium")];
|
||||
let found = find_entry_by_stem(&entries, "sOdIuM").expect("entry should match");
|
||||
assert_eq!(found.file_stem, "Sodium");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn returns_none_for_missing_stem() {
|
||||
let entries = vec![entry("Sodium")];
|
||||
assert!(find_entry_by_stem(&entries, "iris").is_none());
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
use super::*;
|
||||
|
||||
fn version(name: &str, number: &str) -> modrinth::VersionInfo {
|
||||
modrinth::VersionInfo {
|
||||
id: number.to_owned(),
|
||||
project_id: "project".to_owned(),
|
||||
name: name.to_owned(),
|
||||
version_number: number.to_owned(),
|
||||
game_versions: vec!["1.21.1".to_owned()],
|
||||
loaders: vec!["fabric".to_owned()],
|
||||
date_published: String::new(),
|
||||
files: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn version_override_matches_name_or_number_and_rejects_unknown_values() {
|
||||
let versions = vec![version("Stable", "1.0.0"), version("Beta", "2.0.0-beta")];
|
||||
|
||||
assert_eq!(select_version(&versions, None).unwrap().name, "Stable");
|
||||
assert_eq!(
|
||||
select_version(&versions, Some("Beta"))
|
||||
.unwrap()
|
||||
.version_number,
|
||||
"2.0.0-beta"
|
||||
);
|
||||
assert_eq!(
|
||||
select_version(&versions, Some("1.0.0")).unwrap().name,
|
||||
"Stable"
|
||||
);
|
||||
assert_eq!(
|
||||
select_version(&versions, Some("missing")).unwrap_err(),
|
||||
"Version 'missing' not found"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn import_command_parses_name_and_version_overrides() {
|
||||
let matches = crate::cli::build_command()
|
||||
.try_get_matches_from([
|
||||
"rmcl",
|
||||
"import",
|
||||
"example-pack",
|
||||
"--name",
|
||||
"Example",
|
||||
"--version",
|
||||
"1.2.3",
|
||||
])
|
||||
.unwrap();
|
||||
let import = matches.subcommand_matches("import").unwrap();
|
||||
|
||||
assert_eq!(
|
||||
import.get_one::<String>("source").map(String::as_str),
|
||||
Some("example-pack")
|
||||
);
|
||||
assert_eq!(
|
||||
import.get_one::<String>("name").map(String::as_str),
|
||||
Some("Example")
|
||||
);
|
||||
assert_eq!(
|
||||
import.get_one::<String>("version").map(String::as_str),
|
||||
Some("1.2.3")
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn missing_local_pack_returns_a_file_not_found_error() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let missing = temp.path().join("missing.mrpack");
|
||||
let source = missing.to_string_lossy().into_owned();
|
||||
let matches = crate::cli::build_command()
|
||||
.try_get_matches_from(["rmcl", "import", &source])
|
||||
.unwrap();
|
||||
let import = matches.subcommand_matches("import").unwrap();
|
||||
|
||||
let error = handle_import(import).await.unwrap_err().to_string();
|
||||
|
||||
assert_eq!(error, format!("File not found: {}", missing.display()));
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
use super::parse_resolution;
|
||||
|
||||
#[test]
|
||||
fn parses_valid_resolution() {
|
||||
assert_eq!(
|
||||
parse_resolution("1920x1080").expect("should parse"),
|
||||
(1920, 1080)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_invalid_resolution_format() {
|
||||
assert!(parse_resolution("1920").is_err());
|
||||
assert!(parse_resolution("1920xa").is_err());
|
||||
assert!(parse_resolution("0x1080").is_err());
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
use super::resolve_log_path;
|
||||
|
||||
#[test]
|
||||
fn resolves_latest_log_when_no_file_is_given() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let dir = tmp.path().join("demo/minecraft/logs/launches");
|
||||
std::fs::create_dir_all(&dir).expect("log directory should exist");
|
||||
std::fs::write(dir.join("2024-01-02_03-04-05.log"), "newer").expect("write newer log");
|
||||
std::fs::write(dir.join("2024-01-01_03-04-05.log"), "older").expect("write older log");
|
||||
|
||||
let path = resolve_log_path(tmp.path(), "demo", None).expect("latest log should resolve");
|
||||
assert_eq!(
|
||||
path.file_name().and_then(|name| name.to_str()),
|
||||
Some("2024-01-02_03-04-05.log")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolves_named_log_file() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let dir = tmp.path().join("demo/minecraft/logs/launches");
|
||||
std::fs::create_dir_all(&dir).expect("log directory should exist");
|
||||
std::fs::write(dir.join("latest.log"), "hello").expect("write named log");
|
||||
|
||||
let path =
|
||||
resolve_log_path(tmp.path(), "demo", Some("latest.log")).expect("named log should resolve");
|
||||
assert_eq!(
|
||||
path.file_name().and_then(|name| name.to_str()),
|
||||
Some("latest.log")
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
use super::{format_datetime, render_table};
|
||||
use chrono::{TimeZone, Utc};
|
||||
|
||||
#[test]
|
||||
fn render_table_aligns_columns() {
|
||||
let rendered = render_table(
|
||||
&["Name", "State"],
|
||||
&[
|
||||
vec!["Alpha".to_string(), "enabled".to_string()],
|
||||
vec!["Longer Name".to_string(), "off".to_string()],
|
||||
],
|
||||
);
|
||||
|
||||
let lines: Vec<&str> = rendered.lines().collect();
|
||||
assert_eq!(lines[0], "Name State ");
|
||||
assert_eq!(lines[1], "----------- -------");
|
||||
assert_eq!(lines[2], "Alpha enabled");
|
||||
assert_eq!(lines[3], "Longer Name off ");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn formats_datetime_consistently() {
|
||||
let dt = Utc.with_ymd_and_hms(2024, 1, 2, 3, 4, 5).unwrap();
|
||||
assert_eq!(format_datetime(&dt), "2024-01-02 03:04:05 UTC");
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
use super::filter_manifest_versions;
|
||||
use crate::net::mojang::{LatestVersions, VersionEntry, VersionManifest};
|
||||
use std::collections::HashSet;
|
||||
|
||||
fn manifest() -> VersionManifest {
|
||||
VersionManifest {
|
||||
latest: LatestVersions {
|
||||
release: "1.20.1".to_string(),
|
||||
snapshot: "24w01a".to_string(),
|
||||
},
|
||||
versions: vec![
|
||||
VersionEntry {
|
||||
id: "1.20.1".to_string(),
|
||||
version_type: "release".to_string(),
|
||||
url: "https://example.com/release".to_string(),
|
||||
sha1: "a".to_string(),
|
||||
},
|
||||
VersionEntry {
|
||||
id: "24w01a".to_string(),
|
||||
version_type: "snapshot".to_string(),
|
||||
url: "https://example.com/snapshot".to_string(),
|
||||
sha1: "b".to_string(),
|
||||
},
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn filters_out_snapshots_by_default() {
|
||||
let rows = filter_manifest_versions(&manifest(), None, false);
|
||||
assert_eq!(rows.len(), 1);
|
||||
assert_eq!(rows[0].id, "1.20.1");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn intersects_supported_versions() {
|
||||
let supported = HashSet::from(["24w01a".to_string()]);
|
||||
let rows = filter_manifest_versions(&manifest(), Some(&supported), true);
|
||||
assert_eq!(rows.len(), 1);
|
||||
assert_eq!(rows[0].id, "24w01a");
|
||||
}
|
||||
Reference in new issue
Block a user