usesuper::*;/// RULE: The Domains package validates as a package would at import, its tables are plain data tables, and the Cloudflare mapping names only columns those tables have.////// A mapping that drifts from the tables is caught here, not on the owner's/// Mac at 3 a.m.#[test]fn the_domains_package_is_valid_and_the_cloudflare_mapping_fits_its_tables() {let package = package(); package.tool.validate().expect("the tool validates"); assert_eq!(package.tool.id, TOOL_ID); assert_eq!(package.tool.tables, vec![DOMAIN, DNS_RECORD]);for table in &package.tables { table.validate().unwrap_or_else(|error| panic!("{}: {error}", table.id)); assert!(!table.builtin, "{} is data, not an engine table", table.id); assert!(table.engine.is_none()); }let mapping = cloudflare_mapping(); mapping.validate(&package.tables).expect("the mapping fits the tables"); assert_eq!(mapping.tables(), vec![DNS_RECORD, DOMAIN]); assert!(mapping.calls.iter().all(|call| call.tool == "execute" && call.args.contains_key("code")));}#[test]fn cloudflare_is_proposed_for_its_own_api_server_and_a_skeleton_for_anyone_else() {let cloudflare = vec![serde_json::json!({"name":"search"}), serde_json::json!({"name":"execute"})];let proposal = propose("https://mcp.cloudflare.com/mcp", &cloudflare); assert_eq!(proposal["ready"], true); assert_eq!(proposal["installs"], serde_json::json!([TOOL_ID])); assert_eq!(Mapping::parse(&proposal["mapping"]).unwrap(), cloudflare_mapping());// Cloudflare's narrower servers speak other tools; a skeleton from those.let analytics = vec![serde_json::json!({"name":"zones_list","description":"List zones under the current active account."})];let proposal = propose("https://dns-analytics.mcp.cloudflare.com/mcp", &analytics); assert_eq!(proposal["ready"], false); assert_eq!(proposal["mapping"]["calls"][0]["tool"], "zones_list"); assert_eq!(proposal["installs"], serde_json::json!([]));}// RULE: Cloudflare's API server is dialled with its truncation off, because a cut answer loses rows; every other server is dialled exactly as given.#[test]fn cloudflare_is_dialled_with_truncation_off_and_others_as_given() { assert_eq!(dial_url("https://mcp.cloudflare.com/mcp"), "https://mcp.cloudflare.com/mcp?truncateToolResult=false"); assert_eq!(dial_url("https://mcp.cloudflare.com/mcp?truncateToolResult=true"), "https://mcp.cloudflare.com/mcp?truncateToolResult=true"); assert_eq!(dial_url("https://mcp.example-notes.test/mcp"), "https://mcp.example-notes.test/mcp");}/// Runs one of the mapping's scripts under node against a fake/// `cloudflare.request` holding `zones` zones (one DNS record each) and two/// accounts — one with `domains` registrar domains, one without the registrar.fn run_against_fake(code: &str, zones: usize, domains: usize) -> Vec<Value> {let harness = format!(r#"const zones = {zones}, domains = {domains};const pageOf = (items, query) => {{ const per = Number(query.get('per_page')), page = Number(query.get('page')); return {{ result: items.slice((page - 1) * per, page * per), result_info: {{ page, per_page: per, total_count: items.length, total_pages: Math.ceil(items.length / per) }} }};}};const cloudflare = {{ request: async ({{ path }}) => {{ const url = new URL(path, 'https://api.test'); const zone = url.pathname.match(/^\/zones\/(z\d+)\/dns_records$/); if (url.pathname === '/zones') return pageOf(Array.from({{ length: zones }}, (_, i) => ({{ id: `z${{i}}`, name: `zone${{i}}.test` }})), url.searchParams); if (zone) return pageOf([{{ id: `r-${{zone[1]}}`, zone_id: zone[1] }}], url.searchParams); if (url.pathname === '/accounts') return pageOf([{{ id: 'a1' }}, {{ id: 'a2' }}], url.searchParams); if (url.pathname === '/accounts/a1/registrar/domains') return pageOf(Array.from({{ length: domains }}, (_, i) => ({{ name: `d${{i}}.test` }})), url.searchParams); if (url.pathname === '/accounts/a2/registrar/domains') throw new Error('registrar not enabled'); throw new Error(`unexpected ${{path}}`);}} }};({code})().then((out) => console.log(JSON.stringify(out)), (e) => {{ console.error(e); process.exit(1); }});"# );let output = std::process::Command::new("node") .args(["-e", &harness]) .output() .expect("node runs the Cloudflare scripts (it is on every builder and the CI machines)"); assert!(output.status.success(), "the script failed: {}", String::from_utf8_lossy(&output.stderr)); serde_json::from_slice(&output.stdout).expect("the script answers a JSON list")}// RULE: Every Cloudflare list is read to its last page, a partial one too: 2,451 zones are 49 full pages and one zone, and all 2,451 come back with their records.#[test]fn every_page_is_read_to_the_last_partial_one() {let zones = run_against_fake(ZONES, 2451, 0); assert_eq!(zones.len(), 2451); assert_eq!(zones.last().unwrap()["id"], "z2450"); assert_eq!(run_against_fake(RECORDS, 2451, 0).len(), 2451); assert_eq!(run_against_fake(ZONES, 120, 0).len(), 120); assert_eq!(run_against_fake(ZONES, 50, 0).len(), 50);// One account's 51 domains are two pages; the account without the registrar is skipped. assert_eq!(run_against_fake(REGISTRAR, 0, 51).len(), 51);}/// RULE: A Cloudflare account's zone and its registration are one domain row, DNS at says Cloudflare only for a zone Cloudflare answers for, and a registration with no zone is still a row.////// Two patches to one row would be two synced writes each read and a moment/// when the row says half; and "DNS at Cloudflare" on a zone still waiting on/// its nameservers would tell the owner a move had happened that has not.#[test]fn an_accounts_zones_and_registrations_shape_into_one_row_each() {let zones = [ json!({"id":"z1","name":"example.com","status":"active","plan":{"name":"Free Website"},"name_servers":["ada.ns.cloudflare.com"],"created_on":"2024-05-01T10:00:00Z","account":{"name":"An account"},"original_registrar":"another registrar"}), json!({"id":"z2","name":"example.org","status":"pending","name_servers":["ada.ns.cloudflare.com"],"original_registrar":"another registrar"}), ];let registrar = [ json!({"name":"example.com","expires_at":"2027-04-30T00:00:00Z","auto_renew":true}), json!({"name":"example.net","expires_at":"2028-01-02T00:00:00Z","auto_renew":false}), ];let records = [json!({"id":"r1","zone_id":"z1","name":"www.example.com","type":"CNAME","content":"example.pages.dev","ttl":1,"proxied":true})];let rows = shape_account(&zones, ®istrar, &records).expect("shapes"); assert_eq!(rows.zones.len(), 2);let held = &rows.zones[0]; assert_eq!(held.key, "z1"); assert_eq!(held.data["dns_at"], "cloudflare"); assert_eq!(held.data["registrar"], "cloudflare", "the account registers it: not what Cloudflare saw before"); assert_eq!(held.data["expires"], "2027-04-30"); assert_eq!(held.data["auto_renew"], true);let waiting = &rows.zones[1]; assert!(!waiting.data.contains_key("dns_at"), "a pending zone's DNS is not at Cloudflare yet: {:?}", waiting.data); assert_eq!(waiting.data["registrar"], "another registrar"); assert!(!waiting.data.contains_key("expires")); assert_eq!(rows.unzoned.len(), 1, "a registration with no zone is its own row"); assert_eq!(rows.unzoned[0].key, "example.net"); assert_eq!(rows.unzoned[0].data["expires"], "2028-01-02"); assert_eq!(rows.records.len(), 1); assert_eq!(rows.records[0].links[0].3, "z1");let package = package();let domain = package.tables.iter().find(|table| table.id == DOMAIN).unwrap();for shaped in rows.zones.iter().chain(&rows.unzoned) {for column in shaped.data.keys() { assert!(domain.columns.iter().any(|held| held.key == *column), "{column} is not a column of domain"); } }}