Skip to content

Commit 221eddd

Browse files
committed
improve and migrate tests for parseURLParams
- Add regression tests for major/major.minor version patterns - Add tests for invalid input fallback behavior - Migrate test file to TypeScript - Export p5VersionStrings from parseUrlParams for test validation - Use regex assertions to avoid breaking when p5Versions updates
1 parent 38986b9 commit 221eddd

3 files changed

Lines changed: 123 additions & 56 deletions

File tree

client/utils/parseURLParams.test.js

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import { parseUrlParams, p5VersionStrings } from './parseURLParams';
2+
import { currentP5Version } from '../../common/p5Versions';
3+
4+
describe('parseUrlParams', () => {
5+
describe('default behavior', () => {
6+
test('returns defaults when no params are provided', () => {
7+
const url = 'https://example.com';
8+
const result = parseUrlParams(url);
9+
10+
expect(result).toEqual({
11+
version: currentP5Version,
12+
sound: true,
13+
preload: false,
14+
shapes: false,
15+
data: false
16+
});
17+
});
18+
19+
test('falls back to defaults for unsupported inputs', () => {
20+
const url =
21+
'https://example.com?version=A&sound=A&preload=A&shapes=A&data=A';
22+
const result = parseUrlParams(url);
23+
24+
expect(result).toEqual({
25+
version: currentP5Version,
26+
sound: true,
27+
preload: false,
28+
shapes: false,
29+
data: false
30+
});
31+
});
32+
});
33+
34+
describe('version parsing', () => {
35+
// Uses regex since p5Versions may be updated over time.
36+
// Checks to ensure version is valid too.
37+
38+
test('parses a valid p5 version and falls back for invalid versions', () => {
39+
const good = parseUrlParams('https://example.com?version=1.4.0');
40+
expect(good.version).toBe('1.4.0');
41+
42+
const bad = parseUrlParams('https://example.com?version=9.9.9');
43+
expect(bad.version).toBe(currentP5Version);
44+
});
45+
46+
test('parses major.minor version 0.5.x to newest patch', () => {
47+
const url = 'https://example.com?version=0.5';
48+
const result = parseUrlParams(url);
49+
50+
expect(result.version).toMatch(/^0\.5\.\d+$/);
51+
expect(p5VersionStrings).toContain(result.version);
52+
});
53+
54+
test('parses major version 0 to newest 0.x version', () => {
55+
const url = 'https://example.com?version=0';
56+
const result = parseUrlParams(url);
57+
58+
expect(result.version).toMatch(/^0\.\d+\.\d+$/);
59+
expect(p5VersionStrings).toContain(result.version);
60+
});
61+
62+
test('parses major version 1 to newest 1.x version', () => {
63+
const url = 'https://example.com?version=1';
64+
const result = parseUrlParams(url);
65+
expect(result.version).toMatch(/^1\.\d+\.\d+$/);
66+
expect(p5VersionStrings).toContain(result.version);
67+
});
68+
69+
test('parses major.minor version 1.1.x to newest patch', () => {
70+
const url = 'https://example.com?version=1.1';
71+
const result = parseUrlParams(url);
72+
expect(result.version).toMatch(/^1\.1\.\d+$/);
73+
expect(p5VersionStrings).toContain(result.version);
74+
});
75+
76+
test('parses major version 2 to newest 2.x version', () => {
77+
const url = 'https://example.com?version=2';
78+
const result = parseUrlParams(url);
79+
expect(result.version).toMatch(/^2\.\d+\.\d+$/);
80+
expect(p5VersionStrings).toContain(result.version);
81+
});
82+
83+
test('parses major.minor version 2.0.x to newest patch', () => {
84+
const url = 'https://example.com?version=2.0';
85+
const result = parseUrlParams(url);
86+
expect(result.version).toMatch(/^2\.0\.\d+$/);
87+
expect(p5VersionStrings).toContain(result.version);
88+
});
89+
});
90+
91+
describe('boolean param parsing', () => {
92+
test('parses boolean-like params for sound/preload/shapes/data (true variants)', () => {
93+
const trueVariants = ['on', 'true', '1', 'ON', 'True'];
94+
95+
trueVariants.forEach((v) => {
96+
const url = `https://example.com?sound=${v}&preload=${v}&shapes=${v}&data=${v}`;
97+
const result = parseUrlParams(url);
98+
expect(result.sound).toBe(true);
99+
expect(result.preload).toBe(true);
100+
expect(result.shapes).toBe(true);
101+
expect(result.data).toBe(true);
102+
});
103+
});
104+
105+
test('parses boolean-like params for sound/preload/shapes/data (false variants)', () => {
106+
const falseVariants = ['off', 'false', '0', 'OFF', 'False'];
107+
108+
falseVariants.forEach((v) => {
109+
const url = `https://example.com?sound=${v}&preload=${v}&shapes=${v}&data=${v}`;
110+
const result = parseUrlParams(url);
111+
expect(result.sound).toBe(false);
112+
expect(result.preload).toBe(false);
113+
expect(result.shapes).toBe(false);
114+
expect(result.data).toBe(false);
115+
});
116+
});
117+
});
118+
});

client/utils/parseURLParams.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ function getVersionString(
2121
return typeof item === 'string' ? item : item.version;
2222
}
2323

24+
export const p5VersionStrings = p5Versions.map(getVersionString);
25+
2426
function getNewestVersion(versions: string[]): string {
2527
return versions.sort((a, b) => {
2628
const pa = a.split('.').map((n) => parseInt(n, 10));
@@ -39,15 +41,13 @@ function validateVersion(version: string | null): string {
3941

4042
const ver = String(version).trim();
4143

42-
const versions = p5Versions.map(getVersionString);
43-
44-
if (versions.includes(ver)) return ver;
44+
if (p5VersionStrings.includes(ver)) return ver;
4545

4646
// if only major.minor provided like "1.11"
4747
const majorMinorMatch = /^(\d+)\.(\d+)$/.exec(ver);
4848
if (majorMinorMatch) {
4949
const [, major, minor] = majorMinorMatch;
50-
const matches = versions.filter((v) => {
50+
const matches = p5VersionStrings.filter((v) => {
5151
const parts = v.split('.');
5252
return parts[0] === major && parts[1] === minor;
5353
});
@@ -60,7 +60,7 @@ function validateVersion(version: string | null): string {
6060
const majorOnlyMatch = /^(\d+)$/.exec(ver);
6161
if (majorOnlyMatch) {
6262
const [, major] = majorOnlyMatch;
63-
const matches = versions.filter((v) => v.split('.')[0] === major);
63+
const matches = p5VersionStrings.filter((v) => v.split('.')[0] === major);
6464
if (matches.length) {
6565
return getNewestVersion(matches);
6666
}

0 commit comments

Comments
 (0)