use std::collections::HashMap;
use std::fs::{self, OpenOptions};
use std::io::{ErrorKind, Write};
use std::path::{Path, PathBuf};
use std::sync::{Mutex, MutexGuard, PoisonError};
use anyhow::{Context, Result, anyhow, bail};
use base64::Engine;
use base64::prelude::BASE64_STANDARD;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use crate::redact::redact;
use crate::sealed::{self, SealError, SealKeys, seal_error};
pub const MANIFEST_FILE: &str = "manifest.jsonl";
pub const SEALED_MANIFEST_FILE: &str = "manifest.sealed";
const JOURNAL_FILE: &str = "removal.journal";
const RESEAL_MARKER: &str = "reseal.pending";
const BLOB_SUFFIX: &str = ".jsonl.zst";
const LEGACY_TMP_SUFFIX: &str = ".tmp";
const COMPACTION_SLACK: usize = 64;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Archived {
Stored,
Refreshed,
Unchanged,
Copy,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ArchiveEntry {
pub source: String,
pub file: String,
pub sha256: String,
pub archived_at: DateTime<Utc>,
}
pub struct TranscriptArchive {
dir: PathBuf,
state: Mutex<State>,
}
struct State {
entries: HashMap<String, ArchiveEntry>,
manifest_lines: usize,
plaintext_lines: usize,
manifest_damaged: bool,
unsealed: bool,
keys: Option<SealKeys>,
}
impl State {
fn needs_compaction(&self) -> bool {
self.manifest_damaged || self.manifest_lines > 2 * self.entries.len() + COMPACTION_SLACK
}
fn plaintext_pending(&self) -> bool {
self.keys.is_some() && self.plaintext_lines > 0
}
fn holder_of(&self, sha256: &str) -> Option<&ArchiveEntry> {
self.entries.values().find(|e| e.sha256 == sha256)
}
fn references(&self, file: &str) -> bool {
self.entries.values().any(|e| e.file == file)
}
}
impl TranscriptArchive {
pub fn open(dir: impl Into<PathBuf>) -> Result<Self> {
Self::open_with(dir.into(), None)
}
pub fn open_sealed(dir: impl Into<PathBuf>, keys: SealKeys) -> Result<Self> {
Self::open_with(dir.into(), Some(keys))
}
fn open_with(dir: PathBuf, keys: Option<SealKeys>) -> Result<Self> {
fs::create_dir_all(&dir)
.with_context(|| format!("creating the transcript archive at {}", dir.display()))?;
let manifest = dir.join(MANIFEST_FILE);
if !manifest.exists() {
sealed::write_file(&manifest, None, MANIFEST_FILE, b"")
.with_context(|| format!("creating {}", manifest.display()))?;
}
let (state, torn, superseded) = load_manifest(&dir, keys)?;
let archive = Self {
dir,
state: Mutex::new(state),
};
{
let mut state = archive.state();
if torn || state.needs_compaction() || state.plaintext_pending() {
archive.compact(&mut state)?;
}
for file in superseded.iter().filter(|f| !state.references(f)) {
archive.remove_blob(file);
}
archive.finish_interrupted_removal(&state)?;
}
Ok(archive)
}
pub fn holds_unsealed(&self) -> bool {
self.state().unsealed
}
pub fn seal_keys(&self) -> Option<SealKeys> {
self.state().keys.clone()
}
pub fn set_seal_keys(&self, keys: SealKeys) {
self.state().keys = Some(keys);
}
pub fn reseal(&self) -> Result<usize> {
if self.state().keys.is_none() {
return Err(no_keys());
}
let mut resealed = 0;
for file in self.blob_names()? {
let state = self.state();
let keys = state.keys.clone().ok_or_else(no_keys)?;
let path = self.dir.join(&file);
if path.exists() && sealed::reseal_file(&path, &keys, &file)? {
resealed += 1;
}
}
let mut state = self.state();
let keys = state.keys.clone().ok_or_else(no_keys)?;
let journal = self.dir.join(JOURNAL_FILE);
if journal.exists() && sealed::reseal_file(&journal, &keys, JOURNAL_FILE)? {
resealed += 1;
}
if !self.manifest_sealed_under(&keys)? {
self.compact(&mut state)?;
resealed += 1;
}
self.remove_tmp_files()?;
let marker = self.dir.join(RESEAL_MARKER);
match fs::remove_file(&marker) {
Ok(()) => {}
Err(e) if e.kind() == ErrorKind::NotFound => {}
Err(e) => return Err(e).with_context(|| format!("removing {}", marker.display())),
}
state.unsealed = false;
Ok(resealed)
}
pub fn remove_where(
&self,
doomed: impl Fn(&ArchiveEntry) -> bool,
) -> Result<Vec<ArchiveEntry>> {
let mut state = self.state();
if state.plaintext_pending() {
self.compact(&mut state)?;
}
let removed: Vec<ArchiveEntry> = state
.entries
.values()
.filter(|e| doomed(e))
.cloned()
.collect();
if removed.is_empty() {
return Ok(removed);
}
for entry in &removed {
state.entries.remove(&entry.source);
}
let mut doomed_blobs: Vec<&str> = removed
.iter()
.map(|e| e.file.as_str())
.filter(|file| !state.references(file))
.collect();
doomed_blobs.sort_unstable();
doomed_blobs.dedup();
if !doomed_blobs.is_empty() {
let journal = self.dir.join(JOURNAL_FILE);
sealed::write_file(
&journal,
state.keys.as_ref(),
JOURNAL_FILE,
doomed_blobs.join("\n").as_bytes(),
)
.with_context(|| format!("writing {}", journal.display()))?;
}
self.compact(&mut state)?;
for file in &doomed_blobs {
self.remove_blob(file);
}
self.remove_journal();
Ok(removed)
}
pub fn copy_into(&self, dest: &Path) -> Result<()> {
self.copy_into_with(dest, || {})
}
fn copy_into_with(&self, dest: &Path, between_phases: impl FnOnce()) -> Result<()> {
crate::private_files::create_private_dir(dest)?;
for file in self.blob_names()? {
copy_if_present(&self.dir.join(&file), &dest.join(&file))?;
}
between_phases();
let state = self.state();
for name in [
MANIFEST_FILE,
SEALED_MANIFEST_FILE,
JOURNAL_FILE,
RESEAL_MARKER,
] {
copy_if_present(&self.dir.join(name), &dest.join(name))?;
}
for entry in state.entries.values() {
let target = dest.join(&entry.file);
if !target.exists() {
crate::dir_copy::copy_file_synced(&self.dir.join(&entry.file), &target)?;
}
}
for entry in fs::read_dir(dest).with_context(|| format!("listing {}", dest.display()))? {
let name = entry?.file_name().to_string_lossy().into_owned();
if name.ends_with(BLOB_SUFFIX) && !state.references(&name) {
let path = dest.join(&name);
fs::remove_file(&path).with_context(|| format!("removing {}", path.display()))?;
}
}
drop(state);
crate::dir_copy::sync_dir(dest)
}
pub fn holder(&self, sha256: &str) -> Option<ArchiveEntry> {
self.state().holder_of(sha256).cloned()
}
pub fn dir(&self) -> &Path {
&self.dir
}
pub fn archive(&self, source: &Path) -> Result<Archived> {
let mut state = self.state();
let bytes = fs::read(source).with_context(|| format!("reading {}", source.display()))?;
let sha256 = hex(&Sha256::digest(&bytes));
let key = source_key(source);
let previous = state.entries.get(&key).cloned();
if let Some(prev) = &previous
&& prev.sha256 == sha256
&& self.dir.join(&prev.file).exists()
{
return Ok(Archived::Unchanged);
}
let file = format!("{sha256}{BLOB_SUFFIX}");
let shared = state
.holder_of(&sha256)
.is_some_and(|holder| self.dir.join(&holder.file).exists());
if !shared {
self.write_blob(state.keys.as_ref(), &file, &bytes)?;
}
let entry = ArchiveEntry {
source: key.clone(),
file,
sha256,
archived_at: Utc::now(),
};
state.entries.insert(key, entry.clone());
if state.needs_compaction() {
self.compact(&mut state)?;
} else if let Err(e) = self.append(&mut state, &entry) {
state.manifest_damaged = true;
return Err(e);
}
if let Some(prev) = &previous
&& !state.references(&prev.file)
{
self.remove_blob(&prev.file);
}
Ok(match (shared, previous) {
(true, _) => Archived::Copy,
(false, None) => Archived::Stored,
(false, Some(_)) => Archived::Refreshed,
})
}
pub fn entry(&self, source: &Path) -> Option<ArchiveEntry> {
self.state().entries.get(&source_key(source)).cloned()
}
pub fn read(&self, source: &Path) -> Result<Option<String>> {
let (entry, keys) = {
let state = self.state();
(
state.entries.get(&source_key(source)).cloned(),
state.keys.clone(),
)
};
let Some(entry) = entry else {
return Ok(None);
};
let path = self.dir.join(&entry.file);
let compressed = sealed::read_file(&path, keys.as_ref(), &entry.file)?;
let bytes = zstd::decode_all(compressed.as_slice())
.with_context(|| format!("decompressing {}", path.display()))?;
let text =
String::from_utf8(bytes).with_context(|| format!("decoding {}", path.display()))?;
Ok(Some(text))
}
pub fn entries(&self) -> Vec<ArchiveEntry> {
self.state().entries.values().cloned().collect()
}
pub fn len(&self) -> usize {
self.state().entries.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
fn state(&self) -> MutexGuard<'_, State> {
self.state.lock().unwrap_or_else(PoisonError::into_inner)
}
fn write_blob(&self, keys: Option<&SealKeys>, file: &str, source_bytes: &[u8]) -> Result<()> {
let text = redact(&String::from_utf8_lossy(source_bytes)).text;
let compressed =
zstd::encode_all(text.as_bytes(), 0).context("compressing a transcript")?;
let target = self.dir.join(file);
sealed::write_file(&target, keys, file, &compressed)
.with_context(|| format!("writing {}", target.display()))
}
fn blob_names(&self) -> Result<Vec<String>> {
let mut names = Vec::new();
for entry in
fs::read_dir(&self.dir).with_context(|| format!("listing {}", self.dir.display()))?
{
let name = entry
.with_context(|| format!("listing {}", self.dir.display()))?
.file_name()
.to_string_lossy()
.into_owned();
if name.ends_with(BLOB_SUFFIX) {
names.push(name);
}
}
Ok(names)
}
fn manifest_sealed_under(&self, keys: &SealKeys) -> Result<bool> {
let plaintext = read_manifest_text(&self.dir.join(MANIFEST_FILE))?;
if plaintext.lines().any(|l| !l.trim().is_empty()) {
return Ok(false);
}
let text = read_manifest_text(&self.dir.join(SEALED_MANIFEST_FILE))?;
Ok(text.lines().filter(|l| !l.trim().is_empty()).all(|line| {
BASE64_STANDARD
.decode(line.trim())
.is_ok_and(|bytes| sealed::key_id_of(&bytes) == Some(keys.current_id()))
}))
}
fn remove_tmp_files(&self) -> Result<()> {
for entry in
fs::read_dir(&self.dir).with_context(|| format!("listing {}", self.dir.display()))?
{
let entry = entry.with_context(|| format!("listing {}", self.dir.display()))?;
let name = entry.file_name().to_string_lossy().into_owned();
if sealed::is_tmp_name(&name) || name.ends_with(LEGACY_TMP_SUFFIX) {
fs::remove_file(entry.path())
.or_else(|e| match e.kind() {
ErrorKind::NotFound => Ok(()),
_ => Err(e),
})
.with_context(|| format!("removing {}", entry.path().display()))?;
}
}
Ok(())
}
fn finish_interrupted_removal(&self, state: &State) -> Result<()> {
let journal = self.dir.join(JOURNAL_FILE);
if !journal.exists() {
return Ok(());
}
let bytes = sealed::read_file(&journal, state.keys.as_ref(), JOURNAL_FILE)?;
let text =
String::from_utf8(bytes).with_context(|| format!("decoding {}", journal.display()))?;
for name in text.lines().map(str::trim) {
let is_blob_name = name.ends_with(BLOB_SUFFIX) && !name.contains(['/', '\\']);
if is_blob_name && !state.references(name) {
self.remove_blob(name);
}
}
self.remove_journal();
Ok(())
}
fn remove_journal(&self) {
let path = self.dir.join(JOURNAL_FILE);
match fs::remove_file(&path) {
Ok(()) => {}
Err(e) if e.kind() == ErrorKind::NotFound => {}
Err(e) => {
tracing::warn!(path = %path.display(), error = %e, "removing the archive removal journal")
}
}
}
fn remove_blob(&self, file: &str) {
let path = self.dir.join(file);
match fs::remove_file(&path) {
Ok(()) => {}
Err(e) if e.kind() == ErrorKind::NotFound => {}
Err(e) => {
tracing::warn!(path = %path.display(), error = %e, "removing an unused archive blob")
}
}
}
fn append(&self, state: &mut State, entry: &ArchiveEntry) -> Result<()> {
let path = self.dir.join(manifest_file(state.keys.as_ref()));
let mut line = manifest_line(state.keys.as_ref(), entry)?;
line.push('\n');
let mut file = OpenOptions::new()
.create(true)
.append(true)
.open(&path)
.with_context(|| format!("opening {}", path.display()))?;
file.write_all(line.as_bytes())
.and_then(|()| file.sync_data())
.with_context(|| format!("appending to {}", path.display()))?;
state.manifest_lines += 1;
if state.keys.is_none() {
state.plaintext_lines += 1;
state.unsealed = true;
}
Ok(())
}
fn compact(&self, state: &mut State) -> Result<()> {
let mut entries: Vec<&ArchiveEntry> = state.entries.values().collect();
entries.sort_by(|a, b| a.source.cmp(&b.source));
let mut body = String::new();
for entry in &entries {
body.push_str(&manifest_line(state.keys.as_ref(), entry)?);
body.push('\n');
}
let count = entries.len();
let name = manifest_file(state.keys.as_ref());
let path = self.dir.join(name);
sealed::write_file(&path, None, name, body.as_bytes())
.with_context(|| format!("compacting {}", path.display()))?;
if state.keys.is_some() {
let plaintext = self.dir.join(MANIFEST_FILE);
if fs::metadata(&plaintext).is_ok_and(|m| m.len() > 0) {
let marker = self.dir.join(RESEAL_MARKER);
sealed::write_file(&marker, None, RESEAL_MARKER, b"")
.with_context(|| format!("writing {}", marker.display()))?;
sealed::write_file(&plaintext, None, MANIFEST_FILE, b"")
.with_context(|| format!("emptying {}", plaintext.display()))?;
}
state.plaintext_lines = 0;
} else {
state.plaintext_lines = count;
state.unsealed |= count > 0;
}
state.manifest_lines = count;
state.manifest_damaged = false;
Ok(())
}
}
fn copy_if_present(from: &Path, to: &Path) -> Result<()> {
match crate::dir_copy::copy_file_synced(from, to) {
Err(e) if crate::dir_copy::is_not_found(&e) => Ok(()),
other => other,
}
}
fn no_keys() -> anyhow::Error {
anyhow!("the transcript archive has no sealing keys to reseal under")
}
fn manifest_file(keys: Option<&SealKeys>) -> &'static str {
match keys {
Some(_) => SEALED_MANIFEST_FILE,
None => MANIFEST_FILE,
}
}
fn manifest_line(keys: Option<&SealKeys>, entry: &ArchiveEntry) -> Result<String> {
let json = serde_json::to_string(entry)?;
match keys {
Some(keys) => {
Ok(BASE64_STANDARD.encode(sealed::seal(keys, SEALED_MANIFEST_FILE, json.as_bytes())?))
}
None => Ok(json),
}
}
fn parse_plaintext_line(line: &str) -> Result<ArchiveEntry> {
Ok(serde_json::from_str(line)?)
}
fn parse_sealed_line(line: &str, keys: Option<&SealKeys>) -> Result<ArchiveEntry> {
let sealed_bytes = BASE64_STANDARD.decode(line.trim())?;
if !sealed::looks_sealed(&sealed_bytes) {
bail!("the line is not a sealed record");
}
let keys = keys.ok_or(SealError::KeyRequired)?;
Ok(serde_json::from_slice(&sealed::open(
keys,
SEALED_MANIFEST_FILE,
&sealed_bytes,
)?)?)
}
fn read_manifest_text(path: &Path) -> Result<String> {
match fs::read_to_string(path) {
Ok(text) => Ok(text),
Err(e) if e.kind() == ErrorKind::NotFound => Ok(String::new()),
Err(e) => Err(e).with_context(|| format!("reading {}", path.display())),
}
}
fn read_manifest(
path: &Path,
parse: impl Fn(&str) -> Result<ArchiveEntry>,
) -> Result<(HashMap<String, ArchiveEntry>, usize, bool)> {
let text = read_manifest_text(path)?;
let mut entries = HashMap::new();
let mut lines = 0;
let mut torn = !text.is_empty() && !text.ends_with('\n');
for line in text.lines().filter(|l| !l.trim().is_empty()) {
lines += 1;
match parse(line) {
Ok(entry) => {
entries.insert(entry.source.clone(), entry);
}
Err(e) if is_missing_key(&e) => {
return Err(e).with_context(|| format!("reading {}", path.display()));
}
Err(e) => {
torn = true;
tracing::warn!(path = %path.display(), error = %e, "ignoring an unreadable archive manifest line");
}
}
}
Ok((entries, lines, torn))
}
fn load_manifest(dir: &Path, keys: Option<SealKeys>) -> Result<(State, bool, Vec<String>)> {
let (mut entries, sealed_lines, sealed_torn) =
read_manifest(&dir.join(SEALED_MANIFEST_FILE), |line| {
parse_sealed_line(line, keys.as_ref())
})?;
let (plaintext, plaintext_lines, plaintext_torn) =
read_manifest(&dir.join(MANIFEST_FILE), parse_plaintext_line)?;
let unsealed = !plaintext.is_empty() || dir.join(RESEAL_MARKER).exists();
let mut superseded = Vec::new();
for (source, entry) in plaintext {
match entries.get(&source) {
Some(held) if held.archived_at > entry.archived_at => superseded.push(entry.file),
Some(held) => {
superseded.push(held.file.clone());
entries.insert(source, entry);
}
None => {
entries.insert(source, entry);
}
}
}
let state = State {
entries,
manifest_lines: sealed_lines + plaintext_lines,
plaintext_lines,
manifest_damaged: false,
unsealed,
keys,
};
Ok((state, sealed_torn || plaintext_torn, superseded))
}
fn is_missing_key(error: &anyhow::Error) -> bool {
matches!(
seal_error(error),
Some(SealError::KeyRequired | SealError::UnknownKey(_))
)
}
fn source_key(source: &Path) -> String {
source.to_string_lossy().into_owned()
}
fn hex(bytes: &[u8]) -> String {
bytes.iter().map(|b| format!("{b:02x}")).collect()
}
#[cfg(test)]
mod tests {
use std::fs;
use std::io::Write;
use base64::Engine;
use base64::prelude::BASE64_STANDARD;
use super::*;
use crate::keys::KeyGuard;
use crate::sealed::{self, SealError, SealKey, seal_error};
fn blobs(archive: &TranscriptArchive) -> Vec<String> {
let mut names: Vec<String> = fs::read_dir(archive.dir())
.unwrap()
.map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
.filter(|n| n.ends_with(".jsonl.zst"))
.collect();
names.sort();
names
}
fn manifest_lines(archive: &TranscriptArchive) -> Vec<String> {
fs::read_to_string(archive.dir().join(MANIFEST_FILE))
.unwrap()
.lines()
.map(str::to_string)
.collect()
}
struct Fixture {
_tmp: tempfile::TempDir,
projects: PathBuf,
archive_dir: PathBuf,
}
impl Fixture {
fn new() -> Self {
let tmp = tempfile::tempdir().unwrap();
let projects = tmp.path().join("projects");
fs::create_dir_all(&projects).unwrap();
let archive_dir = tmp.path().join("data").join("transcripts");
Self {
_tmp: tmp,
projects,
archive_dir,
}
}
fn write(&self, name: &str, text: &str) -> PathBuf {
let path = self.projects.join(name);
fs::write(&path, text).unwrap();
path
}
fn open(&self) -> TranscriptArchive {
TranscriptArchive::open(&self.archive_dir).unwrap()
}
}
#[test]
fn open_creates_the_directory() {
let fx = Fixture::new();
let archive = fx.open();
assert!(archive.dir().is_dir());
assert!(archive.is_empty());
}
#[test]
fn a_new_source_is_stored_then_unchanged() {
let fx = Fixture::new();
let source = fx.write("a.jsonl", "{\"line\":1}\n");
let archive = fx.open();
assert_eq!(archive.archive(&source).unwrap(), Archived::Stored);
assert_eq!(archive.archive(&source).unwrap(), Archived::Unchanged);
assert_eq!(archive.len(), 1);
assert_eq!(blobs(&archive).len(), 1);
assert_eq!(manifest_lines(&archive).len(), 1);
let entry = archive.entry(&source).unwrap();
assert_eq!(entry.source, source.to_string_lossy());
assert_eq!(entry.file, format!("{}.jsonl.zst", entry.sha256));
assert_eq!(entry.sha256.len(), 64);
assert_eq!(
archive.read(&source).unwrap().as_deref(),
Some("{\"line\":1}\n")
);
}
#[test]
fn a_changed_source_is_refreshed_and_its_old_blob_removed() {
let fx = Fixture::new();
let source = fx.write("a.jsonl", "{\"line\":1}\n");
let archive = fx.open();
archive.archive(&source).unwrap();
let old = archive.entry(&source).unwrap();
fs::write(&source, "{\"line\":1}\n{\"line\":2}\n").unwrap();
assert_eq!(archive.archive(&source).unwrap(), Archived::Refreshed);
let new = archive.entry(&source).unwrap();
assert_ne!(new.sha256, old.sha256);
assert_eq!(
archive.read(&source).unwrap().as_deref(),
Some("{\"line\":1}\n{\"line\":2}\n")
);
assert_eq!(blobs(&archive), vec![new.file]);
assert!(!archive.dir().join(&old.file).exists());
}
#[test]
fn a_copy_at_another_path_shares_the_blob() {
let fx = Fixture::new();
let first = fx.write("a.jsonl", "{\"same\":true}\n");
let second = fx.write("b.jsonl", "{\"same\":true}\n");
let archive = fx.open();
assert_eq!(archive.archive(&first).unwrap(), Archived::Stored);
assert_eq!(archive.archive(&second).unwrap(), Archived::Copy);
assert_eq!(archive.len(), 2);
assert_eq!(blobs(&archive).len(), 1);
assert_eq!(
archive.entry(&first).unwrap().file,
archive.entry(&second).unwrap().file
);
assert_eq!(
archive.read(&second).unwrap().as_deref(),
Some("{\"same\":true}\n")
);
}
#[test]
fn refreshing_one_copy_keeps_the_blob_the_other_still_uses() {
let fx = Fixture::new();
let first = fx.write("a.jsonl", "{\"same\":true}\n");
let second = fx.write("b.jsonl", "{\"same\":true}\n");
let archive = fx.open();
archive.archive(&first).unwrap();
archive.archive(&second).unwrap();
fs::write(&first, "{\"same\":false}\n").unwrap();
assert_eq!(archive.archive(&first).unwrap(), Archived::Refreshed);
assert_eq!(blobs(&archive).len(), 2);
assert_eq!(
archive.read(&second).unwrap().as_deref(),
Some("{\"same\":true}\n")
);
}
#[test]
fn the_archive_outlives_a_deleted_source() {
let fx = Fixture::new();
let source = fx.write("a.jsonl", "{\"kept\":true}\n");
let archive = fx.open();
archive.archive(&source).unwrap();
fs::remove_file(&source).unwrap();
assert!(archive.archive(&source).is_err());
assert_eq!(
archive.read(&source).unwrap().as_deref(),
Some("{\"kept\":true}\n")
);
assert_eq!(blobs(&archive).len(), 1);
let reopened = fx.open();
assert_eq!(
reopened.read(&source).unwrap().as_deref(),
Some("{\"kept\":true}\n")
);
}
#[test]
fn an_unknown_source_reads_as_none() {
let fx = Fixture::new();
let archive = fx.open();
let missing = fx.projects.join("never.jsonl");
assert!(archive.entry(&missing).is_none());
assert_eq!(archive.read(&missing).unwrap(), None);
}
#[test]
fn archived_text_is_redacted() {
let fx = Fixture::new();
let secret = format!("gh{}_{}", "p", "aB3dE6".repeat(6));
let source = fx.write(
"a.jsonl",
&format!("{{\"text\":\"token {secret} and the build passed\"}}\n"),
);
let archive = fx.open();
archive.archive(&source).unwrap();
let text = archive.read(&source).unwrap().unwrap();
assert!(!text.contains(&secret));
assert!(text.contains("the build passed"));
assert!(text.contains("[REDACTED:github_token]"));
let entry = archive.entry(&source).unwrap();
let raw = fs::read(archive.dir().join(&entry.file)).unwrap();
let decoded = String::from_utf8(zstd::decode_all(raw.as_slice()).unwrap()).unwrap();
assert!(!decoded.contains(&secret));
assert!(decoded.contains("the build passed"));
}
#[test]
fn the_blob_is_named_by_the_source_bytes() {
let fx = Fixture::new();
let secret = format!("gh{}_{}", "p", "aB3dE6".repeat(6));
let source = fx.write("a.jsonl", &format!("{secret}\n"));
let archive = fx.open();
archive.archive(&source).unwrap();
let changed = format!("gh{}_{}", "p", "zY9xW8".repeat(6));
fs::write(&source, format!("{changed}\n")).unwrap();
assert_eq!(archive.archive(&source).unwrap(), Archived::Refreshed);
}
#[test]
fn reopening_loads_the_manifest() {
let fx = Fixture::new();
let first = fx.write("a.jsonl", "{\"a\":1}\n");
let second = fx.write("b.jsonl", "{\"b\":2}\n");
let archive = fx.open();
archive.archive(&first).unwrap();
archive.archive(&second).unwrap();
let entry = archive.entry(&first).unwrap();
drop(archive);
let reopened = fx.open();
assert_eq!(reopened.len(), 2);
assert_eq!(reopened.entry(&first), Some(entry));
assert_eq!(reopened.archive(&first).unwrap(), Archived::Unchanged);
assert_eq!(
reopened.read(&second).unwrap().as_deref(),
Some("{\"b\":2}\n")
);
}
#[test]
fn a_torn_last_manifest_line_is_ignored() {
let fx = Fixture::new();
let first = fx.write("a.jsonl", "{\"a\":1}\n");
let second = fx.write("b.jsonl", "{\"b\":2}\n");
let archive = fx.open();
archive.archive(&first).unwrap();
archive.archive(&second).unwrap();
drop(archive);
let mut manifest = fs::OpenOptions::new()
.append(true)
.open(fx.archive_dir.join(MANIFEST_FILE))
.unwrap();
manifest.write_all(b"{\"source\":\"/x/c.js").unwrap();
drop(manifest);
let reopened = fx.open();
assert_eq!(reopened.len(), 2);
let third = fx.write("c.jsonl", "{\"c\":3}\n");
assert_eq!(reopened.archive(&third).unwrap(), Archived::Stored);
drop(reopened);
let again = fx.open();
assert_eq!(again.len(), 3);
assert_eq!(again.read(&third).unwrap().as_deref(), Some("{\"c\":3}\n"));
}
#[test]
fn a_failed_manifest_append_is_repaired_by_the_next_write() {
use std::os::unix::fs::PermissionsExt;
let fx = Fixture::new();
let first = fx.write("a.jsonl", "{\"a\":1}\n");
let second = fx.write("b.jsonl", "{\"b\":2}\n");
let third = fx.write("c.jsonl", "{\"c\":3}\n");
let archive = fx.open();
archive.archive(&first).unwrap();
let manifest = fx.archive_dir.join(MANIFEST_FILE);
fs::set_permissions(&manifest, fs::Permissions::from_mode(0o400)).unwrap();
assert!(archive.archive(&second).is_err());
assert_eq!(archive.archive(&third).unwrap(), Archived::Stored);
drop(archive);
let reopened = fx.open();
assert_eq!(reopened.len(), 3);
assert_eq!(
reopened.read(&second).unwrap().as_deref(),
Some("{\"b\":2}\n")
);
}
#[test]
fn compaction_keeps_the_latest_entry_per_source() {
let fx = Fixture::new();
let first = fx.write("a.jsonl", "{\"a\":1}\n");
let second = fx.write("b.jsonl", "{\"b\":1}\n");
let archive = fx.open();
archive.archive(&first).unwrap();
archive.archive(&second).unwrap();
let latest_second = archive.entry(&second).unwrap();
drop(archive);
let manifest = fx.archive_dir.join(MANIFEST_FILE);
let stale = ArchiveEntry {
source: first.to_string_lossy().into_owned(),
file: "0".repeat(64) + ".jsonl.zst",
sha256: "0".repeat(64),
archived_at: Utc::now(),
};
let mut lines = fs::read_to_string(&manifest).unwrap();
let mut stale_lines = String::new();
for _ in 0..80 {
stale_lines.push_str(&serde_json::to_string(&stale).unwrap());
stale_lines.push('\n');
}
lines.insert_str(0, &stale_lines);
fs::write(&manifest, &lines).unwrap();
let reopened = fx.open();
let kept = manifest_lines(&reopened);
assert_eq!(kept.len(), 2);
assert_eq!(reopened.len(), 2);
assert_ne!(reopened.entry(&first).unwrap().sha256, stale.sha256);
assert_eq!(reopened.entry(&second), Some(latest_second));
assert_eq!(
reopened.read(&first).unwrap().as_deref(),
Some("{\"a\":1}\n")
);
}
#[test]
fn a_long_running_archive_keeps_its_manifest_compact() {
let fx = Fixture::new();
let source = fx.write("a.jsonl", "");
let archive = fx.open();
let mut text = String::new();
for i in 0..100 {
text.push_str(&format!("{{\"line\":{i}}}\n"));
fs::write(&source, &text).unwrap();
archive.archive(&source).unwrap();
}
assert!(manifest_lines(&archive).len() <= 2 + 64);
assert_eq!(blobs(&archive).len(), 1);
drop(archive);
let reopened = fx.open();
assert_eq!(reopened.read(&source).unwrap(), Some(text));
}
#[test]
fn removed_entries_leave_the_manifest_and_their_blobs_and_stay_gone_on_reopen() {
let fx = Fixture::new();
let gone = fx.write("gone.jsonl", "{\"line\":\"secret\"}\n");
let copy = fx.write("copy.jsonl", "{\"line\":\"secret\"}\n");
let kept = fx.write("kept.jsonl", "{\"line\":\"kept\"}\n");
let archive = fx.open();
for source in [&gone, ©, &kept] {
archive.archive(source).unwrap();
}
let sha = archive.entry(&gone).unwrap().sha256;
assert!(archive.holder(&sha).is_some());
let removed = archive.remove_where(|e| e.source.contains("gone")).unwrap();
assert_eq!(removed.len(), 1);
assert!(
archive.holder(&sha).is_some(),
"the copy still uses the blob"
);
assert_eq!(blobs(&archive).len(), 2);
archive.remove_where(|e| e.source.contains("copy")).unwrap();
assert!(archive.holder(&sha).is_none());
assert_eq!(blobs(&archive).len(), 1);
assert!(archive.entry(&gone).is_none());
assert!(manifest_lines(&archive).iter().all(|l| !l.contains("gone")));
let reopened = fx.open();
assert_eq!(reopened.len(), 1);
assert!(reopened.entry(&kept).is_some());
}
fn stray_blob(archive: &TranscriptArchive, fill: char) -> PathBuf {
let path = archive
.dir()
.join(format!("{}{BLOB_SUFFIX}", fill.to_string().repeat(64)));
fs::write(&path, b"unreferenced").unwrap();
path
}
#[test]
fn a_blob_no_entry_or_journal_names_survives_open() {
let fx = Fixture::new();
let source = fx.write("a.jsonl", "{\"line\":1}\n");
let archive = fx.open();
archive.archive(&source).unwrap();
let stray = stray_blob(&archive, 'f');
drop(archive);
let reopened = fx.open();
assert!(stray.exists());
assert_eq!(blobs(&reopened).len(), 2);
}
#[test]
fn a_journal_left_by_a_crash_after_compaction_has_its_blobs_deleted_at_open() {
let fx = Fixture::new();
let kept = fx.write("kept.jsonl", "{\"line\":\"kept\"}\n");
let archive = fx.open();
archive.archive(&kept).unwrap();
let doomed = stray_blob(&archive, 'a');
let bystander = stray_blob(&archive, 'b');
let kept_blob = archive.entry(&kept).unwrap().file;
let names = format!(
"{}\n{kept_blob}\n",
doomed.file_name().unwrap().to_string_lossy()
);
fs::write(archive.dir().join(JOURNAL_FILE), names).unwrap();
drop(archive);
let reopened = fx.open();
assert!(!doomed.exists());
assert!(bystander.exists());
assert!(reopened.dir().join(&kept_blob).exists());
assert!(!reopened.dir().join(JOURNAL_FILE).exists());
assert_eq!(reopened.len(), 1);
}
#[test]
fn remove_where_leaves_no_journal_behind() {
let fx = Fixture::new();
let source = fx.write("a.jsonl", "{\"line\":1}\n");
let archive = fx.open();
archive.archive(&source).unwrap();
archive.remove_where(|_| true).unwrap();
assert!(!archive.dir().join(JOURNAL_FILE).exists());
assert!(blobs(&archive).is_empty());
}
#[test]
fn a_missing_manifest_never_deletes_blobs() {
let fx = Fixture::new();
let source = fx.write("a.jsonl", "{\"line\":1}\n");
let archive = fx.open();
archive.archive(&source).unwrap();
let dir = archive.dir().to_path_buf();
drop(archive);
fs::remove_file(dir.join(MANIFEST_FILE)).unwrap();
let reopened = fx.open();
assert!(reopened.is_empty());
assert_eq!(blobs(&reopened).len(), 1);
}
fn key(byte: u8) -> SealKey {
SealKey::from_bytes(KeyGuard::new([byte; 32])).unwrap()
}
fn keys(byte: u8) -> SealKeys {
SealKeys::single(key(byte))
}
fn files(dir: &Path) -> Vec<PathBuf> {
let mut paths: Vec<PathBuf> = fs::read_dir(dir)
.unwrap()
.map(|e| e.unwrap().path())
.collect();
paths.sort();
paths
}
fn contains(haystack: &[u8], needle: &[u8]) -> bool {
haystack.windows(needle.len()).any(|w| w == needle)
}
fn assert_all_sealed_under(dir: &Path, keys: &SealKeys) {
for path in files(dir) {
let name = path.file_name().unwrap().to_string_lossy().into_owned();
if name == MANIFEST_FILE {
assert_eq!(fs::read_to_string(&path).unwrap(), "");
} else if name == SEALED_MANIFEST_FILE {
let text = fs::read_to_string(&path).unwrap();
for line in text.lines() {
let sealed = BASE64_STANDARD.decode(line).unwrap();
assert_eq!(sealed::key_id_of(&sealed), Some(keys.current_id()));
}
} else {
assert_eq!(
sealed::sealed_key_id(&path).unwrap(),
Some(keys.current_id()),
"{name}"
);
}
}
}
impl Fixture {
fn open_sealed(&self, keys: SealKeys) -> TranscriptArchive {
TranscriptArchive::open_sealed(&self.archive_dir, keys).unwrap()
}
}
#[test]
fn a_sealed_archive_holds_neither_the_transcript_text_nor_its_source_path() {
let fx = Fixture::new();
let text = "{\"text\":\"the quarterly migration plan\"}\n".repeat(4);
let source = fx.write("orchard-ledger.jsonl", &text);
let archive = fx.open_sealed(keys(1));
archive.archive(&source).unwrap();
let path_bytes = source.to_string_lossy().into_owned();
for path in files(archive.dir()) {
let raw = fs::read(&path).unwrap();
assert!(!contains(&raw, b"quarterly migration"), "{path:?}");
assert!(!contains(&raw, b"orchard-ledger"), "{path:?}");
assert!(!contains(&raw, path_bytes.as_bytes()), "{path:?}");
assert!(zstd::decode_all(raw.as_slice()).is_err(), "{path:?}");
}
assert_all_sealed_under(archive.dir(), &keys(1));
assert_eq!(archive.read(&source).unwrap(), Some(text.clone()));
drop(archive);
let reopened = fx.open_sealed(keys(1));
assert_eq!(reopened.read(&source).unwrap(), Some(text));
}
#[test]
fn a_copy_opens_with_every_entry_under_the_same_keys() {
let fx = Fixture::new();
let a = fx.write("a.jsonl", "{\"line\":\"alpha\"}\n");
let b = fx.write("b.jsonl", "{\"line\":\"beta\"}\n");
let archive = fx.open_sealed(keys(5));
archive.archive(&a).unwrap();
archive.archive(&b).unwrap();
let dest = fx._tmp.path().join("backup-transcripts");
archive.copy_into(&dest).unwrap();
let copy = TranscriptArchive::open_sealed(&dest, keys(5)).unwrap();
assert_eq!(copy.len(), 2);
assert_eq!(copy.read(&a).unwrap(), archive.read(&a).unwrap());
assert_eq!(copy.read(&b).unwrap(), archive.read(&b).unwrap());
}
#[test]
fn a_blob_removed_while_the_copy_runs_leaves_nothing_in_it() {
let fx = Fixture::new();
let a = fx.write("a.jsonl", "{\"line\":\"the deleted session\"}\n");
let b = fx.write("b.jsonl", "{\"line\":\"kept\"}\n");
let archive = fx.open_sealed(keys(6));
archive.archive(&a).unwrap();
archive.archive(&b).unwrap();
let doomed = archive.entry(&a).unwrap();
let dest = fx._tmp.path().join("backup-transcripts");
archive
.copy_into_with(&dest, || {
archive.remove_where(|e| e.source == doomed.source).unwrap();
})
.unwrap();
assert!(!dest.join(&doomed.file).exists());
let copy = TranscriptArchive::open_sealed(&dest, keys(6)).unwrap();
assert_eq!(copy.len(), 1);
assert_eq!(copy.read(&a).unwrap(), None);
assert!(copy.read(&b).unwrap().is_some());
}
#[test]
fn a_copy_of_an_archive_awaiting_reseal_still_awaits_it() {
let fx = Fixture::new();
let a = fx.write("a.jsonl", "{\"line\":1}\n");
fx.open().archive(&a).unwrap();
let archive = fx.open_sealed(keys(7));
assert!(archive.holds_unsealed());
let dest = fx._tmp.path().join("backup-transcripts");
archive.copy_into(&dest).unwrap();
let copy = TranscriptArchive::open_sealed(&dest, keys(7)).unwrap();
assert!(copy.holds_unsealed());
assert!(copy.read(&a).unwrap().is_some());
}
#[test]
fn a_sealed_archive_does_not_open_without_its_key() {
let fx = Fixture::new();
let source = fx.write("a.jsonl", "{\"line\":1}\n");
let archive = fx.open_sealed(keys(2));
archive.archive(&source).unwrap();
drop(archive);
let err = TranscriptArchive::open(&fx.archive_dir)
.err()
.expect("an unkeyed open fails");
assert_eq!(seal_error(&err), Some(&SealError::KeyRequired));
let err = TranscriptArchive::open_sealed(&fx.archive_dir, keys(3))
.err()
.expect("another key fails");
assert!(matches!(seal_error(&err), Some(SealError::UnknownKey(_))));
assert_eq!(blobs(&fx.open_sealed(keys(2))).len(), 1);
}
#[test]
fn a_plaintext_archive_opens_sealed_and_reseal_seals_every_file() {
let fx = Fixture::new();
let first = fx.write("a.jsonl", "{\"a\":1}\n");
let copy = fx.write("b.jsonl", "{\"a\":1}\n");
let second = fx.write("c.jsonl", "{\"c\":3}\n");
let plain = fx.open();
for source in [&first, ©, &second] {
plain.archive(source).unwrap();
}
drop(plain);
fs::write(fx.archive_dir.join("manifest.jsonl.tmp"), "{\"source\":").unwrap();
let archive = fx.open_sealed(keys(4));
assert_eq!(archive.read(©).unwrap().as_deref(), Some("{\"a\":1}\n"));
assert_eq!(
archive.read(&second).unwrap().as_deref(),
Some("{\"c\":3}\n")
);
assert!(manifest_lines(&archive).is_empty());
assert_eq!(archive.reseal().unwrap(), 2);
assert_all_sealed_under(archive.dir(), &keys(4));
assert_eq!(archive.reseal().unwrap(), 0);
assert_eq!(
archive.read(&first).unwrap().as_deref(),
Some("{\"a\":1}\n")
);
drop(archive);
let reopened = fx.open_sealed(keys(4));
assert_eq!(reopened.len(), 3);
assert_eq!(
reopened.read(&second).unwrap().as_deref(),
Some("{\"c\":3}\n")
);
}
#[test]
fn a_rotation_then_reseal_leaves_only_the_new_key() {
let fx = Fixture::new();
let first = fx.write("a.jsonl", "{\"a\":1}\n");
let second = fx.write("b.jsonl", "{\"b\":2}\n");
let archive = fx.open_sealed(keys(5));
archive.archive(&first).unwrap();
archive.set_seal_keys(SealKeys::new(key(6), vec![key(5)]));
archive.archive(&second).unwrap();
assert_eq!(
archive.read(&first).unwrap().as_deref(),
Some("{\"a\":1}\n")
);
assert_eq!(archive.reseal().unwrap(), 2);
assert_all_sealed_under(archive.dir(), &keys(6));
drop(archive);
let reopened = fx.open_sealed(keys(6));
assert_eq!(reopened.len(), 2);
assert_eq!(
reopened.read(&first).unwrap().as_deref(),
Some("{\"a\":1}\n")
);
assert_eq!(
reopened.read(&second).unwrap().as_deref(),
Some("{\"b\":2}\n")
);
}
#[test]
fn a_torn_sealed_manifest_line_is_ignored() {
let fx = Fixture::new();
let first = fx.write("a.jsonl", "{\"a\":1}\n");
let second = fx.write("b.jsonl", "{\"b\":2}\n");
let archive = fx.open_sealed(keys(7));
archive.archive(&first).unwrap();
archive.archive(&second).unwrap();
let line = sealed_manifest_lines(archive.dir())[0].clone();
drop(archive);
for cut in [line.len() / 2, line.len() - 3] {
let mut manifest = fs::OpenOptions::new()
.append(true)
.open(fx.archive_dir.join(SEALED_MANIFEST_FILE))
.unwrap();
manifest.write_all(&line.as_bytes()[..cut]).unwrap();
drop(manifest);
let reopened = fx.open_sealed(keys(7));
assert_eq!(reopened.len(), 2);
assert_eq!(sealed_manifest_lines(reopened.dir()).len(), 2);
}
let reopened = fx.open_sealed(keys(7));
let third = fx.write("c.jsonl", "{\"c\":3}\n");
assert_eq!(reopened.archive(&third).unwrap(), Archived::Stored);
drop(reopened);
let again = fx.open_sealed(keys(7));
assert_eq!(again.len(), 3);
assert_eq!(again.read(&third).unwrap().as_deref(), Some("{\"c\":3}\n"));
assert_all_sealed_under(again.dir(), &keys(7));
}
#[test]
fn removal_works_sealed_and_an_interrupted_removal_is_finished_from_a_sealed_journal() {
let fx = Fixture::new();
let gone = fx.write("gone.jsonl", "{\"line\":\"gone\"}\n");
let kept = fx.write("kept.jsonl", "{\"line\":\"kept\"}\n");
let archive = fx.open_sealed(keys(8));
archive.archive(&gone).unwrap();
archive.archive(&kept).unwrap();
let removed = archive.remove_where(|e| e.source.contains("gone")).unwrap();
assert_eq!(removed.len(), 1);
assert_eq!(blobs(&archive).len(), 1);
assert!(!archive.dir().join(JOURNAL_FILE).exists());
let doomed = stray_blob(&archive, 'a');
let bystander = stray_blob(&archive, 'b');
let names = format!("{}\n", doomed.file_name().unwrap().to_string_lossy());
sealed::write_file(
&archive.dir().join(JOURNAL_FILE),
Some(&keys(8)),
JOURNAL_FILE,
names.as_bytes(),
)
.unwrap();
drop(archive);
let reopened = fx.open_sealed(keys(8));
assert!(!doomed.exists());
assert!(bystander.exists());
assert!(!reopened.dir().join(JOURNAL_FILE).exists());
assert_eq!(reopened.len(), 1);
assert_eq!(
reopened.read(&kept).unwrap().as_deref(),
Some("{\"line\":\"kept\"}\n")
);
assert!(manifest_lines(&reopened).is_empty());
assert_eq!(sealed_manifest_lines(reopened.dir()).len(), 1);
}
#[test]
fn a_sealed_journal_is_not_read_without_the_key() {
let fx = Fixture::new();
let archive = fx.open();
let doomed = stray_blob(&archive, 'c');
let names = format!("{}\n", doomed.file_name().unwrap().to_string_lossy());
sealed::write_file(
&archive.dir().join(JOURNAL_FILE),
Some(&keys(9)),
JOURNAL_FILE,
names.as_bytes(),
)
.unwrap();
drop(archive);
let err = TranscriptArchive::open(&fx.archive_dir)
.err()
.expect("an unkeyed open fails");
assert_eq!(seal_error(&err), Some(&SealError::KeyRequired));
assert!(doomed.exists());
}
fn sealed_manifest_lines(dir: &Path) -> Vec<String> {
match fs::read_to_string(dir.join(SEALED_MANIFEST_FILE)) {
Ok(text) => text.lines().map(str::to_string).collect(),
Err(e) if e.kind() == ErrorKind::NotFound => Vec::new(),
Err(e) => panic!("{e}"),
}
}
fn line_from_a_binary_before_sealing(
fx: &Fixture,
source: &Path,
archived_at: DateTime<Utc>,
) -> String {
let bytes = fs::read(source).unwrap();
let sha256 = hex(&Sha256::digest(&bytes));
let file = format!("{sha256}{BLOB_SUFFIX}");
fs::write(
fx.archive_dir.join(&file),
zstd::encode_all(bytes.as_slice(), 0).unwrap(),
)
.unwrap();
serde_json::to_string(&ArchiveEntry {
source: source_key(source),
file,
sha256,
archived_at,
})
.unwrap()
}
#[test]
fn a_sealed_archive_leaves_the_plaintext_manifest_readable_by_a_binary_from_before_sealing() {
let fx = Fixture::new();
let first = fx.write("a.jsonl", "{\"a\":1}\n");
let plain = fx.open();
plain.archive(&first).unwrap();
drop(plain);
let archive = fx.open_sealed(keys(10));
let second = fx.write("b.jsonl", "{\"b\":2}\n");
archive.archive(&second).unwrap();
archive.reseal().unwrap();
let third = fx.write("c.jsonl", "{\"c\":3}\n");
archive.archive(&third).unwrap();
for line in manifest_lines(&archive) {
serde_json::from_str::<ArchiveEntry>(&line).unwrap();
}
assert_eq!(sealed_manifest_lines(archive.dir()).len(), 3);
}
#[test]
fn entries_a_binary_from_before_sealing_appended_are_merged_and_then_sealed() {
let fx = Fixture::new();
let kept = fx.write("kept.jsonl", "{\"k\":1}\n");
let changed = fx.write("changed.jsonl", "{\"v\":1}\n");
let archive = fx.open_sealed(keys(11));
archive.archive(&kept).unwrap();
archive.archive(&changed).unwrap();
let old_blob = archive.entry(&changed).unwrap().file;
drop(archive);
let added = fx.write("added.jsonl", "{\"n\":1}\n");
fs::write(&changed, "{\"v\":2}\n").unwrap();
let later = Utc::now() + chrono::Duration::seconds(1);
let lines = [
line_from_a_binary_before_sealing(&fx, &added, later),
line_from_a_binary_before_sealing(&fx, &changed, later),
];
let mut manifest = fs::OpenOptions::new()
.append(true)
.open(fx.archive_dir.join(MANIFEST_FILE))
.unwrap();
for line in &lines {
writeln!(manifest, "{line}").unwrap();
}
drop(manifest);
let reopened = fx.open_sealed(keys(11));
assert_eq!(reopened.len(), 3);
assert_eq!(
reopened.read(&added).unwrap().as_deref(),
Some("{\"n\":1}\n")
);
assert_eq!(
reopened.read(&changed).unwrap().as_deref(),
Some("{\"v\":2}\n")
);
assert_eq!(
reopened.read(&kept).unwrap().as_deref(),
Some("{\"k\":1}\n")
);
assert!(!reopened.dir().join(&old_blob).exists());
assert!(manifest_lines(&reopened).is_empty());
assert!(reopened.holds_unsealed());
reopened.reseal().unwrap();
assert!(!reopened.holds_unsealed());
assert_all_sealed_under(reopened.dir(), &keys(11));
drop(reopened);
let again = fx.open_sealed(keys(11));
assert_eq!(again.len(), 3);
assert!(!again.holds_unsealed());
}
#[test]
fn a_removal_after_keys_arrive_leaves_no_plaintext_entry_behind() {
let fx = Fixture::new();
let gone = fx.write("gone.jsonl", "{\"line\":\"gone\"}\n");
let kept = fx.write("kept.jsonl", "{\"line\":\"kept\"}\n");
let archive = fx.open();
archive.archive(&gone).unwrap();
archive.archive(&kept).unwrap();
archive.set_seal_keys(keys(12));
archive.remove_where(|e| e.source.contains("gone")).unwrap();
assert!(manifest_lines(&archive).is_empty());
drop(archive);
let reopened = fx.open_sealed(keys(12));
assert_eq!(reopened.len(), 1);
assert!(reopened.entry(&gone).is_none());
}
#[test]
fn a_fold_cut_short_before_its_reseal_still_reports_unsealed_files_after_a_reopen() {
let fx = Fixture::new();
let first = fx.write("a.jsonl", "{\"a\":1}\n");
let plain = fx.open();
plain.archive(&first).unwrap();
drop(plain);
let folded = fx.open_sealed(keys(13));
assert!(folded.holds_unsealed());
assert!(manifest_lines(&folded).is_empty());
drop(folded);
let reopened = fx.open_sealed(keys(13));
assert!(reopened.holds_unsealed());
reopened.reseal().unwrap();
assert_all_sealed_under(reopened.dir(), &keys(13));
drop(reopened);
assert!(!fx.open_sealed(keys(13)).holds_unsealed());
}
}