Skip to content

serve the citation e2e tests from a local mock of the citation services - #19290

Merged
gtritchie merged 9 commits into
mainfrom
test/citation-probe-request-url
Oct 28, 2026
Merged

gtritchie merged 9 commits into
mainfrom
test/citation-probe-request-url

Conversation

@kevinushey

@kevinushey kevinushey commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

The Insert Citation e2e tests looked up DOIs and ran searches against the live doi.org, Crossref, DataCite and PubMed services. They skipped when a host was unreachable, and this PR first tried to also skip when a lookup hung by re-probing the exact request afterwards. That can't separate a slow service from a stuck product: in this PR's own run 40447537582, a DataCite search sat in the rsession for 30s while the test's re-probe of the identical request answered in 9.4s (2.6s on the retry), so the Linux Desktop shard failed on DataCite's latency.

So this takes the live services out of the tests instead:

  • The rsession's citation lookups read their base URLs from R options -- rstudio.doi_url, rstudio.crossref_url, rstudio.datacite_url, rstudio.pubmed_url -- falling back to the real services (serviceBaseUrl() in SessionPanmirrorUtils, unit-tested in SessionPanmirrorUtilsTests.cpp). These sit alongside the existing rstudio.crossref_email and rstudio.pubmed_api_key options, and are read per request, so setting them from the console takes effect immediately.
  • utils/citation-services.ts is a loopback HTTP mock of the four services, answering from fixtures/citation-services.json (responses captured from the live services and trimmed). It keeps the contract the DOI lookup depends on -- CSL only for a request that negotiates for it with Accept: application/vnd.citationstyles.csl+json -- and can fail a service, or one of its resources, on demand.
  • citations.test.ts points the session at the mock before each test and drops the reachability gates and the re-probe/skip logic. New tests check that a failing service shows the dialog's error line for each source (and for PubMed's second, esummary leg), which the live services never let us exercise. Against a remote server, whose rsession can't reach this machine's loopback, the network-backed tests skip and the session's options are left alone.
  • PW_CITATION_LIVE=1 runs the same tests against the live services instead, to check that the fixtures still match what those services answer; the failure-injection tests skip.
  • The re-probe helpers (CITATION_SERVICE_REQUESTS, reprobeRequest(), CITATION_SERVICE_HOSTS), their harness spec, and the now-unused InsertCitationDialog.searchStatus are removed.

Also fixed along the way: asyncDownloadFile() spliced the URL and header values into the child R process's download.file() call unescaped. A quote in a DOI -- typed into the dialog, or carried by a Crossref/DataCite/PubMed result -- ended the string literal, so the lookup failed to parse. Both are now escaped with singleQuotedStrEscape(), and a new test looks up 10.1002/it's.

Verified locally with npm run test:desktop-dev -- tests/panes/editor/citations.test.ts: all 17 citation tests pass. With PW_CITATION_LIVE=1, everything but searches DataCite passes; that one fails on DataCite's current ranking drift (the same failure main has right now), not on a change in the responses. The rsession gtests pass.

No issue: test infrastructure (the options are an undocumented test hook), plus the escaping fix above, which came up in review.

A citation test that saw neither results nor an error re-probed only the service's host root, which can answer while the lookup's own endpoint or redirect target hangs. In run 40377299047 doi.org answered the probe in milliseconds while its CSL content negotiation (a 302 to data.crosscite.org) and api.datacite.org's search both sat on the rsession's download for R's full 60s timeout, so a DataCite outage failed the shard instead of skipping.

The tests now re-probe the exact request the rsession made (mirrored from the panmirror modules), following redirects and reading the body, and treat a 5xx/429 like a hang. The verdict replaces the host's cached entry so later guards skip too. The search tests share the wait with the DOI inserts, so a search that hangs skips the same way. A harness spec covers the classification against a local server.
@kevinushey kevinushey added this to the 2026.11 (Montauk Daisy) milestone Oct 26, 2026
The PubMed search is two requests: esearch for the ids, then esummary for their records, and the dialog shows nothing until both answer. A ServiceRequest can now name the request the rsession makes next from its response body, and the probe follows the chain under one timeout.
@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

RStudio E2E - all platforms

All tests passed!

#################### 100% pass rate

Passed Failed Skipped Flaky
4558 0 239 2
Platform ✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
✅ auth-teardown 4 0 16 0
✅ desktop-linux 1143 0 43 0
✅ desktop-macos 1140 0 47 0
✅ desktop-windows 1140 0 50 2
✅ server-linux 1083 0 51 0
✅ setup 48 0 32 0
View / download the full report

