use onetaskgraph_github_projects::{NODE_COUNT_LIMIT, graphql, worst_case_node_count};
fn over_limit(doing: &str, document: &str) -> Option<String> {
let count = worst_case_node_count(document)
.unwrap_or_else(|error| panic!("the document for {doing} could not be counted: {error}"));
(count >= NODE_COUNT_LIMIT).then(|| {
format!(
"the document for {doing} may return {count} nodes, at or above GitHub's limit \
of {NODE_COUNT_LIMIT}; next: give one of its connections a smaller page size, \
or stop selecting it"
)
})
}
#[test]
fn every_document_this_source_sends_stays_under_githubs_node_limit() {
let refused = graphql::DOCUMENTS
.iter()
.filter_map(|(document, doing)| over_limit(doing, document))
.collect::<Vec<_>>();
assert!(refused.is_empty(), "{}", refused.join("\n"));
}
#[test]
fn the_documents_that_reach_an_issue_under_a_page_are_the_ones_with_least_headroom() {
let count = |document: &str| worst_case_node_count(document).expect("a countable document");
assert_eq!(count(graphql::SEARCH_ISSUES), 20_400);
assert_eq!(count(graphql::SUB_ISSUES), 6_095);
assert_eq!(count(graphql::BOARD), 10_150);
assert_eq!(count(graphql::ISSUE_BOARD_ITEMS), 5_100);
assert_eq!(count(graphql::ISSUE), 456);
assert_eq!(count(graphql::ISSUE_DETAIL), 303);
assert_eq!(
count(graphql::ISSUE_DETAILS),
303 * onetaskgraph_github_projects::DETAIL_BATCH as u64
);
assert_eq!(count(graphql::ISSUE_DEPENDENCIES), 200);
assert_eq!(count(graphql::ISSUE_COMMENTS), 100);
assert_eq!(count(graphql::COMMENT_ISSUE), 0);
assert_eq!(count(graphql::REPOSITORY), 0);
assert_eq!(
count(graphql::SEARCH_BATCH),
u64::from(onetaskgraph_github_projects::MAX_PAGE_SIZE)
* (onetaskgraph_github_projects::SEARCH_BATCH
+ onetaskgraph_github_projects::SEARCH_BATCH_ORIGINS) as u64
);
}
fn connections_under_a_row(document: &str) -> Vec<String> {
use graphql_parser::query::{Definition, OperationDefinition, Selection};
fn walk(selections: &[Selection<'_, String>], path: &str, found: &mut Vec<String>) {
for selection in selections {
match selection {
Selection::Field(field) => {
let here = format!("{path}/{}", field.name);
if field
.arguments
.iter()
.any(|(name, _)| name == "first" || name == "last")
{
found.push(here.clone());
}
walk(&field.selection_set.items, &here, found);
}
Selection::InlineFragment(fragment) => {
walk(&fragment.selection_set.items, path, found);
}
Selection::FragmentSpread(spread) => {
found.push(format!("{path}/...{}", spread.fragment_name));
}
}
}
}
fn rows<'a>(
selections: &'a [Selection<'a, String>],
found: &mut Vec<&'a [Selection<'a, String>]>,
) {
for selection in selections {
match selection {
Selection::Field(field) if field.name == "nodes" => {
found.push(&field.selection_set.items);
}
Selection::Field(field) => rows(&field.selection_set.items, found),
Selection::InlineFragment(fragment) => rows(&fragment.selection_set.items, found),
Selection::FragmentSpread(_) => {}
}
}
}
let parsed = graphql_parser::parse_query::<String>(document).expect("a document");
let mut found = Vec::new();
for definition in &parsed.definitions {
let Definition::Operation(OperationDefinition::Query(query)) = definition else {
continue;
};
for selection in &query.selection_set.items {
let Selection::Field(slot) = selection else {
continue;
};
let Some(alias) = &slot.alias else {
continue;
};
let mut held = Vec::new();
rows(&slot.selection_set.items, &mut held);
assert!(!held.is_empty(), "the slot {alias} reads no rows");
for row in held {
walk(row, alias, &mut found);
}
}
}
found
}
#[test]
fn a_batched_search_slot_selects_no_connection_priced_per_row() {
assert_eq!(
connections_under_a_row(graphql::SEARCH_BATCH),
Vec::<String>::new()
);
}
#[test]
fn the_row_check_names_a_connection_a_search_slot_selects_again() {
let fragment = graphql::SEARCH_BATCH.replacen(
"nodes{__typename ... on Issue{id number",
"nodes{__typename ...BoardIssue ... on Issue{id number",
1,
) + " fragment BoardIssue on Issue{id}";
assert_eq!(connections_under_a_row(&fragment), ["s0/...BoardIssue"]);
let labels = graphql::SEARCH_BATCH.replacen(
"repository{nameWithOwner}}}}",
"repository{nameWithOwner} labels(first:50){nodes{id}}}}}",
1,
);
assert_eq!(connections_under_a_row(&labels), ["s0/labels"]);
let field_values = graphql::SEARCH_BATCH.replacen(
"content{__typename ... on Issue{id}}",
"content{__typename ... on Issue{id}} fieldValues(first:50){nodes{__typename}}",
1,
);
assert_eq!(connections_under_a_row(&field_values), ["o0/fieldValues"]);
}
const SEARCH_ISSUES_BEFORE_THE_FIX: &str = r#"query($search:String!,$type:SearchType!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
search(query:$search,type:$type,first:$first,after:$after){
pageInfo{hasNextPage endCursor}
nodes{__typename ...BoardIssue}
}
} fragment BoardIssue on Issue{__typename id title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total}
labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}
projectItems(first:$boardItems){nodes{id project{number}
fieldValues(first:$nestedFirst){nodes{
... on ProjectV2ItemFieldSingleSelectValue{name field{
... on ProjectV2SingleSelectField{id name options{id name}}
}}
... on ProjectV2ItemFieldTextValue{text field{... on ProjectV2Field{id name}}}
... on ProjectV2ItemFieldLabelValue{labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}}
}pageInfo{hasNextPage}}}pageInfo{hasNextPage}}}"#;
#[test]
fn the_check_reports_a_failure_naming_an_over_limit_document_and_its_count() {
let refusal = over_limit(
"searching this board's issues before the fix",
SEARCH_ISSUES_BEFORE_THE_FIX,
)
.expect("the pre-fix document is over the limit");
assert!(
refusal.contains("searching this board's issues before the fix"),
"{refusal}"
);
assert!(refusal.contains("770400"), "{refusal}");
assert!(refusal.contains("500000"), "{refusal}");
}
#[test]
fn the_count_sums_sibling_paths_rather_than_taking_the_deepest_one() {
assert_eq!(
worst_case_node_count(SEARCH_ISSUES_BEFORE_THE_FIX).expect("a countable document"),
770_400
);
}
#[test]
fn every_document_an_opted_in_source_sends_stays_under_githubs_node_limit() {
let over: Vec<String> = graphql::DOCUMENTS
.iter()
.filter_map(|(document, doing)| {
let spelled = graphql::with_assignees(document)?;
let mut sizes = onetaskgraph_github_projects::largest_page_sizes();
sizes.insert("assigneeFirst".to_owned(), 10);
let count = onetaskgraph_github_projects::node_count(&spelled, &sizes)
.unwrap_or_else(|error| panic!("{doing} with assignees: {error}"));
(count >= NODE_COUNT_LIMIT).then(|| format!("{doing}: {count} nodes"))
})
.collect();
assert!(over.is_empty(), "{}", over.join("\n"));
}