Skip to content

Commit e3bb613

Browse files
authored
Merge pull request #71 from ewired/refactorurls
Streamline TestOption processing
2 parents 51af248 + 3fdea68 commit e3bb613

3 files changed

Lines changed: 30 additions & 39 deletions

File tree

packages/magnitude-test/src/discovery/testDeclaration.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TestDeclaration, TestOptions, TestFunction, TestGroupFunction } from './types';
2-
import { TestRegistry, processUrl } from './testRegistry';
3-
import { addProtocolIfMissing } from '@/util';
2+
import { TestRegistry } from './testRegistry';
3+
import { addProtocolIfMissing, processUrl } from '@/util';
44

55
function testDecl(
66
title: string,
@@ -32,7 +32,7 @@ function testDecl(
3232
};
3333

3434
if (!combinedOptions.url) {
35-
throw Error("URL must be provided either through (1) env var MAGNITUDE_TEST_URL, (2) via test.config, or (3) in group or test options");
35+
throw Error("URL must be provided either through (1) env var MAGNITUDE_TEST_URL, (2) via magnitude.config.ts, or (3) in group or test options");
3636
}
3737

3838
// Add the declared test function as a runnable to the registry

packages/magnitude-test/src/discovery/testRegistry.ts

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { TestOptions, TestGroup, MagnitudeConfig, TestFunction, RegisteredTest } from "./types";
22
import { TestCompiler } from "@/compiler";
3+
import { processUrl } from "@/util";
34
import cuid2 from "@paralleldrive/cuid2";
45
import { pathToFileURL } from "node:url";
56

@@ -140,28 +141,12 @@ export class TestRegistry {
140141
url: process.env.MAGNITUDE_TEST_URL
141142
} : {};
142143

143-
//console.log("global options:", this.globalOptions)
144-
145-
//const configuredOptions = this.globalOptions;
146-
const globalOptions = this.globalOptions.url ? {
147-
url: processUrl(envOptions.url, this.globalOptions.url)
148-
} : {};
149-
150-
const groupOptions = this.currentGroup?.options ? {
151-
...this.currentGroup.options,
152-
url: processUrl(globalOptions.url, this.currentGroup.options.url)
153-
} : {};
154-
155-
156-
const combinedOptions = {
157-
...globalOptions,
144+
return {
145+
...this.globalOptions,
158146
...envOptions, // env options take precedence over global options
159-
...groupOptions
160-
}
161-
162-
//console.log("combinedOptions:", combinedOptions)
163-
164-
return combinedOptions;
147+
...(this.currentGroup?.options ?? {}),
148+
url: processUrl(envOptions.url, this.globalOptions.url, this.currentGroup?.options?.url)
149+
};
165150
}
166151

167152
async loadTestFile(absoluteFilePath: string, relativeFilePath: string): Promise<void> {
@@ -191,19 +176,4 @@ export class TestRegistry {
191176
}
192177
}
193178

194-
export function processUrl(base: string | undefined, relative: string | undefined): string | undefined {
195-
if (!relative) return base;
196-
if (!base) return relative;
197-
try {
198-
return new URL(relative).toString(); // It's a full URL by itself
199-
} catch {
200-
try {
201-
// Not a full URL on its own, try to combine with base
202-
return new URL(relative, base).toString();
203-
} catch (e) {
204-
return relative;
205-
}
206-
}
207-
}
208-
209179
export default TestRegistry;

packages/magnitude-test/src/util.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,24 @@ export const knownCostMap: Record<string, number[]> = {
208208
'gpt-4.1-nano': [0.10, 0.40],
209209
'gpt-4o': [3.75, 15.00],
210210
}
211+
212+
export function processUrl(...urls: (string | undefined)[]): string | undefined {
213+
if (urls.length === 0) return;
214+
if (urls.length === 1) return urls[0];
215+
216+
const [base, relative, ...rest] = urls;
217+
if (!relative) return processUrl(base, ...rest);
218+
if (!base) return processUrl(relative, ...rest);
219+
220+
try {
221+
return processUrl(new URL(relative).toString(), ...rest); // It's a full URL by itself
222+
} catch {
223+
try {
224+
// Not a full URL on its own, try to combine with base
225+
return processUrl(new URL(relative, base).toString(), ...rest);
226+
} catch (_e) {
227+
return processUrl(relative, ...rest);
228+
}
229+
}
230+
}
231+

0 commit comments

Comments
 (0)