mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 17:01:37 +03:00
test(cli-rs): compare request queries and failed statuses in parity
P-TUI and P-APP compared writes by method, path and body only, so a write with another query, or one the server rejected, still matched. Writes now carry their sorted query, and in P-TUI any status other than 2xx. P-TUI also fails a requests deviation whose file does not exist.
This commit is contained in:
@@ -394,22 +394,30 @@ pub fn is_read(method: &str, path: &str) -> bool {
|
||||
.is_some_and(|plugin| !plugin.is_empty() && !plugin.contains('/'))
|
||||
}
|
||||
|
||||
/// `actual` holds every Rust request in order; `unmatched` the ones no route answered. Bodies are compared
|
||||
/// with client-minted IDs normalized (plan 7.5).
|
||||
/// `actual` holds every Rust request in order; `unmatched` the ones no route answered. Writes are compared by
|
||||
/// method, path, sorted query, and body, with client-minted IDs normalized (plan 7.5). Both sides get the status
|
||||
/// of the same recorded route, so it is not compared.
|
||||
pub fn compare_requests(recording: &Recording, actual: &[Sent], unmatched: &[Sent]) -> RequestComparison {
|
||||
let writes: Vec<(&str, &str, Option<Value>)> = actual
|
||||
// Bodies stay JSON values, so key order does not count.
|
||||
let write = |method: &str, path: &str, query: &Query, body: Option<&Value>| {
|
||||
let query: Vec<String> =
|
||||
query.iter().flat_map(|(key, values)| values.iter().map(move |value| format!("{key}={value}"))).collect();
|
||||
let query = if query.is_empty() { String::new() } else { format!("?{}", query.join("&")) };
|
||||
(format!("{method} {path}{query}"), body.map(minted))
|
||||
};
|
||||
let writes: Vec<(String, Option<Value>)> = actual
|
||||
.iter()
|
||||
.filter(|sent| !is_read(&sent.method, &sent.path))
|
||||
.map(|sent| (sent.method.as_str(), sent.path.as_str(), sent.body.as_ref().map(minted)))
|
||||
.map(|sent| write(&sent.method, &sent.path, &sent.query, sent.body.as_ref()))
|
||||
.collect();
|
||||
let expected: Vec<(&str, &str, Option<Value>)> = recording
|
||||
let expected: Vec<(String, Option<Value>)> = recording
|
||||
.requests
|
||||
.writes
|
||||
.iter()
|
||||
.map(|sent| (sent.method.as_str(), sent.path.as_str(), sent.body.as_ref().map(minted)))
|
||||
.map(|sent| write(&sent.method, &sent.path, &sent.query, sent.body.as_ref()))
|
||||
.collect();
|
||||
let line = |(method, path, body): &(&str, &str, Option<Value>)| {
|
||||
format!("{method} {path}{}", body.as_ref().map(|body| format!(" {body}")).unwrap_or_default())
|
||||
let line = |(head, body): &(String, Option<Value>)| {
|
||||
format!("{head}{}", body.as_ref().map(|body| format!(" {body}")).unwrap_or_default())
|
||||
};
|
||||
let writes_differ = (writes != expected).then(|| {
|
||||
let mut out = String::from("writes differ\n");
|
||||
@@ -534,6 +542,14 @@ mod tests {
|
||||
);
|
||||
assert_eq!(result.not_sent, ["GET /api/vcs"]);
|
||||
|
||||
let queried = [Sent { query: Query::from([("resume".into(), vec!["true".into()])]), ..same[0].clone() }];
|
||||
assert!(
|
||||
compare_requests(&recording, &queried, &[])
|
||||
.writes_differ
|
||||
.unwrap()
|
||||
.contains("+ rs | POST /api/a?resume=true")
|
||||
);
|
||||
|
||||
let other = [sent("POST", "/api/a", Some(serde_json::json!({ "n": 2 })))];
|
||||
let missing = [sent("GET", "/api/nope", None)];
|
||||
let result = compare_requests(&recording, &other, &missing);
|
||||
|
||||
@@ -163,17 +163,24 @@ export type RequestComparison = {
|
||||
onlyB: string[]
|
||||
}
|
||||
|
||||
// Writes must match in order after the normalizers; reads one side sent and the other did not are listed only.
|
||||
// Writes must match in order after the normalizers, with their sorted query and any status other than 2xx; reads
|
||||
// one side sent and the other did not are listed only.
|
||||
export function compareRequests(a: Logged[], b: Logged[], root: string): RequestComparison {
|
||||
// A live server stamps real times, so epoch milliseconds are normalized too (plan 7.5, timestamps).
|
||||
// Bodies are compared as JSON values, so key order does not count.
|
||||
const line = (entry: Logged) =>
|
||||
normalize(
|
||||
`${entry.method} ${entry.path}${entry.body === undefined ? "" : " " + JSON.stringify(sorted(entry.body))}`,
|
||||
{ root },
|
||||
)
|
||||
const line = (entry: Logged) => {
|
||||
const query = new URLSearchParams(
|
||||
Object.keys(entry.query)
|
||||
.toSorted()
|
||||
.flatMap((key) => entry.query[key].map((value) => [key, value])),
|
||||
).toString()
|
||||
const status =
|
||||
entry.status === undefined || (entry.status >= 200 && entry.status < 300) ? "" : ` -> ${entry.status}`
|
||||
const body = entry.body === undefined ? "" : " " + JSON.stringify(sorted(entry.body))
|
||||
return normalize(`${entry.method} ${entry.path}${query ? "?" + query : ""}${body}${status}`, { root })
|
||||
.replace(minted, "<$1>")
|
||||
.replace(/\b1\d{12}\b/g, "<epoch>")
|
||||
}
|
||||
const writes = (log: Logged[]) => log.filter((entry) => !isRead(entry.method, entry.path)).map(line)
|
||||
const reads = (log: Logged[]) => [
|
||||
...new Set(
|
||||
|
||||
@@ -116,11 +116,13 @@ async function runScenario(name: string, scenario: SimScenario) {
|
||||
}
|
||||
}
|
||||
const requests = compareRequests(results[0].requests, results[1].requests, box.root)
|
||||
const requestsPass = requests.equal || scenario.requests !== undefined
|
||||
const requestsMissing = scenario.requests !== undefined && !deviationExists(scenario.requests.deviation)
|
||||
const requestsPass = !requestsMissing && (requests.equal || scenario.requests !== undefined)
|
||||
ok &&= requestsPass
|
||||
lines.push(
|
||||
` requests: ${requests.equal ? "writes equal" : requestsPass ? "writes differ, allowed" : "writes differ"}`,
|
||||
)
|
||||
if (requestsMissing) lines.push(` deviation file missing: ${JSON.stringify(scenario.requests?.deviation)}`)
|
||||
if (!requests.equal) {
|
||||
lines.push(...requests.writes.a.map((write) => ` - ${labels[0]} | ${write}`))
|
||||
lines.push(...requests.writes.b.map((write) => ` + ${labels[1]} | ${write}`))
|
||||
|
||||
Reference in New Issue
Block a user