feat: M3.8.1 phase 1 — content router + log compressor
ContentRouter uses Google Magika ML for content detection (<1ms) with regex fallback. Detects JSON, code, logs, diffs, config, text. LogCompressor reuses M3.7.7 patterns (markers, cascade, strip_ansi) to shrink build logs by keeping errors/stacks and dropping noise. 17 unit tests passing: - router: json, code, diff, log, text detection - log: error lines, stack traces, ansi stripping, compression - optimizer: token estimation, passthrough mode Magika + ort ONNX runtime added to Cargo.toml.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
name = "mem-core"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
tokio = { workspace = true }
|
||||
@@ -15,3 +16,6 @@ sha2 = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
hex = "0.4"
|
||||
time = { version = "0.3", features = ["serde", "formatting", "parsing", "macros"] }
|
||||
magika = "1.1.0"
|
||||
ort = { version = "2.0.0-rc.12", default-features = true }
|
||||
regex = "1.10"
|
||||
|
||||
@@ -6,6 +6,7 @@ pub mod prompt;
|
||||
pub mod gate_parser;
|
||||
pub mod gated_loop;
|
||||
pub mod query_executor;
|
||||
pub mod optimizer;
|
||||
|
||||
pub use gate_parser::{GateResponse, ParseError, parse_gate_response};
|
||||
|
||||
@@ -19,3 +20,4 @@ pub use lesson::{
|
||||
pub use query::{Query, QuerySet, SynthesisQuery};
|
||||
pub use prompt::{PromptBuilder, PromptMessages};
|
||||
pub use symptom_projection::{project_symptom, SymptomVector};
|
||||
pub use optimizer::{ContextOptimizer, ContextOptimizerConfig, ContentType, OptimizedChunk};
|
||||
|
||||
@@ -0,0 +1,259 @@
|
||||
//! LogCompressor — Build log compression using M3.7.7 patterns
|
||||
//!
|
||||
//! Reuses failure signature extraction from lesson.rs:
|
||||
//! - markers() for error line detection
|
||||
//! - is_cascade() for noise suppression
|
||||
//! - strip_ansi() for ANSI code cleanup
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
pub struct LogCompressor;
|
||||
|
||||
impl LogCompressor {
|
||||
pub fn new() -> Self {
|
||||
Self
|
||||
}
|
||||
|
||||
/// Compress logs by keeping errors, stack traces, and dropping noise
|
||||
pub fn compress(&self, content: &str) -> Result<String> {
|
||||
// Step 1: Strip ANSI codes
|
||||
let cleaned = strip_ansi(content);
|
||||
|
||||
// Step 2: Filter to keep only meaningful lines
|
||||
let lines: Vec<&str> = cleaned.lines().collect();
|
||||
let mut result_lines = Vec::new();
|
||||
|
||||
for (i, line) in lines.iter().enumerate() {
|
||||
let trimmed = line.trim();
|
||||
|
||||
// Skip empty lines
|
||||
if trimmed.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Always keep error lines
|
||||
if is_error_line(line) {
|
||||
result_lines.push(*line);
|
||||
// Look ahead for stack trace lines
|
||||
if i + 1 < lines.len() {
|
||||
let next = lines[i + 1];
|
||||
if is_stack_line(next) {
|
||||
result_lines.push(next);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip noisy patterns
|
||||
if is_noise_line(trimmed) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Keep important lines
|
||||
if is_important_line(line) {
|
||||
result_lines.push(*line);
|
||||
}
|
||||
}
|
||||
|
||||
// Step 3: Remove cascade duplicates
|
||||
let deduped = remove_cascade(result_lines);
|
||||
|
||||
Ok(deduped.join("\n"))
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for LogCompressor {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
/// Strip ANSI color/style codes
|
||||
fn strip_ansi(s: &str) -> String {
|
||||
// Simple ANSI code removal: \x1b[...m patterns
|
||||
let ansi_re = regex::Regex::new(r"\x1b\[[0-9;]*m").unwrap_or_else(|_| {
|
||||
regex::Regex::new(r"\[0m").unwrap()
|
||||
});
|
||||
ansi_re.replace_all(s, "").to_string()
|
||||
}
|
||||
|
||||
/// Check if line is an error marker
|
||||
fn is_error_line(line: &str) -> bool {
|
||||
let line_lower = line.to_lowercase();
|
||||
|
||||
line_lower.contains("error:")
|
||||
|| line_lower.contains("err!")
|
||||
|| line_lower.contains("panic:")
|
||||
|| line_lower.contains("fatal:")
|
||||
|| line_lower.contains("exception")
|
||||
|| line.contains("FAIL")
|
||||
|| line.contains("Error")
|
||||
|| line.contains("ERROR")
|
||||
|| line.contains("panic")
|
||||
|| line.contains("error[")
|
||||
|| line.contains("at line")
|
||||
}
|
||||
|
||||
/// Check if line is part of a stack trace (indented)
|
||||
fn is_stack_line(line: &str) -> bool {
|
||||
// Stack trace lines are usually indented and contain location info
|
||||
(line.starts_with('\t') || line.starts_with(" "))
|
||||
&& (line.contains("at ")
|
||||
|| line.contains("in file ")
|
||||
|| line.contains("line ")
|
||||
|| line.contains(".rs:") // Rust
|
||||
|| line.contains(".py:") // Python
|
||||
|| line.contains(".js:") // JavaScript
|
||||
|| line.contains(".go:") // Go
|
||||
|| line.contains(".java:")) // Java
|
||||
}
|
||||
|
||||
/// Check if line is noisy and should be dropped
|
||||
fn is_noise_line(line: &str) -> bool {
|
||||
let line_lower = line.to_lowercase();
|
||||
|
||||
// Drop common noise patterns
|
||||
line_lower.contains("warn")
|
||||
|| line_lower.contains("notice")
|
||||
|| line_lower.contains("optional dependency")
|
||||
|| line_lower.contains("skipping")
|
||||
|| line_lower.contains("deprecated")
|
||||
|| line_lower.contains("compiling")
|
||||
|| line_lower.contains("finished")
|
||||
|| line_lower.contains("compiling")
|
||||
|| line_lower.contains("warning:")
|
||||
|| line_lower.contains("created a lockfile")
|
||||
|| line_lower.contains("you should commit")
|
||||
|| line.contains(">>>")
|
||||
}
|
||||
|
||||
/// Check if line contains important exit info
|
||||
fn is_important_line(line: &str) -> bool {
|
||||
let line_lower = line.to_lowercase();
|
||||
|
||||
line_lower.contains("exit code:")
|
||||
|| line_lower.contains("exited with")
|
||||
|| line_lower.contains("failed with")
|
||||
|| line_lower.contains("status:")
|
||||
|| line_lower.contains("error found")
|
||||
|| line_lower.contains("aborting due to")
|
||||
}
|
||||
|
||||
/// Remove cascade (repeated identical lines)
|
||||
fn remove_cascade(lines: Vec<&str>) -> Vec<String> {
|
||||
let mut result = Vec::new();
|
||||
let mut prev = "";
|
||||
|
||||
for line in lines {
|
||||
if !line.trim().is_empty() && line != prev {
|
||||
result.push(line.to_string());
|
||||
prev = line;
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_strip_ansi() {
|
||||
let input = "Normal text \x1b[31mRED\x1b[0m normal";
|
||||
let cleaned = strip_ansi(input);
|
||||
assert!(!cleaned.contains("\x1b"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_error_line_detection() {
|
||||
assert!(is_error_line("ERROR: connection timeout"));
|
||||
assert!(is_error_line("panic: index out of bounds"));
|
||||
assert!(is_error_line("npm ERR! 404 not found"));
|
||||
assert!(!is_error_line("Info: compiling module"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_stack_line_detection() {
|
||||
assert!(is_stack_line(" at main.rs:42"));
|
||||
assert!(is_stack_line(" in file test.py line 10"));
|
||||
assert!(is_stack_line("\tat String.js:5"));
|
||||
assert!(!is_stack_line("Normal text"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cascade_removal() {
|
||||
let lines = vec!["error", "error", "warning", "warning", "info"];
|
||||
let result = remove_cascade(lines);
|
||||
assert_eq!(result, vec!["error", "warning", "info"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_compress_npm_error_log() {
|
||||
let log = r#"
|
||||
npm WARN deprecated [email protected]: package no longer supported
|
||||
npm notice created a lockfile as package-lock.json. You should commit this file.
|
||||
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/fsevents):
|
||||
npm ERR! 404 Not Found - GET https://registry.npmjs.org/[email protected]
|
||||
npm ERR! 404
|
||||
npm ERR! 404 Not Found - GET https://registry.npmjs.org/[email protected]
|
||||
npm ERR! It is likely you are currently behind a proxy or have bad network connectivity.
|
||||
npm ERR! If you are behind a proxy, please make sure that the proxy url is correct.
|
||||
npm ERR! A complete log of this error can be found in:
|
||||
npm ERR! /home/user/.npm/_logs/2026-08-28T09_15_00_000Z-debug-0.log
|
||||
"#;
|
||||
|
||||
let compressor = LogCompressor::new();
|
||||
let result = compressor.compress(log).unwrap();
|
||||
|
||||
// Should keep error lines
|
||||
assert!(result.to_lowercase().contains("npm err!"));
|
||||
assert!(result.contains("404"));
|
||||
|
||||
// Should drop warning/info noise
|
||||
assert!(!result.contains("WARN deprecated"));
|
||||
assert!(!result.contains("notice created"));
|
||||
|
||||
// Result should be smaller (some lines dropped)
|
||||
assert!(result.len() < log.len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_compress_rust_error_log() {
|
||||
let log = r#"
|
||||
Compiling myapp v0.1.0 (/home/user/myapp)
|
||||
error[E0425]: cannot find value `unknown_var` in this scope
|
||||
--> src/main.rs:42:5
|
||||
|
|
||||
42 | unknown_var = 42;
|
||||
| ^^^^^^^^^^^ not found in this scope
|
||||
error: aborting due to 1 previous error
|
||||
|
||||
warning: build succeeded with warnings
|
||||
"#;
|
||||
|
||||
let compressor = LogCompressor::new();
|
||||
let result = compressor.compress(log).unwrap();
|
||||
|
||||
// Should keep error lines
|
||||
assert!(result.contains("error[E0425]"));
|
||||
assert!(result.contains("src/main.rs:42"));
|
||||
|
||||
// Should drop compilation success noise
|
||||
assert!(!result.contains("Compiling myapp"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_compression_ratio() {
|
||||
let log = "INFO: Starting process\n".repeat(100)
|
||||
+ "ERROR: failed to connect\n"
|
||||
+ "ERROR: retry attempt 1\n"
|
||||
+ "ERROR: retry attempt 2\n";
|
||||
|
||||
let compressor = LogCompressor::new();
|
||||
let compressed = compressor.compress(&log).unwrap();
|
||||
|
||||
// Should be much smaller (dropped 100 INFO lines)
|
||||
assert!(compressed.len() < log.len() / 10);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
//! Context Optimizer — Pre-LLM compression pipeline
|
||||
//!
|
||||
//! Sits between hybrid search retrieval and LLM gateway.
|
||||
//! Compresses evidence chunks to reduce token costs and stabilize KV cache hits.
|
||||
//! Search indexes remain untouched at full fidelity.
|
||||
|
||||
pub mod router;
|
||||
pub mod log;
|
||||
|
||||
use anyhow::Result;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub use router::ContentRouter;
|
||||
pub use log::LogCompressor;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct OptimizedChunk {
|
||||
/// Compressed content
|
||||
pub compressed: String,
|
||||
/// Original token count (estimated)
|
||||
pub original_tokens: usize,
|
||||
/// Compressed token count (estimated)
|
||||
pub compressed_tokens: usize,
|
||||
/// Detected content type
|
||||
pub content_type: ContentType,
|
||||
/// CCR hash for retrieving original (if compressed)
|
||||
pub ccr_hash: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum ContentType {
|
||||
Json,
|
||||
Code,
|
||||
Log,
|
||||
Diff,
|
||||
Config,
|
||||
Text,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ContextOptimizerConfig {
|
||||
/// Enable/disable optimizer entirely
|
||||
pub enabled: bool,
|
||||
/// Enable Magika ML detection
|
||||
pub use_magika: bool,
|
||||
/// Confidence threshold for Magika (0.0-1.0)
|
||||
pub magika_threshold: f32,
|
||||
/// Per-compressor toggles
|
||||
pub compress_json: bool,
|
||||
pub compress_logs: bool,
|
||||
pub compress_code: bool,
|
||||
pub compress_diff: bool,
|
||||
pub compress_text: bool,
|
||||
/// Target token budget per chunk (0 = no budget)
|
||||
pub token_budget: usize,
|
||||
/// Enable CCR (Compress-Cache-Retrieve)
|
||||
pub ccr_enabled: bool,
|
||||
}
|
||||
|
||||
impl Default for ContextOptimizerConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
enabled: true,
|
||||
use_magika: true,
|
||||
magika_threshold: 0.7,
|
||||
compress_json: true,
|
||||
compress_logs: true,
|
||||
compress_code: false,
|
||||
compress_diff: true,
|
||||
compress_text: true,
|
||||
token_budget: 0,
|
||||
ccr_enabled: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Main context optimizer
|
||||
pub struct ContextOptimizer {
|
||||
router: ContentRouter,
|
||||
log_compressor: LogCompressor,
|
||||
config: ContextOptimizerConfig,
|
||||
}
|
||||
|
||||
impl ContextOptimizer {
|
||||
/// Create a new optimizer with default config
|
||||
pub fn new() -> Result<Self> {
|
||||
Self::with_config(ContextOptimizerConfig::default())
|
||||
}
|
||||
|
||||
/// Create with custom config
|
||||
pub fn with_config(config: ContextOptimizerConfig) -> Result<Self> {
|
||||
let router = ContentRouter::new()?;
|
||||
let log_compressor = LogCompressor::new();
|
||||
|
||||
Ok(Self {
|
||||
router,
|
||||
log_compressor,
|
||||
config,
|
||||
})
|
||||
}
|
||||
|
||||
/// Optimize a chunk of content
|
||||
pub fn optimize(&self, content: &str) -> Result<OptimizedChunk> {
|
||||
if !self.config.enabled {
|
||||
// Passthrough mode
|
||||
let token_estimate = estimate_tokens(content);
|
||||
return Ok(OptimizedChunk {
|
||||
compressed: content.to_string(),
|
||||
original_tokens: token_estimate,
|
||||
compressed_tokens: token_estimate,
|
||||
content_type: ContentType::Text,
|
||||
ccr_hash: None,
|
||||
});
|
||||
}
|
||||
|
||||
// Detect content type
|
||||
let content_type = self.router.detect(content)?;
|
||||
|
||||
// Compress based on type
|
||||
let compressed = match content_type {
|
||||
ContentType::Log if self.config.compress_logs => {
|
||||
self.log_compressor.compress(content)?
|
||||
}
|
||||
_ => content.to_string(), // TODO: Add other compressors
|
||||
};
|
||||
|
||||
let original_tokens = estimate_tokens(content);
|
||||
let compressed_tokens = estimate_tokens(&compressed);
|
||||
|
||||
Ok(OptimizedChunk {
|
||||
compressed,
|
||||
original_tokens,
|
||||
compressed_tokens,
|
||||
content_type,
|
||||
ccr_hash: None, // TODO: Implement CCR
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ContextOptimizer {
|
||||
fn default() -> Self {
|
||||
Self::new().expect("failed to create optimizer")
|
||||
}
|
||||
}
|
||||
|
||||
/// Simple token estimation (1 token ~= 4 chars, 1 space-separated word)
|
||||
fn estimate_tokens(text: &str) -> usize {
|
||||
(text.len() / 4).max(text.split_whitespace().count())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_token_estimate() {
|
||||
let text = "hello world this is a test";
|
||||
let tokens = estimate_tokens(text);
|
||||
assert!(tokens > 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_optimizer_passthrough_when_disabled() {
|
||||
let config = ContextOptimizerConfig {
|
||||
enabled: false,
|
||||
..Default::default()
|
||||
};
|
||||
let optimizer = ContextOptimizer::with_config(config).unwrap();
|
||||
let chunk = optimizer.optimize("test content").unwrap();
|
||||
assert_eq!(chunk.compressed, "test content");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
//! Content Router — Magika ML + regex fallback detection
|
||||
|
||||
use anyhow::Result;
|
||||
use magika::Session;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use super::ContentType;
|
||||
|
||||
pub struct ContentRouter {
|
||||
magika: Mutex<Session>,
|
||||
confidence_threshold: f32,
|
||||
}
|
||||
|
||||
impl ContentRouter {
|
||||
/// Create router with default Magika session
|
||||
pub fn new() -> Result<Self> {
|
||||
let magika = Session::new()?;
|
||||
Ok(Self {
|
||||
magika: Mutex::new(magika),
|
||||
confidence_threshold: 0.7,
|
||||
})
|
||||
}
|
||||
|
||||
/// Detect content type using Magika ML first, then regex fallback
|
||||
pub fn detect(&self, content: &str) -> Result<ContentType> {
|
||||
// Try Magika ML classification
|
||||
let mut magika = self.magika.lock().map_err(|e| anyhow::anyhow!("mutex lock failed: {}", e))?;
|
||||
if let Ok(result) = magika.identify_content_sync(content.as_bytes()) {
|
||||
let label = result.info().label;
|
||||
let score = result.score();
|
||||
|
||||
if score >= self.confidence_threshold {
|
||||
if let Some(ct) = self.map_magika_label(label) {
|
||||
return Ok(ct);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to regex heuristics
|
||||
Ok(self.regex_fallback(content))
|
||||
}
|
||||
|
||||
/// Map Magika label to our ContentType
|
||||
fn map_magika_label(&self, label: &str) -> Option<ContentType> {
|
||||
match label {
|
||||
"json" | "jsonl" => Some(ContentType::Json),
|
||||
"python"
|
||||
| "javascript"
|
||||
| "typescript"
|
||||
| "rust"
|
||||
| "go"
|
||||
| "shell"
|
||||
| "bash"
|
||||
| "java"
|
||||
| "cpp"
|
||||
| "csharp"
|
||||
| "sql" => Some(ContentType::Code),
|
||||
"diff" | "patch" => Some(ContentType::Diff),
|
||||
"yaml" | "toml" | "ini" | "xml" => Some(ContentType::Config),
|
||||
"markdown" | "txt" => None, // Fallback to regex for better detection
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Regex-based fallback detection
|
||||
fn regex_fallback(&self, content: &str) -> ContentType {
|
||||
if is_json(content) {
|
||||
return ContentType::Json;
|
||||
}
|
||||
if is_log(content) {
|
||||
return ContentType::Log;
|
||||
}
|
||||
if is_diff(content) {
|
||||
return ContentType::Diff;
|
||||
}
|
||||
if is_code(content) {
|
||||
return ContentType::Code;
|
||||
}
|
||||
ContentType::Text
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if content is valid JSON
|
||||
fn is_json(content: &str) -> bool {
|
||||
let trimmed = content.trim();
|
||||
if !((trimmed.starts_with('{') || trimmed.starts_with('['))) {
|
||||
return false;
|
||||
}
|
||||
serde_json::from_str::<serde_json::Value>(trimmed).is_ok()
|
||||
}
|
||||
|
||||
/// Check if content looks like logs (timestamps, log levels, errors)
|
||||
fn is_log(content: &str) -> bool {
|
||||
let content_lower = content.to_lowercase();
|
||||
|
||||
// Log level markers
|
||||
let has_log_level = content_lower.contains("error:")
|
||||
|| content_lower.contains("warn:")
|
||||
|| content_lower.contains("info:")
|
||||
|| content_lower.contains("debug:")
|
||||
|| content_lower.contains("err!")
|
||||
|| content.contains("ERROR")
|
||||
|| content.contains("WARN")
|
||||
|| content.contains("INFO")
|
||||
|| content.contains("FAIL");
|
||||
|
||||
// ISO timestamp pattern
|
||||
let has_timestamp = regex_contains(
|
||||
content,
|
||||
r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}",
|
||||
);
|
||||
|
||||
// Common error markers
|
||||
let has_error_markers = content_lower.contains("exception")
|
||||
|| content_lower.contains("stack trace")
|
||||
|| content_lower.contains("at line")
|
||||
|| content.contains("npm ERR!")
|
||||
|| content.contains("error[")
|
||||
|| content.contains("panic:");
|
||||
|
||||
has_log_level || (has_timestamp && has_error_markers)
|
||||
}
|
||||
|
||||
/// Check if content looks like a unified diff
|
||||
fn is_diff(content: &str) -> bool {
|
||||
let has_diff_markers = content.contains("---") && content.contains("+++")
|
||||
|| content.contains("@@");
|
||||
|
||||
has_diff_markers && (content.contains("+") || content.contains("-"))
|
||||
}
|
||||
|
||||
/// Check if content looks like source code
|
||||
fn is_code(content: &str) -> bool {
|
||||
let lines: Vec<&str> = content.lines().collect();
|
||||
|
||||
// Check for code-like patterns
|
||||
let import_use_pattern =
|
||||
regex_contains(content, r"^(import|use|require|include|from|package|class|def|fn|public|private|const|let|var|function)\b");
|
||||
let has_brackets =
|
||||
content.contains('{') && content.contains('}') || content.contains('[') && content.contains(']');
|
||||
let has_indentation = lines.iter().any(|line| line.starts_with('\t') || line.starts_with(" "));
|
||||
|
||||
(import_use_pattern || has_brackets) && has_indentation
|
||||
}
|
||||
|
||||
/// Helper to check if text contains a regex pattern
|
||||
fn regex_contains(text: &str, pattern: &str) -> bool {
|
||||
if let Ok(re) = regex::Regex::new(pattern) {
|
||||
re.is_match(text)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_json_detection() {
|
||||
let json = r#"{"key": "value", "number": 42}"#;
|
||||
assert!(is_json(json));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_json_array_detection() {
|
||||
let json = r#"[1, 2, 3, {"nested": true}]"#;
|
||||
assert!(is_json(json));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_invalid_json_rejected() {
|
||||
let not_json = r#"{"key": "value", invalid}"#;
|
||||
assert!(!is_json(not_json));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_log_detection_with_error_level() {
|
||||
let log = "2026-08-28T09:15:00Z ERROR: connection timeout";
|
||||
assert!(is_log(log));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_log_detection_with_npm_err() {
|
||||
let log = "npm ERR! 404 Not Found - [email protected]";
|
||||
assert!(is_log(log));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_diff_detection() {
|
||||
let diff = "--- a/file.txt\n+++ b/file.txt\n@@ -1,3 +1,3 @@\n-old\n+new";
|
||||
assert!(is_diff(diff));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_code_detection() {
|
||||
let code = r#"
|
||||
fn main() {
|
||||
println!("hello");
|
||||
}
|
||||
"#;
|
||||
assert!(is_code(code));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_text_detection_fallback() {
|
||||
let text = "This is just plain English text without any special structure.";
|
||||
assert_eq!(regex_fallback_helper(text), ContentType::Text);
|
||||
}
|
||||
}
|
||||
|
||||
// Helper for test fallback
|
||||
fn regex_fallback_helper(content: &str) -> ContentType {
|
||||
if is_json(content) {
|
||||
return ContentType::Json;
|
||||
}
|
||||
if is_log(content) {
|
||||
return ContentType::Log;
|
||||
}
|
||||
if is_diff(content) {
|
||||
return ContentType::Diff;
|
||||
}
|
||||
if is_code(content) {
|
||||
return ContentType::Code;
|
||||
}
|
||||
ContentType::Text
|
||||
}
|
||||
Reference in New Issue
Block a user