Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changeset/28946-qa-base-url-trailing-slash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@objectstack/core': patch
---

fix(core): `os test --url http://host:PORT/` addresses the same endpoints as the URL without the slash, and a failed `api_call` names the URL it addressed (#28946)

Clause-②: no
Metadata-protocol: none

`HttpTestAdapter` now strips trailing slashes from its base URL once, in its constructor, so every URL it builds (the discovery probe, the record collection URLs, an `api_call` relative target and the provenance clauses that name them) reads the same base. Before, each builder appended a path starting with `/` to the base as given, so a `--url` copied with its trailing slash addressed `//api/v1/...` and a stock server answered every HTTP scenario `404 ENDPOINT_NOT_FOUND`. A base with a path prefix (`http://host/prefix/`) and repeated slashes are normalised the same way. Nothing else about `--url` is validated, so a malformed value behaves as it did.

An `api_call` step that fails 404 or 405 now appends ` — api_call addressed METHOD URL` to its `HTTP Error STATUS: BODY` sentence, as record actions already append the mount they addressed. Other statuses stay undecorated, so the server's own message stands alone.
74 changes: 72 additions & 2 deletions packages/core/src/qa/http-adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,22 @@ describe('[#7848] the three non-record action types are unchanged', () => {
expect(calls[0].url).toBe(`${BASE_URL}/api/2026-01/data/crm_account`);
});

it('[#7983] an api_call failure is not decorated with a mount it never used', async () => {
it('[#7983] an api_call failure is not decorated with a mount it never used — it names the URL it addressed', async () => {
fetchMock.mockImplementation(async () => jsonResponse({ error: 'Not found' }, 404));
const adapter = new HttpTestAdapter(BASE_URL);
const err = await adapter
.execute(action('api_call', '/nope', { method: 'GET' }), {})
.then(() => undefined, (e: Error) => e);
expect(err?.message).toBe('HTTP Error 404: {"error":"Not found"}');
expect(err?.message).toBe(`HTTP Error 404: {"error":"Not found"} — api_call addressed GET ${BASE_URL}/nope`);
expect(err?.message).not.toContain('record actions addressed');
});

it('an api_call 422 stays undecorated — it reached a route and the server\'s message stands alone', async () => {
fetchMock.mockImplementation(async () => jsonResponse({ error: 'name is required' }, 422));
const err = await new HttpTestAdapter(BASE_URL)
.execute(action('api_call', '/api/v1/data/crm_account', { method: 'POST', body: {} }), {})
.then(() => undefined, (e: Error) => e);
expect(err?.message).toBe('HTTP Error 422: {"error":"name is required"}');
});

it('wait resolves without touching the network', async () => {
Expand Down Expand Up @@ -452,3 +461,64 @@ describe('[#20289] readTargetServices hands the runner the SAME probe\'s `servic
expect(warnings).toEqual([]);
});
});

/**
* PIN — the base URL is normalised once, so a trailing slash on `--url` does
* not double the slash in front of every path the adapter builds.
*
* Each URL builder appends a path that starts with `/`; `os test --url
* http://host:PORT/` kept the slash and addressed `//api/v1/...`, which a stock
* server answers `404 ENDPOINT_NOT_FOUND` — every HTTP scenario failed while the
* same URL without the slash passed. Pinned per builder (discovery probe, record
* collection URL, `api_call` relative target, the provenance clause) against a
* captured `fetch`, for one slash, several, and a base that carries a path.
*/
describe('a trailing slash on the base URL addresses the same endpoints', () => {
const rows: Array<[label: string, given: string, normalised: string]> = [
['one trailing slash', `${BASE_URL}/`, BASE_URL],
['two trailing slashes', `${BASE_URL}//`, BASE_URL],
['a path prefix with a trailing slash', 'http://localhost:3000/prefix/', 'http://localhost:3000/prefix'],
];

for (const [label, given, normalised] of rows) {
describe(label, () => {
const discoveryUrl = `${normalised}${EXPECTED_API_BASE}/discovery`;

beforeEach(() => {
fetchMock.mockImplementation(async (input: unknown, init?: RequestInit) => {
calls.push({ url: String(input), method: init?.method ?? 'GET', body: undefined, headers: {} });
if (String(input) === discoveryUrl) return discoveryDocument({ data: ADVERTISED_DATA_PATH });
return jsonResponse({ ok: true });
});
});

it('the discovery probe and the record collection URL carry one slash', async () => {
await new HttpTestAdapter(given).execute(action('create_record', 'crm_account', { name: 'Acme' }), {});
expect(calls.map((c) => c.url)).toEqual([discoveryUrl, `${normalised}${ADVERTISED_DATA_PATH}/crm_account`]);
expect(warnings).toEqual([]);
});

it('an api_call relative target carries one slash', async () => {
await new HttpTestAdapter(given).execute(action('api_call', '/api/v1/health', { method: 'GET' }), {});
expect(calls.map((c) => c.url)).toEqual([`${normalised}/api/v1/health`]);
});

it('the record-action provenance clause names the normalised base', async () => {
fetchMock.mockImplementation(async () => jsonResponse({ error: 'Not found' }, 404));
const err = await new HttpTestAdapter(given)
.execute(action('read_record', 'crm_account', { id: 'rec_1' }), {})
.then(() => undefined, (e: Error) => e);
expect(err?.message).toContain(`record actions addressed ${normalised}${EXPECTED_DATA_PATH} (`);
expect(err?.message).toContain(`GET ${discoveryUrl} answered 404`);
});

it('an api_call 404 names the normalised URL it addressed', async () => {
fetchMock.mockImplementation(async () => jsonResponse({ error: 'Not found' }, 404));
const err = await new HttpTestAdapter(given)
.execute(action('api_call', '/api/v1/health', { method: 'GET' }), {})
.then(() => undefined, (e: Error) => e);
expect(err?.message).toBe(`HTTP Error 404: {"error":"Not found"} — api_call addressed GET ${normalised}/api/v1/health`);
});
});
}
});
25 changes: 20 additions & 5 deletions packages/core/src/qa/http-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,20 @@ export class HttpTestAdapter implements TestExecutionAdapter {
/** The data mount derived from that probe, memoised so its fallback warns once. */
private mountPromise?: Promise<ResolvedDataMount>;

constructor(private baseUrl: string, private authToken?: string) {}
/**
* The target origin (plus any path prefix) with its trailing slashes
* stripped, normalised ONCE here so every URL this adapter builds — the
* discovery probe, the record collection URLs, `api_call`'s relative target
* and the provenance clauses that name them — reads the same base. Each
* builder appends a path that starts with `/`, so `--url http://host:PORT/`
* kept verbatim addressed `//api/v1/...`, which the server answers 404.
* Nothing else is validated: a malformed `--url` behaves as it did.
*/
private readonly baseUrl: string;

constructor(baseUrl: string, private authToken?: string) {
this.baseUrl = baseUrl.replace(/\/+$/, '');
}

/** The run's discovery read; probes at most once per adapter. */
private discovery(): Promise<DiscoveryRead> {
Expand Down Expand Up @@ -370,15 +383,17 @@ export class HttpTestAdapter implements TestExecutionAdapter {
headers,
body
});
return this.handleResponse(response);
// The URL it addressed, never a mount: `api_call` consulted none.
return this.handleResponse(response, `api_call addressed ${method} ${url}`);
}

private async handleResponse(response: Response, mountNote?: string) {
private async handleResponse(response: Response, addressNote?: string) {
if (!response.ok) {
const text = await response.text();
// The `HTTP Error <status>: <body>` prefix is unchanged — a suite author
// (and every transcript predating #7983) reads the same first clause;
// the mount provenance is APPENDED, never substituted.
// the mount provenance (or, for `api_call`, the URL it addressed) is
// APPENDED, never substituted.
//
// Appended on 404/405 only: those are the statuses a wrong mount
// produces (nothing registered at the path / registered under another
Expand All @@ -388,7 +403,7 @@ export class HttpTestAdapter implements TestExecutionAdapter {
// under a URL the author has no reason to doubt.
const wrongUrlShaped = response.status === 404 || response.status === 405;
throw new Error(
`HTTP Error ${response.status}: ${text}${wrongUrlShaped && mountNote ? ` — ${mountNote}` : ''}`,
`HTTP Error ${response.status}: ${text}${wrongUrlShaped && addressNote ? ` — ${addressNote}` : ''}`,
);
}
const contentType = response.headers.get('content-type');
Expand Down
Loading