#![allow(dead_code)]
use cooklang_sync_client::connection::{get_connection_pool, ConnectionPool};
use std::path::PathBuf;
use tempfile::TempDir;
use tokio::fs;
pub fn fresh_client_pool() -> (ConnectionPool, TempDir) {
let dir = TempDir::new().expect("create tempdir");
let db_path = dir.path().join("client.sqlite3");
let db_path_str = db_path.to_str().expect("tempdir path is utf-8").to_string();
let pool = get_connection_pool(&db_path_str).expect("build connection pool");
(pool, dir)
}
pub async fn tempdir_with_files(files: &[(&str, &[u8])]) -> TempDir {
let dir = TempDir::new().expect("create tempdir");
for (rel, bytes) in files {
let path: PathBuf = dir.path().join(rel);
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).await.expect("mkdir -p");
}
fs::write(&path, bytes).await.expect("write file");
}
dir
}
pub fn sample_jwt(uid: i32) -> String {
let header = base64_url_nopad(br#"{"alg":"HS256","typ":"JWT"}"#);
let payload_json = format!(r#"{{"uid":{},"exp":4102444800}}"#, uid);
let payload = base64_url_nopad(payload_json.as_bytes());
let signature = base64_url_nopad(b"test-signature-unverified");
format!("{}.{}.{}", header, payload, signature)
}
fn base64_url_nopad(bytes: &[u8]) -> String {
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use base64::Engine;
URL_SAFE_NO_PAD.encode(bytes)
}
use cooklang_sync_client::chunker::{Chunker, InMemoryCache};
pub struct ClientBase {
pub pool: cooklang_sync_client::connection::ConnectionPool,
pub dir: TempDir,
pub chunker: Chunker,
}
pub fn client_base() -> ClientBase {
let (pool, dir) = fresh_client_pool();
let cache = InMemoryCache::new(100, 10_000_000);
let chunker = Chunker::new(cache, dir.path().to_path_buf());
ClientBase { pool, dir, chunker }
}