Skip to content
This repository was archived by the owner on Dec 20, 2025. It is now read-only.

Commit 05ffb59

Browse files
authored
test(google-sr): improve test organization and rate limiting (#77)
* test(google-sr): remove old tests * test(google-sr): add tests dependencies * fix(google-sr): improve URL construction in safeGetFetch to handle query parameters correctly * refactor(google-sr): simplify decodeResponse function by removing fallback charset detection * test(google-sr): improve test organization and rate limiting - Standardize rate limiting cooldown across test files - Only apply cooldown when live tests are enabled - Simplify test assertions and structure - Remove redundant test helpers and constants
1 parent 51828ad commit 05ffb59

8 files changed

Lines changed: 479 additions & 310 deletions

File tree

packages/google-sr/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
},
3333
"devDependencies": {
3434
"@vitest/coverage-v8": "^3.1.3",
35+
"@vitest/ui": "3.1.3",
36+
"is-ci": "4.1.0",
3537
"vitest": "^3.1.3"
3638
}
3739
}

packages/google-sr/src/utils.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ export async function safeGetFetch(options: RequestOptions): Promise<Response> {
2929
if (!options.url) {
3030
throw new TypeError("Request options must contain a valid URL.");
3131
}
32+
const queryParams = options.queryParams?.toString();
3233
// get the full url with query parameters
33-
const url = `${options.url}?${options.queryParams?.toString()}`;
34+
const url = `${options.url}${queryParams ? `?${queryParams}` : ""}`;
35+
options.queryParams = undefined; // remove queryParams from options to avoid sending it again
3436
const response = await fetch(url, options);
3537
// we error on non-200 status codes
3638
if (!response.ok) {
@@ -45,27 +47,16 @@ export async function safeGetFetch(options: RequestOptions): Promise<Response> {
4547
/**
4648
* @private
4749
* Try to decode the response body using the `ISO-8859-1` encoding,
48-
* falling back to charset detection if it fails.
4950
* @param response The response object from a fetch call
5051
* @returns The decoded response body as a string
5152
*/
5253
export async function decodeResponse(response: Response): Promise<string> {
5354
const dataBuffer = await response.arrayBuffer();
5455

55-
try {
56-
// During testing using the current user agent, the response was always in ISO-8859-1 encoding
57-
// It is safe to assume that the response will always be in ISO-8859-1 encoding
58-
// However if this were to change, then the text decoder will error, and will fallback to charset detection
59-
return new TextDecoder("iso-8859-1", { fatal: true }).decode(dataBuffer);
60-
} catch {
61-
// fallback to charset detection via content-type header
62-
// this should most likely never happen, but it's a good fallback
63-
const contentType = response.headers.get("content-type") || "";
64-
const charsetMatch = contentType.match(/charset=([^;]+)/i);
65-
const charset = charsetMatch?.[1] ? charsetMatch[1].toLowerCase() : "utf-8";
66-
// it will try to use whatever charset is detected, or default to utf-8
67-
return new TextDecoder(charset, { fatal: true }).decode(dataBuffer);
68-
}
56+
// During testing using the current user agent, the response was always in ISO-8859-1 encoding
57+
// It is safe to assume that the response will always be in ISO-8859-1 encoding
58+
// However if this were to change, then the text decoder will error
59+
return new TextDecoder("iso-8859-1", { fatal: true }).decode(dataBuffer);
6960
}
7061

7162
/**

packages/google-sr/tests/paged.test.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

packages/google-sr/tests/result.test.ts

Lines changed: 0 additions & 207 deletions
This file was deleted.

0 commit comments

Comments
 (0)