test_that("sparse NUL text keeps the existing unknown-content diagnosis", { bytes <- c(charToRaw(paste(rep("CoreEngine internal device log\n", 100), collapse = "")), as.raw(0), charToRaw("tail")) path <- tempfile(fileext = ".txt") output <- tempfile("appusage-sparse-nul-") on.exit(unlink(c(path, output), recursive = TRUE), add = TRUE) writeBin(bytes, path) before <- unname(tools::md5sum(path)) ratios <- appusage_byte_ratios(bytes) expect_lt(ratios$nul_byte_ratio, 0.01) expect_lt(ratios$control_byte_ratio, 0.20) expect_error(rawToChar(bytes), "embedded nul") result <- appusage_source_preflight(path, encoding = "UTF-8") expect_identical(result$status, "unknown_content") expect_identical(result$failure_family, "source_unknown_content") expect_equal(result$byte_count, length(bytes)) expect_identical(result$nul_byte_ratio, ratios$nul_byte_ratio) expect_identical(result$control_byte_ratio, ratios$control_byte_ratio) expect_identical(as.character(result$lines), as.character(read_appusage_lines(path, encoding = "UTF-8"))) expect_identical(detect_appusage_type(path), "unknown") summary <- read_appusage_batch(path, ids = "sparse-nul", output_dir = output, project_name = "Synthetic", project_id = "nul", progress = FALSE) expect_identical(summary$status, "error") expect_identical(summary$failure_family, "source_unknown_content") expect_identical(summary$preflight_status, "unknown_content") expect_match(summary$error_class, "appusage_unknown_content") expect_equal(summary$retry_attempt, 0L) expect_true(is.na(summary$data_file)) expect_true(file.exists(summary$metadata_file)) metadata <- jsonlite::read_json(summary$metadata_file, simplifyVector = TRUE) expect_identical(metadata$source$preflight$failure_family, "source_unknown_content") expect_equal(metadata$source$preflight$byte_count, length(bytes)) expect_gt(metadata$source$preflight$nul_byte_ratio, 0) expect_identical(unname(tools::md5sum(path)), before) }) test_that("sparse NUL valid exports match the established file-reader payload", { fixture <- test_path("fixtures", "line_sample.txt") bytes <- readBin(fixture, "raw", n = file.info(fixture)$size) path <- tempfile(fileext = ".txt") on.exit(unlink(path), add = TRUE) sparse <- append(bytes, as.raw(0), after = 10L) writeBin(sparse, path) result <- appusage_source_preflight(path, encoding = "UTF-8") expect_identical(result$status, "ok") expect_identical(result$selected_component, "line") expect_equal(result$byte_count, length(sparse)) expect_identical(result$nul_byte_ratio, 1 / length(sparse)) established <- read_appusage_lines(path, encoding = "UTF-8") expect_identical(as.character(result$lines), as.character(established)) expected <- parse_line(established, input = "lines", source_file = "synthetic") actual <- parse_line(path, source_file = "synthetic", encoding = "UTF-8") expect_identical(actual, expected) first <- run_first_level_appusage(path, encoding = "UTF-8", write_cache = FALSE) expect_identical(first$status, "success") expect_identical(make_second_level_appusage(first$data, export_type = "line"), make_second_level_appusage(parse_line(established, input = "lines", source_file = basename(path)), export_type = "line")) }) test_that("NUL cleanup follows binary gates and leaves ordinary decoding intact", { high <- tempfile(fileext = ".txt") plain <- tempfile(fileext = ".txt") on.exit(unlink(c(high, plain)), add = TRUE) writeBin(c(charToRaw(paste(rep("text", 100), collapse = "")), as.raw(rep(0, 100))), high) result <- appusage_source_preflight(high) expect_identical(result$status, "binary_control_bytes") expect_identical(result$failure_family, "source_binary") expect_identical(result$lines, character()) writeLines("ordinary internal log", plain, useBytes = TRUE) result <- appusage_source_preflight(plain) expect_identical(result$status, "unknown_content") expect_identical(result$nul_byte_ratio, 0) expect_identical(as.character(result$lines), as.character(read_appusage_lines(plain))) })