View report online -- opens in the browser, no download needed. Available for 14 days.

Download playwright-report.zip -- extract and open index.html

Includes screenshots, traces, and step-by-step details for every test. Expires in 15 days.


All platforms - commit 2c1b642441de56f137483969b25f7c5e39294d69 - Run #1785

The rsession's DOI, Crossref, DataCite and PubMed lookups now read their base
URLs from the rstudio.{doi,crossref,datacite,pubmed}_url options, so the
citations spec can point them at a loopback mock answering from captured
fixtures. This replaces re-probing the live services, which can't tell a slow
service from a stuck product.
@kevinushey kevinushey changed the title re-probe the request itself when a citation lookup hangs serve the citation e2e tests from a local mock of the citation services Oct 27, 2026
@kevinushey
kevinushey requested a review from gtritchie October 27, 2026 19:10

@gtritchie gtritchie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving these tests off the live services makes sense, and the run 40447537582 analysis makes the case well. Notes inline, roughly in priority order:

  1. URLs reach download.file() unescaped (pre-existing for DOIs; this PR routes the new option values through the same string).
  2. The mock is gated on sessionsRunLocally(), which doesn't quite answer "can the rsession reach this machine's loopback".
  3. Nothing checks the fixtures against the live services anymore.
  4. Smaller: options set on remote sessions that can't reach the mock, afterAll when the mock never started, trailing slash in the base URL, failure-path coverage, option names spread across files, and no test for serviceBaseUrl().

}
}

std::string serviceBaseUrl(const std::string& option, const std::string& defaultUrl)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The URL built from this ends up in download.file('<url>' ...) (src/cpp/session/SessionAsyncDownloadFile.cpp:144) without escaping, so a ' in the option value breaks the R command.

The DOI has the same problem, and that predates this PR. The DOI panel passes the trimmed search text straight to fetchCSL, and panmirror's Wiley pattern (10.1002/[^\s]+) accepts a quote anyway. I checked: the command built for 10.1002/it's fails parse() with "unexpected symbol".

Escaping the URL with string_utils::singleQuotedStrEscape in asyncDownloadFile, the way the user agent already is, would cover both.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 28c7507: asyncDownloadFile() now escapes the URL, and the header values too, with singleQuotedStrEscape(). It isn't only typed input: the Crossref, DataCite and PubMed panels also call fetchCSL with the DOI from a search result. Added an e2e test that looks up 10.1002/it's against the mock and expects "No data for this DOI could be found." rather than the parse error.


