From d71e430981daf8d901bf9952880c141fa3407e3c Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 21 Nov 2026 04:17:12 +0000 Subject: [PATCH 1/7] fix(fleet): let GraphQL board reads fail over to the App identity Treat a gh api graphql call whose inline query is a { } selection or a named query as read-only, and count GraphQL's 'Could not resolve to a node' answer as a permission refusal, so board reads try the next identity. Mutations, file or stdin queries and unknown shapes stay writes. Refs #12154 Co-Authored-By: Claude Opus 5.5 --- pkgs/fleet/crates/fleet/src/gh.rs | 141 +++++++++++++++++++++++++++++- 1 file changed, 140 insertions(+), 1 deletion(-) diff --git a/pkgs/fleet/crates/fleet/src/gh.rs b/pkgs/fleet/crates/fleet/src/gh.rs index d210ef8175..2827939342 100644 --- a/pkgs/fleet/crates/fleet/src/gh.rs +++ b/pkgs/fleet/crates/fleet/src/gh.rs @@ -281,6 +281,11 @@ fn classify_failure(output: &str) -> FailureClass { || lower.contains("this endpoint requires you to be authenticated") || lower.contains("bad credentials") || lower.contains("http 401") + // GraphQL answers a node this identity cannot see as if it did + // not exist ("Could not resolve to a node with the global id", + // "Could not resolve to a ProjectV2 ...", "... to an + // Organization ..."), with no permission wording (#12154). + || lower.contains("could not resolve to a") { return FailureClass::Permission; } @@ -298,6 +303,7 @@ fn is_read_call(args: &[String]) -> bool { return false; }; match verb { + "api" if api_endpoint(&args[1..]) == Some("graphql") => is_graphql_read(&args[1..]), "api" => { // gh api: GET unless --method says otherwise or a field // flag (-f/-F/--field/--raw-field/--input) implies POST. @@ -343,6 +349,98 @@ fn is_read_call(args: &[String]) -> bool { } } +/// `gh api` flags that take the next argument as their value +/// (`gh api --help`), so that value is not mistaken for the endpoint. +const GH_API_VALUE_FLAGS: [&str; 17] = [ + "-f", + "-F", + "--field", + "--raw-field", + "--input", + "-X", + "--method", + "-H", + "--header", + "-q", + "--jq", + "-t", + "--template", + "-p", + "--preview", + "--hostname", + "--cache", +]; + +/// The endpoint argument of a `gh api` call (the first positional), +/// given the arguments after `api`. +fn api_endpoint(args: &[String]) -> Option<&str> { + let mut i = 0; + while i < args.len() { + let a = args[i].as_str(); + if GH_API_VALUE_FLAGS.contains(&a) { + i += 2; + continue; + } + if !a.starts_with('-') { + return Some(a); + } + i += 1; + } + None +} + +/// Is this `gh api graphql` call a query rather than a mutation? +/// GraphQL always POSTs, so the field flags say nothing; the +/// operation does. Read-only only when the `query` field is given +/// inline and its operation is an anonymous `{ ... }` selection or a +/// named `query`. A query read from a file (`query=@path`) or stdin +/// (`--input`), or any operation that cannot be told apart, counts +/// as a write (#12154). +fn is_graphql_read(args: &[String]) -> bool { + let mut query: Option<&str> = None; + let mut i = 0; + while i < args.len() { + let a = args[i].as_str(); + if a == "--input" || a.starts_with("--input=") { + return false; + } + let field = if a == "-f" || a == "-F" || a == "--field" || a == "--raw-field" { + i += 1; + args.get(i).map(|s| s.as_str()) + } else { + a.strip_prefix("--field=") + .or(a.strip_prefix("--raw-field=")) + .or(a.strip_prefix("-f").filter(|v| !v.is_empty())) + .or(a.strip_prefix("-F").filter(|v| !v.is_empty())) + }; + if let Some(v) = field.and_then(|f| f.strip_prefix("query=")) { + query = Some(v); + } + i += 1; + } + let Some(query) = query else { + return false; + }; + if query.starts_with('@') { + return false; + } + let mut rest = query.trim_start(); + // Skip leading `#` comments. + while let Some(after) = rest.strip_prefix('#') { + rest = after + .split_once('\n') + .map_or("", |(_, tail)| tail) + .trim_start(); + } + if rest.starts_with('{') { + return true; + } + match rest.strip_prefix("query") { + Some(tail) => !tail.starts_with(|c: char| c.is_alphanumeric() || c == '_'), + None => false, + } +} + /// The owner a call is aimed at, when the arguments name one — /// `--repo`/`-R owner/repo`, `--owner org`, an `api repos//…` /// path, or a github.com URL. The matching App installation leads @@ -1428,6 +1526,18 @@ mod tests { classify_failure("Resource not accessible by integration"), FailureClass::Permission ); + assert_eq!( + classify_failure( + "GraphQL: Could not resolve to a node with the global id of 'PVT_x'. (node)" + ), + FailureClass::Permission + ); + assert_eq!( + classify_failure( + "GraphQL: Could not resolve to an Organization with the login of 'x'." + ), + FailureClass::Permission + ); assert_eq!( classify_failure("gh: Not Found (HTTP 404)"), FailureClass::Other @@ -1473,7 +1583,36 @@ mod tests { ])); assert!(read(&["issue", "list", "--repo", "developing-today/code"])); assert!(read(&["api", "repos/developing-today/code"])); - assert!(read(&["api", "graphql", "-f", "query={viewer{login}}"]) == false); + // GraphQL: the operation decides, not the field flags (#12154). + assert!(read(&["api", "graphql", "-f", "query={viewer{login}}"])); + assert!(read(&[ + "api", + "graphql", + "-f", + "query=query($o:String!){organization(login:$o){projectV2(number:4){id}}}", + "-F", + "o=developing-today" + ])); + assert!(read(&[ + "api", + "-H", + "X: y", + "graphql", + "--raw-field=query= # board\n query Q { viewer { login } }" + ])); + assert!(!read(&[ + "api", + "graphql", + "-f", + "query=mutation{addStar(input:{starrableId:\"x\"}){clientMutationId}}" + ])); + assert!(!read(&["api", "graphql", "-f", "query= mutation M { x }"])); + assert!(!read(&["api", "graphql", "-f", "query=queryish { x }"])); + assert!(!read(&["api", "graphql", "-F", "query=@board.graphql"])); + assert!(!read(&["api", "graphql", "--input", "body.json"])); + assert!(!read(&["api", "graphql"])); + // A field named query on a REST call is still a POST-class write. + assert!(!read(&["api", "repos/o/r/issues", "-f", "query=x"])); assert!(read(&["api", "user", "--method", "GET"])); assert!(!read(&["api", "user", "--method", "PATCH"])); assert!(!read(&["issue", "comment", "1", "--body", "x"])); From 8e36bf98b5d824f61bd7b2159180046af0557916 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 21 Nov 2026 04:22:34 +0000 Subject: [PATCH 2/7] fix(fleet): treat any GraphQL document with a mutation as a write operationName can select a mutation from a document that opens with a query, so a mutation or subscription anywhere makes the call a write. Drop the 'Could not resolve to a' permission class: read-only calls already fail over on any error, and for a write that string does not prove the request was refused before it ran. Refs #12154 Co-Authored-By: Claude Opus 5.5 --- pkgs/fleet/crates/fleet/src/gh.rs | 43 +++++++++++++++++++------------ 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/pkgs/fleet/crates/fleet/src/gh.rs b/pkgs/fleet/crates/fleet/src/gh.rs index 2827939342..9a1d4928e6 100644 --- a/pkgs/fleet/crates/fleet/src/gh.rs +++ b/pkgs/fleet/crates/fleet/src/gh.rs @@ -281,11 +281,6 @@ fn classify_failure(output: &str) -> FailureClass { || lower.contains("this endpoint requires you to be authenticated") || lower.contains("bad credentials") || lower.contains("http 401") - // GraphQL answers a node this identity cannot see as if it did - // not exist ("Could not resolve to a node with the global id", - // "Could not resolve to a ProjectV2 ...", "... to an - // Organization ..."), with no permission wording (#12154). - || lower.contains("could not resolve to a") { return FailureClass::Permission; } @@ -424,6 +419,16 @@ fn is_graphql_read(args: &[String]) -> bool { if query.starts_with('@') { return false; } + // A document can hold several operations and `operationName` picks + // one, so a mutation or subscription anywhere in it makes the call + // a write. The word scan is coarse on purpose: a `mutation` inside + // a string or comment only errs toward treating a read as a write. + let has_write_operation = query + .split(|c: char| !(c.is_alphanumeric() || c == '_')) + .any(|word| word == "mutation" || word == "subscription"); + if has_write_operation { + return false; + } let mut rest = query.trim_start(); // Skip leading `#` comments. while let Some(after) = rest.strip_prefix('#') { @@ -1526,18 +1531,6 @@ mod tests { classify_failure("Resource not accessible by integration"), FailureClass::Permission ); - assert_eq!( - classify_failure( - "GraphQL: Could not resolve to a node with the global id of 'PVT_x'. (node)" - ), - FailureClass::Permission - ); - assert_eq!( - classify_failure( - "GraphQL: Could not resolve to an Organization with the login of 'x'." - ), - FailureClass::Permission - ); assert_eq!( classify_failure("gh: Not Found (HTTP 404)"), FailureClass::Other @@ -1607,6 +1600,22 @@ mod tests { "query=mutation{addStar(input:{starrableId:\"x\"}){clientMutationId}}" ])); assert!(!read(&["api", "graphql", "-f", "query= mutation M { x }"])); + // operationName can select a mutation from a document that opens + // with a query. + assert!(!read(&[ + "api", + "graphql", + "-f", + "query=query Q { viewer { login } } mutation M { x }", + "-f", + "operationName=M" + ])); + assert!(!read(&[ + "api", + "graphql", + "-f", + "query={ a } subscription S { b }" + ])); assert!(!read(&["api", "graphql", "-f", "query=queryish { x }"])); assert!(!read(&["api", "graphql", "-F", "query=@board.graphql"])); assert!(!read(&["api", "graphql", "--input", "body.json"])); From e9139b2c22a9d905e329ba88baef5e4e9829b689 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 21 Nov 2026 04:29:39 +0000 Subject: [PATCH 3/7] fix(fleet): count attached -f/-F field values as POST Co-Authored-By: Claude Opus 5.5 --- pkgs/fleet/crates/fleet/src/gh.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pkgs/fleet/crates/fleet/src/gh.rs b/pkgs/fleet/crates/fleet/src/gh.rs index 9a1d4928e6..bbe9e8cab9 100644 --- a/pkgs/fleet/crates/fleet/src/gh.rs +++ b/pkgs/fleet/crates/fleet/src/gh.rs @@ -305,8 +305,10 @@ fn is_read_call(args: &[String]) -> bool { let mut i = 1; while i < args.len() { let a = args[i].as_str(); - if a == "-f" - || a == "-F" + // `-fkey=value` / `-Fkey=value` are the same field + // flags with the value attached. + if a.starts_with("-f") + || a.starts_with("-F") || a == "--field" || a == "--raw-field" || a == "--input" @@ -1622,6 +1624,9 @@ mod tests { assert!(!read(&["api", "graphql"])); // A field named query on a REST call is still a POST-class write. assert!(!read(&["api", "repos/o/r/issues", "-f", "query=x"])); + // A field value attached to its flag still makes the call POST. + assert!(!read(&["api", "repos/o/r/issues", "-ftitle=x"])); + assert!(!read(&["api", "repos/o/r/labels", "-Fname=x"])); assert!(read(&["api", "user", "--method", "GET"])); assert!(!read(&["api", "user", "--method", "PATCH"])); assert!(!read(&["issue", "comment", "1", "--body", "x"])); From 24e95b79de14dc431fa30c8644a4934f93105bf0 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 21 Nov 2026 04:34:47 +0000 Subject: [PATCH 4/7] fix(fleet): skip other options' values when finding the GraphQL query A --template or --jq value shaped like -fquery=... no longer counts as the query field, and a call with more than one query field is a write. Co-Authored-By: Claude Opus 5.5 --- pkgs/fleet/crates/fleet/src/gh.rs | 40 ++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/pkgs/fleet/crates/fleet/src/gh.rs b/pkgs/fleet/crates/fleet/src/gh.rs index bbe9e8cab9..226753a3b0 100644 --- a/pkgs/fleet/crates/fleet/src/gh.rs +++ b/pkgs/fleet/crates/fleet/src/gh.rs @@ -395,13 +395,21 @@ fn api_endpoint(args: &[String]) -> Option<&str> { /// as a write (#12154). fn is_graphql_read(args: &[String]) -> bool { let mut query: Option<&str> = None; + let mut queries = 0; let mut i = 0; while i < args.len() { let a = args[i].as_str(); if a == "--input" || a.starts_with("--input=") { return false; } - let field = if a == "-f" || a == "-F" || a == "--field" || a == "--raw-field" { + let is_field_flag = a == "-f" || a == "-F" || a == "--field" || a == "--raw-field"; + if !is_field_flag && GH_API_VALUE_FLAGS.contains(&a) { + // Another option's value (a --template, --jq, header...) is + // never a field, even when it looks like `-fquery=...`. + i += 2; + continue; + } + let field = if is_field_flag { i += 1; args.get(i).map(|s| s.as_str()) } else { @@ -412,12 +420,17 @@ fn is_graphql_read(args: &[String]) -> bool { }; if let Some(v) = field.and_then(|f| f.strip_prefix("query=")) { query = Some(v); + queries += 1; } i += 1; } let Some(query) = query else { return false; }; + // Which of several query fields gh sends is not ours to guess. + if queries > 1 { + return false; + } if query.starts_with('@') { return false; } @@ -1619,6 +1632,31 @@ mod tests { "query={ a } subscription S { b }" ])); assert!(!read(&["api", "graphql", "-f", "query=queryish { x }"])); + // Another option's value that looks like a query field is not one. + assert!(!read(&[ + "api", + "graphql", + "-f", + "query=mutation { x }", + "--template", + "-fquery={viewer{login}}" + ])); + assert!(read(&[ + "api", + "graphql", + "--jq", + "-fquery=mutation{x}", + "-f", + "query={viewer{login}}" + ])); + assert!(!read(&[ + "api", + "graphql", + "-f", + "query={ a }", + "-f", + "query={ b }" + ])); assert!(!read(&["api", "graphql", "-F", "query=@board.graphql"])); assert!(!read(&["api", "graphql", "--input", "body.json"])); assert!(!read(&["api", "graphql"])); From 7fc7c21887e324238150c478d0d0c50cded6f45f Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 21 Nov 2026 04:46:01 +0000 Subject: [PATCH 5/7] fix(fleet): never classify clustered short options as a read A cluster like `gh api -it graphql repos/o/r/issues ...` makes the token after it a value, not the endpoint, so the scanner could take a REST write for a GraphQL read. Any short option longer than two characters that is not a value-taking flag with its value attached now counts as a write. Co-Authored-By: Claude Opus 5.5 --- pkgs/fleet/crates/fleet/src/gh.rs | 41 +++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/pkgs/fleet/crates/fleet/src/gh.rs b/pkgs/fleet/crates/fleet/src/gh.rs index 226753a3b0..34fd7c7e97 100644 --- a/pkgs/fleet/crates/fleet/src/gh.rs +++ b/pkgs/fleet/crates/fleet/src/gh.rs @@ -298,6 +298,10 @@ fn is_read_call(args: &[String]) -> bool { return false; }; match verb { + // A clustered short option such as `-it VALUE` hides which + // token is the endpoint and which is a value, so it is never + // trusted as a read (#12157 review). + "api" if args[1..].iter().any(|a| is_short_option_cluster(a)) => false, "api" if api_endpoint(&args[1..]) == Some("graphql") => is_graphql_read(&args[1..]), "api" => { // gh api: GET unless --method says otherwise or a field @@ -370,6 +374,19 @@ const GH_API_VALUE_FLAGS: [&str; 17] = [ /// The endpoint argument of a `gh api` call (the first positional), /// given the arguments after `api`. +/// Is `a` several short flags run together, like `-it`? gh api's +/// short options that take a value (`-f -F -H -X -p -q -t`) may carry +/// it attached (`-tTEMPLATE`), which is one option, not a cluster. +/// Any other short option longer than two characters starts with a +/// boolean flag (`-i`) followed by more flags. +fn is_short_option_cluster(a: &str) -> bool { + let mut chars = a.chars(); + chars.next() == Some('-') + && a.len() > 2 + && !a.starts_with("--") + && !matches!(chars.next(), Some('f' | 'F' | 'H' | 'X' | 'p' | 'q' | 't')) +} + fn api_endpoint(args: &[String]) -> Option<&str> { let mut i = 0; while i < args.len() { @@ -1518,6 +1535,30 @@ fn summarize_status(resp: &serde_json::Value) -> String { #[cfg(test)] mod tests { + #[test] + fn clustered_short_options_are_never_reads() { + let v = |a: &[&str]| a.iter().map(|s| s.to_string()).collect::>(); + // -i then -t (which takes "graphql"): the endpoint is the issues URL. + assert!(!is_read_call(&v(&[ + "api", + "-it", + "graphql", + "repos/o/r/issues", + "-f", + "title=x", + "-f", + "query={viewer{login}}" + ]))); + assert!(!is_read_call(&v(&["api", "-it", "x", "repos/o/r/issues"]))); + // An attached value is one option, not a cluster. + assert!(is_read_call(&v(&["api", "repos/o/r/issues", "-q.[]"]))); + assert!(is_read_call(&v(&["api", "repos/o/r/issues", "-tx", "-i"]))); + assert!(is_short_option_cluster("-it")); + assert!(!is_short_option_cluster("-i")); + assert!(!is_short_option_cluster("-tfoo")); + assert!(!is_short_option_cluster("--paginate")); + } + use super::*; // oracle: GitHub's refusal strings as observed in fleet logs From 603e776bb4864046121e6031154f43b8f2e95c74 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 21 Nov 2026 04:51:57 +0000 Subject: [PATCH 6/7] fix(fleet): treat typed graphql queries with placeholders as writes gh expands {owner}, {repo} and {branch} in -F/--field values before sending, so the unexpanded text is not the operation gh sends. Co-Authored-By: Claude Opus 5.5 --- pkgs/fleet/crates/fleet/src/gh.rs | 38 +++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/pkgs/fleet/crates/fleet/src/gh.rs b/pkgs/fleet/crates/fleet/src/gh.rs index 34fd7c7e97..07cba441ed 100644 --- a/pkgs/fleet/crates/fleet/src/gh.rs +++ b/pkgs/fleet/crates/fleet/src/gh.rs @@ -413,6 +413,7 @@ fn api_endpoint(args: &[String]) -> Option<&str> { fn is_graphql_read(args: &[String]) -> bool { let mut query: Option<&str> = None; let mut queries = 0; + let mut typed = false; let mut i = 0; while i < args.len() { let a = args[i].as_str(); @@ -426,6 +427,12 @@ fn is_graphql_read(args: &[String]) -> bool { i += 2; continue; } + // `-F`/`--field` values are typed: gh fills `{owner}`, `{repo}` + // and `{branch}` in them before sending. + let is_typed = a == "-F" + || a == "--field" + || a.starts_with("--field=") + || (a.starts_with("-F") && a.len() > 2); let field = if is_field_flag { i += 1; args.get(i).map(|s| s.as_str()) @@ -437,6 +444,7 @@ fn is_graphql_read(args: &[String]) -> bool { }; if let Some(v) = field.and_then(|f| f.strip_prefix("query=")) { query = Some(v); + typed = is_typed; queries += 1; } i += 1; @@ -451,6 +459,16 @@ fn is_graphql_read(args: &[String]) -> bool { if query.starts_with('@') { return false; } + // A typed field's placeholders are expanded by gh, so the text we + // see is not the operation gh sends; `{repo}` could become + // `mutation`. Treat any expandable placeholder as a write. + if typed + && ["{owner}", "{repo}", "{branch}"] + .iter() + .any(|p| query.contains(p)) + { + return false; + } // A document can hold several operations and `operationName` picks // one, so a mutation or subscription anywhere in it makes the call // a write. The word scan is coarse on purpose: a `mutation` inside @@ -1699,6 +1717,26 @@ mod tests { "query={ b }" ])); assert!(!read(&["api", "graphql", "-F", "query=@board.graphql"])); + // gh expands placeholders in typed fields, so what it sends is + // unknown; raw fields keep them literally. + assert!(!read(&[ + "api", + "graphql", + "-F", + "query={repo} {addComment(input:{}){x}}" + ])); + assert!(!read(&[ + "api", + "graphql", + "--field=query={owner} { viewer { login } }" + ])); + assert!(!read(&["api", "graphql", "-Fquery={branch} { x }"])); + assert!(read(&[ + "api", + "graphql", + "-f", + "query={ viewer { login } } # {repo}" + ])); assert!(!read(&["api", "graphql", "--input", "body.json"])); assert!(!read(&["api", "graphql"])); // A field named query on a REST call is still a POST-class write. From 2873f1025ba571d8f7c397004a33d424a9a4b80b Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 21 Nov 2026 04:56:25 +0000 Subject: [PATCH 7/7] test(fleet): property-test the gh graphql read classifier Generated documents, field forms and option placement check the invariant that a call able to mutate is never a read eligible for arbitrary-error retries, and that plain read documents still are. Co-Authored-By: Claude Opus 5.5 --- pkgs/fleet/Cargo.lock | 1 + pkgs/fleet/crates/fleet/Cargo.toml | 5 ++ pkgs/fleet/crates/fleet/src/gh.rs | 140 +++++++++++++++++++++++++++++ 3 files changed, 146 insertions(+) diff --git a/pkgs/fleet/Cargo.lock b/pkgs/fleet/Cargo.lock index 210edc41cd..e0e141fdc2 100644 --- a/pkgs/fleet/Cargo.lock +++ b/pkgs/fleet/Cargo.lock @@ -597,6 +597,7 @@ dependencies = [ "fleet-search", "fleet-secrets", "libc", + "proptest", "serde_json", "ureq", ] diff --git a/pkgs/fleet/crates/fleet/Cargo.toml b/pkgs/fleet/crates/fleet/Cargo.toml index 4ef26f1539..7ecc24bce6 100644 --- a/pkgs/fleet/crates/fleet/Cargo.toml +++ b/pkgs/fleet/crates/fleet/Cargo.toml @@ -100,3 +100,8 @@ secrets = ["dep:fleet-secrets"] # if ever needed, it is not a daemon role. gh = ["dep:fleet-secrets", "dep:ureq"] laptop-agent = ["dep:fleet-secrets", "dep:fleet-laptop-agent"] + +[dev-dependencies] +# Property tests for the gh read classifier (AGENTS.md testing +# doctrine), the same proptest version the other fleet crates use. +proptest = "1.12" diff --git a/pkgs/fleet/crates/fleet/src/gh.rs b/pkgs/fleet/crates/fleet/src/gh.rs index 07cba441ed..958f4a89a5 100644 --- a/pkgs/fleet/crates/fleet/src/gh.rs +++ b/pkgs/fleet/crates/fleet/src/gh.rs @@ -2006,3 +2006,143 @@ mod tests { ); } } + +/// Property tests for the read classifier behind arbitrary-error +/// retries. Oracle: the invariant that a call able to mutate (a +/// mutation or subscription anywhere in the document, a typed field +/// gh may rewrite, more than one query field, or a body from +/// `--input`) is never a read; and, in the other direction, that a +/// plain read document sent as a raw field is one. +#[cfg(test)] +mod graphql_read_props { + use super::is_read_call; + use proptest::prelude::*; + + fn comment() -> impl Strategy { + prop_oneof![ + Just(String::new()), + Just("# note\n".to_string()), + Just(" # a\n# b\n".to_string()) + ] + } + + fn name() -> impl Strategy { + "[A-Z][a-z]{0,5}" + } + + fn read_doc() -> impl Strategy { + (comment(), name()).prop_flat_map(|(c, n)| { + prop_oneof![ + Just(format!("{c}{{ viewer {{ login }} }}")), + Just(format!("{c}query {{ x }}")), + Just(format!("{c}query {n} {{ x }}")), + Just(format!("{c}query($n: Int) {{ x(n: $n) }}")), + ] + }) + } + + fn write_doc() -> impl Strategy { + (comment(), name(), name()).prop_flat_map(|(c, a, b)| { + prop_oneof![ + Just(format!("{c}mutation {{ addStar(input: {{}}) {{ x }} }}")), + Just(format!("{c}mutation {a} {{ x }}")), + Just(format!("{c}subscription {{ x }}")), + Just(format!("{c}subscription {a} {{ x }}")), + Just(format!("{c}query {a} {{ x }} mutation {b} {{ y }}")), + Just(format!("{c}{{ x }}\nmutation {b} {{ y }}")), + Just(format!("{c}query {a} {{ x }}\nsubscription {b} {{ y }}")), + ] + }) + } + + /// How the query field is written: (args, typed). + fn field(doc: String) -> impl Strategy, bool)> { + let q = format!("query={doc}"); + prop_oneof![ + Just((vec!["-f".to_string(), q.clone()], false)), + Just((vec!["--raw-field".to_string(), q.clone()], false)), + Just((vec![format!("-f{q}")], false)), + Just((vec![format!("--raw-field={q}")], false)), + Just((vec!["-F".to_string(), q.clone()], true)), + Just((vec!["--field".to_string(), q.clone()], true)), + Just((vec![format!("-F{q}")], true)), + Just((vec![format!("--field={q}")], true)), + ] + } + + /// Options that neither carry a query nor change the method. + fn noise() -> impl Strategy> { + let one = prop_oneof![ + Just(vec!["--paginate".to_string()]), + Just(vec![ + "-H".to_string(), + "Accept: application/json".to_string() + ]), + Just(vec!["--header=X-Y: z".to_string()]), + Just(vec!["--jq".to_string(), ".data".to_string()]), + Just(vec!["-f".to_string(), "owner=o".to_string()]), + Just(vec!["-F".to_string(), "n=1".to_string()]), + ]; + prop::collection::vec(one, 0..3).prop_map(|v| v.concat()) + } + + fn call(before: Vec, field: Vec, after: Vec) -> Vec { + let mut v = vec!["api".to_string(), "graphql".to_string()]; + v.extend(before); + v.extend(field); + v.extend(after); + v + } + + proptest! { + #[test] + fn generated_writes_are_never_reads( + (f, _) in write_doc().prop_flat_map(field), + before in noise(), + after in noise(), + ) { + let args = call(before, f, after); + prop_assert!(!is_read_call(&args), "write classified as read: {args:?}"); + } + + #[test] + fn raw_read_documents_are_reads( + // No placeholder in these documents, so typed fields are reads too. + (f, _) in read_doc().prop_flat_map(field), + before in noise(), + after in noise(), + ) { + let args = call(before, f, after); + prop_assert!(is_read_call(&args), "read classified as write: {args:?}"); + } + + #[test] + fn typed_placeholders_are_never_reads( + doc in read_doc(), + p in prop_oneof![Just("{owner}"), Just("{repo}"), Just("{branch}")], + before in noise(), + after in noise(), + ) { + let doc = format!("{doc} # {p}"); + for flag in ["-F", "--field"] { + let args = call(before.clone(), vec![flag.to_string(), format!("query={doc}")], after.clone()); + prop_assert!(!is_read_call(&args), "typed placeholder classified as read: {args:?}"); + } + } + + #[test] + fn a_second_query_field_or_input_is_never_a_read( + (f1, _) in read_doc().prop_flat_map(field), + (f2, _) in read_doc().prop_flat_map(field), + before in noise(), + input in any::(), + ) { + let args = if input { + call(before, f1, vec!["--input".to_string(), "body.json".to_string()]) + } else { + call(before, f1, f2) + }; + prop_assert!(!is_read_call(&args), "ambiguous call classified as read: {args:?}"); + } + } +}