Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

chore(task_sources): add store and import tests

Steven Enamakel60035765bcbe
diff --git a/crates/openhuman-core/src/integrations/task_sources/store_import_tests.rs b/crates/openhuman-core/src/integrations/task_sources/store_import_tests.rs
index 3c0671b85d..ea5662a6c2 100644
--- a/crates/openhuman-core/src/integrations/task_sources/store_import_tests.rs
+++ b/crates/openhuman-core/src/integrations/task_sources/store_import_tests.rs
@@ -2,23 +2,24 @@ use super::*;
use crate::integrations::composio::contract::NormalizedTask;
use crate::integrations::task_sources::store as live;
use crate::integrations::task_sources::types::{FilterSpec, ProviderSlug, SourceTarget};
-use crate::storage::local::{forget, table_names};
-use crate::storage::rusqlite;
+use crate::storage::import::{run_with, Outcome};
use serde_json::json;
use tempfile::TempDir;
-fn workspace() -> (TempDir, Config, Config) {
+fn workspace() -> (TempDir, Config) {
let dir = TempDir::new().unwrap();
- let mut classic = Config {
+ let config = Config {
workspace_dir: dir.path().to_path_buf(),
..Config::default()
};
- classic.storage.url = Some("classic".into());
- let default = Config {
- workspace_dir: dir.path().to_path_buf(),
- ..Config::default()
- };
- (dir, classic, default)
+ (dir, config)
+}
+
+fn import(config: &Config) -> Result<Outcome> {
+ let backend = crate::storage::local::backend_for(&config.workspace_dir).unwrap();
+ run_with(&backend, config, &[super::super::import_source()])
+ .remove(0)
+ .outcome
}
fn filter() -> FilterSpec {
@@ -41,47 +42,69 @@ fn task(external_id: &str, title: &str) -> NormalizedTask {
}
}
-#[test]
-fn legacy_sources_and_ledger_are_imported_once_through_the_public_api() {
- let (_dir, classic, default) = workspace();
- let first = live::add_source(
- &classic,
- ProviderSlug::Github,
- Some("conn-1".into()),
- Some("My issues".into()),
- filter(),
- 1800,
- SourceTarget::AgentTodoProactive,
- 25,
- )
- .unwrap();
- let second = live::add_source(
- &classic,
- ProviderSlug::Github,
- None,
- None,
- filter(),
- 600,
- SourceTarget::AgentTodoProactive,
- 10,
- )
+/// Writes a `task_sources` row the way the retired store did.
+fn seed_source(config: &Config, id: &str, name: Option<&str>, conn_id: Option<&str>, interval: i64) {
+ with_connection(config, |conn| {
+ conn.execute(
+ "INSERT INTO task_sources
+ (id, provider, connection_id, name, enabled, filter, interval_secs, target,
+ max_tasks_per_fetch, created_at, last_fetch_at, last_status)
+ VALUES (?1, 'github', ?2, ?3, 1, ?4, ?5, ?6, 25, '2026-01-01T00:00:00+00:00',
+ ?7, ?8)",
+ params![
+ id,
+ conn_id,
+ name,
+ serde_json::to_string(&filter()).unwrap(),
+ interval,
+ serde_json::to_string(&SourceTarget::AgentTodoProactive).unwrap(),
+ name.map(|_| "2026-01-02T00:00:00+00:00"),
+ name.map(|_| "periodic: ok"),
+ ],
+ )?;
+ Ok(())
+ })
.unwrap();
- live::mark_ingested(&classic, &first.id, &task("t-1", "First")).unwrap();
- live::mark_ingested(&classic, &first.id, &task("t-2", "Second")).unwrap();
- live::mark_ingested(&classic, &second.id, &task("t-1", "Other")).unwrap();
- live::record_fetch(
- &classic,
- &first.id,
- chrono::Utc::now(),
- crate::integrations::task_sources::FetchReason::Periodic,
- "ok",
- )
+}
+
+fn seed_ingested(config: &Config, source_id: &str, t: &NormalizedTask) {
+ with_connection(config, |conn| {
+ conn.execute(
+ "INSERT INTO ingested_tasks
+ (source_id, external_id, content_hash, title, payload, ingested_at)
+ VALUES (?1, ?2, ?3, ?4, ?5, '2026-01-03T00:00:00+00:00')",
+ params![
+ source_id,
+ t.external_id,
+ live::content_hash(t),
+ t.title,
+ serde_json::to_string(t).unwrap(),
+ ],
+ )?;
+ Ok(())
+ })
.unwrap();
- let db = live::db_path(&classic);
+}
- let sources = live::list_sources(&default).unwrap();
+#[test]
+fn legacy_sources_and_ledger_are_imported_once_and_the_file_is_left_alone() {
+ let (_dir, config) = workspace();
+ seed_source(&config, "src-1", Some("My issues"), Some("conn-1"), 1800);
+ seed_source(&config, "src-2", None, None, 600);
+ seed_ingested(&config, "src-1", &task("t-1", "First"));
+ seed_ingested(&config, "src-1", &task("t-2", "Second"));
+ seed_ingested(&config, "src-2", &task("t-1", "Other"));
+ let db = db_path(&config);
+ let before = std::fs::read(&db).unwrap();
+
+ assert!(matches!(
+ import(&config).unwrap(),
+ Outcome::Imported { found: 5, written: 5 }
+ ));
+
+ let sources = live::list_sources(&config).unwrap();
assert_eq!(sources.len(), 2);
- let got = live::get_source(&default, &first.id).unwrap();
+ let got = live::get_source(&config, "src-1").unwrap();
assert_eq!(got.name.as_deref(), Some("My issues"));
assert_eq!(got.connection_id.as_deref(), Some("conn-1"));
assert_eq!(got.interval_secs, 1800);
@@ -89,119 +112,48 @@ fn legacy_sources_and_ledger_are_imported_once_through_the_public_api() {
assert_eq!(got.filter, filter());
assert!(got.last_fetch_at.is_some());
assert_eq!(got.last_status.as_deref(), Some("periodic: ok"));
- assert!(live::was_ingested(&default, &first.id, "t-1").unwrap());
- assert!(live::was_ingested(&default, &second.id, "t-1").unwrap());
+ assert!(live::was_ingested(&config, "src-1", "t-1").unwrap());
+ assert!(live::was_ingested(&config, "src-2", "t-1").unwrap());
assert!(live::is_ingested(
- &default,
- &first.id,
+ &config,
+ "src-1",
"t-1",
&live::content_hash(&task("t-1", "First"))
)
.unwrap());
- let titles: Vec<_> = live::list_ingested(&default, &first.id, 10)
+ let titles: Vec<_> = live::list_ingested(&config, "src-1", 10)
.unwrap()
.into_iter()
.map(|t| t.title)
.collect();
assert_eq!(titles.len(), 2);
assert!(titles.contains(&"First".to_string()));
- assert_eq!(
- live::list_ingested_refs(&default, &first.id).unwrap().len(),
- 2
- );
-
- let tables = table_names(&db);
- assert!(tables.contains(&"_legacy_task_sources".to_string()));
- assert!(tables.contains(&"_legacy_ingested_tasks".to_string()));
- assert!(!tables.contains(&"task_sources".to_string()));
+ assert_eq!(live::list_ingested_refs(&config, "src-1").unwrap().len(), 2);
+ assert_eq!(std::fs::read(&db).unwrap(), before, "sources.db untouched");
- // A restart does not import again.
- live::remove_source(&default, &second.id).unwrap();
- forget(&db);
- assert_eq!(live::list_sources(&default).unwrap().len(), 1);
+ // A second run does not import again.
+ live::remove_source(&config, "src-2").unwrap();
+ assert!(matches!(import(&config).unwrap(), Outcome::AlreadyDone));
+ assert_eq!(live::list_sources(&config).unwrap().len(), 1);
}
#[test]
-fn switching_between_classic_and_default_keeps_working() {
- let (_dir, classic, default) = workspace();
- let add = |config: &Config, interval: u64| {
- live::add_source(
- config,
- ProviderSlug::Github,
- None,
- None,
- filter(),
- interval,
- SourceTarget::AgentTodoProactive,
- 10,
- )
- .unwrap()
- };
- add(&classic, 600);
- assert_eq!(live::list_sources(&default).unwrap().len(), 1);
- // The tables were retired; a classic call starts a clean legacy schema
- // (it does not trust a stale version stamp) ...
- assert!(live::list_sources(&classic).unwrap().is_empty());
- add(&classic, 900);
- // ... and the next default call imports what it holds, on top of what the
- // document tables already had.
- assert_eq!(live::list_sources(&default).unwrap().len(), 2);
-}
-
-#[test]
-fn a_deleted_database_is_reopened_not_served_from_a_stale_handle() {
- let (_dir, classic, default) = workspace();
- live::add_source(
- &classic,
- ProviderSlug::Github,
- None,
- None,
- filter(),
- 600,
- SourceTarget::AgentTodoProactive,
- 10,
- )
- .unwrap();
- assert_eq!(live::list_sources(&default).unwrap().len(), 1);
- // A data reset removes the workspace while this process keeps running.
- let db = live::db_path(&default);
- for suffix in ["", "-wal", "-shm"] {
- let mut file = db.clone().into_os_string();
- file.push(suffix);
- let _ = std::fs::remove_file(std::path::PathBuf::from(file));
- }
- assert!(live::list_sources(&default).unwrap().is_empty());
- assert!(db.exists(), "the store was created again");
-}
-
-#[test]
-fn an_undecodable_source_row_keeps_the_legacy_table_in_service() {
- let (_dir, classic, default) = workspace();
- live::add_source(
- &classic,
- ProviderSlug::Github,
- None,
- None,
- filter(),
- 600,
- SourceTarget::AgentTodoProactive,
- 10,
- )
- .unwrap();
- let db = live::db_path(&classic);
- rusqlite::Connection::open(&db)
- .unwrap()
- .execute(
+fn an_undecodable_source_row_fails_the_import_and_it_is_retried() {
+ let (_dir, config) = workspace();
+ seed_source(&config, "good", Some("ok"), None, 600);
+ with_connection(&config, |conn| {
+ conn.execute(
"INSERT INTO task_sources (id, provider, enabled, filter, interval_secs, target,
max_tasks_per_fetch, created_at)
VALUES ('bad', 'github', 1, 'not json', 600, '{}', 10, '2026-01-01T00:00:00Z')",
[],
- )
- .unwrap();
- // The import fails, so nothing is retired and the call reports it.
- let error = live::list_sources(&default).unwrap_err();
- assert!(format!("{error:#}").contains("importing the legacy tables failed"));
- let tables = table_names(&db);
- assert!(tables.contains(&"task_sources".to_string()));
- assert!(!tables.contains(&"_legacy_task_sources".to_string()));
+ )?;
+ Ok(())
+ })
+ .unwrap();
+
+ let error = import(&config).unwrap_err();
+ assert!(format!("{error:#}").contains("decode a task_sources row"), "{error:#}");
+ // No marker was written, so nothing is imported and the next boot retries.
+ assert!(live::list_sources(&config).unwrap().is_empty());
}
diff --git a/crates/openhuman-core/src/integrations/task_sources/store_tests.rs b/crates/openhuman-core/src/integrations/task_sources/store_tests.rs
index 1a5e2b3e1e..dc361188a5 100644
--- a/crates/openhuman-core/src/integrations/task_sources/store_tests.rs
+++ b/crates/openhuman-core/src/integrations/task_sources/store_tests.rs
@@ -1,18 +1,15 @@
use super::*;
use crate::config::Config;
-use crate::storage::rusqlite;
use serde_json::json;
use tempfile::TempDir;
fn test_config(tmp: &TempDir) -> Config {
- let mut config = Config {
+ let config = Config {
workspace_dir: tmp.path().join("workspace"),
action_dir: tmp.path().join("workspace"),
config_path: tmp.path().join("config.toml"),
..Config::default()
};
- // These tests exercise the legacy SQLite tables themselves.
- config.storage.url = Some("classic".into());
std::fs::create_dir_all(&config.workspace_dir).unwrap();
config
}
@@ -298,138 +295,3 @@ fn clear_all_removes_every_source() {
assert!(list_sources(&config).unwrap().is_empty());
}
-/// Regression: gating the DDL behind a per-path "already initialized" set
-/// (see [`INITIALIZED_SCHEMAS`]) must not cost the store its self-healing.
-///
-/// Before the gate existed, the DDL ran on every `with_connection` call, so a
-/// database deleted or replaced at runtime (a workspace reset, a manual
-/// deletion, a disk-recovery restore) recovered on the very next call —
-/// `Connection::open` creates a fresh empty file and `CREATE TABLE IF NOT
-/// EXISTS` repopulates it. With a naive cache the set still reports
-/// "initialized" while the file behind it is empty, and every query afterwards
-/// fails `no such table: task_sources` until the process restarts. This pins
-/// the verify-on-hit in `ensure_schema_initialized` that restores it.
-#[test]
-fn schema_reinitializes_when_the_database_file_is_deleted_at_runtime() {
- let tmp = TempDir::new().unwrap();
- let config = test_config(&tmp);
-
- // First use populates the per-path cache and creates the schema.
- let src = add_source(
- &config,
- ProviderSlug::Github,
- None,
- Some("before-deletion".into()),
- github_filter(),
- 1800,
- SourceTarget::TodoOnly,
- 25,
- )
- .unwrap();
- assert_eq!(
- list_sources(&config).unwrap().len(),
- 1,
- "sanity: the source was persisted"
- );
-
- // Simulate a workspace reset / manual deletion while the process lives on.
- let db_path = config.workspace_dir.join("task_sources").join("sources.db");
- assert!(
- db_path.exists(),
- "sanity: the task_sources db exists before deletion"
- );
- std::fs::remove_file(&db_path).unwrap();
- // This store runs in WAL mode, so the sidecars must go too or SQLite can
- // resurrect pages from them.
- let _ = std::fs::remove_file(db_path.with_extension("db-wal"));
- let _ = std::fs::remove_file(db_path.with_extension("db-shm"));
-
- // The cache still says this path is initialized. Without the verify-on-hit
- // this errors with `no such table: task_sources`.
- let after = list_sources(&config)
- .expect("a deleted database must be re-initialized, not left wedged at 'no such table'");
- assert!(
- after.is_empty(),
- "the recreated database starts empty — the prior source is genuinely gone"
- );
- // The prior source's id no longer resolves against the fresh db.
- assert!(get_source(&config, &src.id).is_err());
-
- // And the store is fully usable again, not merely readable.
- let recreated = add_source(
- &config,
- ProviderSlug::Github,
- None,
- Some("after-deletion".into()),
- github_filter(),
- 1800,
- SourceTarget::TodoOnly,
- 25,
- )
- .unwrap();
- assert_eq!(get_source(&config, &recreated.id).unwrap().id, recreated.id);
- assert_eq!(list_sources(&config).unwrap().len(), 1);
-}
-
-/// Regression (CodeRabbit / Codex on #5709): a cache hit must be validated
-/// against the *whole* schema, not just the presence of one table. A database
-/// replaced at runtime with an older/partial schema — `task_sources` present but
-/// a migrated column dropped — must be re-migrated, not trusted and then failed
-/// on the incomplete schema. The `PRAGMA user_version` check is what catches it.
-#[test]
-fn older_on_disk_schema_under_a_cached_path_is_remigrated() {
- let tmp = TempDir::new().unwrap();
- let config = test_config(&tmp);
-
- // First use creates the full (versioned) schema and caches the path.
- let original = add_source(
- &config,
- ProviderSlug::Github,
- None,
- Some("v1".into()),
- github_filter(),
- 1800,
- SourceTarget::TodoOnly,
- 25,
- )
- .unwrap();
-
- // Simulate a workspace restore of an OLDER database swapped in under the
- // same (already-cached) path: drop the migrated card-id column and clear the version
- // stamp, exactly as a pre-migration database would look on disk.
- let db_path = config.workspace_dir.join("task_sources").join("sources.db");
- {
- let raw = rusqlite::Connection::open(&db_path).unwrap();
- raw.execute_batch(
- "ALTER TABLE ingested_tasks DROP COLUMN card_id;
- PRAGMA user_version = 0;",
- )
- .unwrap();
- }
-
- // The path is still cached. With a single-table `sqlite_master` probe this
- // would be trusted and the next ingested-task write would fail with `no
- // such column`. The version check detects the drift and
- // re-migrates instead.
- let listed = list_sources(&config)
- .expect("an older on-disk schema under a cached path must be re-migrated, not trusted");
- assert_eq!(
- listed.len(),
- 1,
- "the pre-existing row survives the schema repair"
- );
- assert_eq!(listed[0].id, original.id);
- // And the store is fully usable again.
- let src = add_source(
- &config,
- ProviderSlug::Github,
- None,
- Some("v2".into()),
- github_filter(),
- 1800,
- SourceTarget::TodoOnly,
- 25,
- )
- .unwrap();
- assert_eq!(get_source(&config, &src.id).unwrap().id, src.id);
-}