std::string serviceBaseUrl(const std::string& option, const std::string& defaultUrl)
{
std::string url = r::options::getOption<std::string>(option, defaultUrl, false);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every caller formats %s/%s, so a value with a trailing slash produces a double slash. For example, options(rstudio.doi_url = "http://127.0.0.1:N/doi/") requests /doi//10.3133/93888, which the mock answers with a 404 (I ran it against the mock). optionsCall() never adds a trailing slash, so this only affects someone setting the option from the console, but stripping a trailing / here is cheap.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in f69352b: serviceBaseUrl() strips trailing slashes, and a value that's only slashes falls back to the default.

std::string serviceBaseUrl(const std::string& option, const std::string& defaultUrl)
{
std::string url = r::options::getOption<std::string>(option, defaultUrl, false);
return url.empty() ? defaultUrl : url;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: nothing tests the unset/empty-string fallback. The e2e suite always sets valid URLs, so a regression here would send production lookups to an empty host without any test noticing. (Non-string values are already handled: getOption logs and returns the default.)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added SessionPanmirrorUtilsTests.cpp in f69352b. It checks that unset, '' and '/' fall back to the default, that a set value is used, and that trailing slashes are dropped.

const char * const kCrossrefWorks = "works";

// kCrossrefApiHost, unless the rstudio.crossref_url option overrides it.
std::string crossrefApiHost();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only Crossref gets an accessor. DOI, DataCite and PubMed call serviceBaseUrl() inline, so the four option names are string literals in four .cpp files. A typo in one silently falls back to the live host, and the e2e tests would quietly start hitting the real service again. Declaring the option names next to the kXxxHost constants, or giving all four an accessor like this one, would keep them together.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f69352b declares the four option names together in SessionPanmirrorUtils.hpp, next to serviceBaseUrl(). The failure-path test now runs once per service (92a1d92), so a name mismatch on either side fails it: a lookup that bypassed the mock would get the live service's results, not the injected 503.

services = await CitationServiceMock.start();
// The mock listens on this machine's loopback, which only an rsession on
// this machine can reach.
servicesReachable = sessionsRunLocally(mode, externalServerUrl());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sessionsRunLocally() was written to answer "can the stack dump see the rsession processes", which isn't quite "can the rsession reach this machine's loopback". Consider a localhost URL that forwards into a container (docker run -p 8787:8787 with PW_RSTUDIO_SERVER_URL=http://localhost:8787): it passes this check, but the rsession's 127.0.0.1 is the container's, so the network-backed tests fail instead of skipping.

None of the documented setups do this, so it's hypothetical. A probe from inside the session would answer the actual question, though: a download.file() of the mock URL from R, along the lines of the in-session probes in utils/connections.ts.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd keep the static check. The two approaches only disagree when the session is local but can't reach this machine's loopback: your port-forwarded container, or a session whose environment sends loopback requests through a proxy. In the proxy case on CI, a probe-gated skip would quietly skip every network-backed test, and the coverage this PR restores would disappear without a red run. In the container case the failure is loud and easy to trace, since every network test reports the same unreachable mock.

try {
await consoleActions.executeInConsole(RESET_CITATION_SERVICE_OPTIONS, { wait: true });
} finally {
await services.close();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If beforeAll fails before the mock starts, services is undefined here, so this throws a TypeError on top of the real failure. Playwright still reports the original error, so it's noise rather than masking, but services?.close() avoids it.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 92a1d92 (services?.close()). The options reset is also skipped when the mock was never reachable.

await consoleActions.executeInConsole('unlink("references.bib")', { wait: true });
// any leftover so each test's bib assertions start from a clean file. The
// same command (re)points the session's citation lookups at the mock.
await consoleActions.executeInConsole(`unlink("references.bib"); ${services.optionsCall()}`, { wait: true });

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This runs even when servicesReachable is false. So on a remote server, the user's session gets rstudio.*_url pointed at an unreachable loopback port before every test, including the ones that don't touch the network. If afterAll doesn't get to run (worker killed, console wedged), Insert Citation stays broken in that session until R restarts. Setting the options only when servicesReachable is true would avoid that.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 92a1d92: beforeEach sets the options only when the mock is reachable, and afterAll only resets them then.

await citation.open();
const searchBox = await citation.selectSource(CITATION_SOURCES.datacite);
await citation.search(searchBox, searchTerm);
await expect(citation.searchError).toHaveText('An error occurred while searching DataCite.', { timeout: 30000 });

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to have the failure path covered at last. It only covers DataCite, though. DOI and PubMed failures go through separate code in the rsession (doiFetchCSL, and the nested pubMedRequest calls in pubMedSearch), and a regression that leaves either stuck on "Searching..." would pass. Parameterizing this over the services would cover them.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

92a1d92 runs the failure test for every source: DOI, Crossref, DataCite, PubMed, and PubMed with only the nested esummary leg failing (fail() now takes an optional resource). Crossref's panel has no service-specific error text, so that case expects the catch-all "An unknown error occurred. Please try again."

@@ -0,0 +1,945 @@
{

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This trades the flakiness for a blind spot: nothing checks anymore that the live services still return what these fixtures say. If DataCite changes its JSON:API shape, or doi.org stops honoring the CSL Accept header, users' lookups break while this suite stays green. There's also no script to re-capture the fixtures.

Could we keep an opt-in live run (env-gated, leaving the options unset so the session uses the real services), or check in the capture script so the fixtures can be refreshed?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an opt-in live run in 2c1b642: PW_CITATION_LIVE=1 leaves the options unset, so the session uses the real services, and skips the failure-injection tests (documented in the e2e README). Run today, everything passes except searches DataCite. That one fails on the ranking drift that currently has main red (a new record pushed the "bobolink" matches past the first five rows), not on a change in the services' responses. It's manual, so it only catches drift when someone runs it.

A quote in a DOI (typed, or carried by a Crossref/DataCite/PubMed result)
ended the R string literal, so the lookup failed to parse.
Also unit-tests serviceBaseUrl()'s fallback to the live service.
@kevinushey
kevinushey requested a review from gtritchie October 28, 2026 04:49

@gtritchie gtritchie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@gtritchie
gtritchie merged commit cd14fac into main Oct 28, 2026
64 checks passed
@gtritchie
gtritchie deleted the test/citation-probe-request-url branch October 28, 2026 12:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants