Skip to content

Commit b3dc0b2

Browse files
committed
fix: sanitize search query and add declarative test suite for bandcamp
1 parent 083e5b1 commit b3dc0b2

2 files changed

Lines changed: 120 additions & 2 deletions

File tree

‎dist/src/sources/bandcamp.js‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,56 @@ const JSON_LD_REGEX = /<script\s+type="application\/ld\+json"\s*>([\s\S]*?)<\/sc
2626
* Bandcamp source implementation.
2727
*/
2828
export default class BandcampSource {
29+
/**
30+
* Declarative test suite for validating Bandcamp source behavior.
31+
* @public
32+
*/
33+
static _TESTS = [
34+
{
35+
id: 'bandcamp.track.resolve',
36+
name: 'Bandcamp Track URL resolution',
37+
input: {
38+
type: 'resolve',
39+
value: 'https://carpenterbrut.bandcamp.com/track/maniac-feat-yann-ligner'
40+
},
41+
expect: {
42+
result: { type: 'track' },
43+
metadata: {
44+
title: { op: 'matches', value: 'MANIAC' },
45+
author: { op: 'matches', value: 'Carpenter Brut' },
46+
sourceName: { op: 'equals', value: 'bandcamp' }
47+
}
48+
}
49+
},
50+
{
51+
id: 'bandcamp.album.resolve',
52+
name: 'Bandcamp Album URL resolution',
53+
input: {
54+
type: 'resolve',
55+
value: 'https://carpenterbrut.bandcamp.com/album/trilogy'
56+
},
57+
expect: {
58+
result: { type: 'playlist' },
59+
collection: { minItems: 10 },
60+
metadata: {
61+
author: { op: 'matches', value: 'Carpenter Brut' }
62+
}
63+
}
64+
},
65+
{
66+
id: 'bandcamp.search.bcsearch',
67+
name: 'Bandcamp Search query (bcsearch:)',
68+
input: {
69+
type: 'search',
70+
value: 'carpenter brut maniac',
71+
searchType: 'bcsearch'
72+
},
73+
expect: {
74+
result: { type: 'search' },
75+
collection: { minItems: 1 }
76+
}
77+
}
78+
];
2979
/**
3080
* Shared worker runtime provided by the source manager.
3181
*/
@@ -87,13 +137,20 @@ export default class BandcampSource {
87137
* @returns Search results, an empty payload, or a structured exception.
88138
*/
89139
async search(query) {
140+
let cleanedQuery = query;
141+
for (const term of this.searchTerms) {
142+
if (cleanedQuery.toLowerCase().startsWith(`${term.toLowerCase()}:`)) {
143+
cleanedQuery = cleanedQuery.slice(term.length + 1).trim();
144+
break;
145+
}
146+
}
90147
try {
91148
const request = await makeRequest(BANDCAMP_SEARCH_API_URL, {
92149
method: 'POST',
93150
disableBodyCompression: true,
94151
headers: REQUEST_HEADERS,
95152
body: {
96-
search_text: query,
153+
search_text: cleanedQuery,
97154
search_filter: 't',
98155
full_page: true,
99156
fan_id: null

‎src/sources/bandcamp.ts‎

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {
1515
TrackUrlResult,
1616
WorkerNodeLink
1717
} from '../typings/sources/source.types.ts'
18+
import type { SourceTestCase } from '../typings/sources/test.types.ts'
1819
import type {
1920
HttpRequestResult,
2021
TrackEncodeInput
@@ -87,6 +88,58 @@ interface BandcampPlaylistTracklist {
8788
* Bandcamp source implementation.
8889
*/
8990
export default class BandcampSource {
91+
/**
92+
* Declarative test suite for validating Bandcamp source behavior.
93+
* @public
94+
*/
95+
public static readonly _TESTS: SourceTestCase[] = [
96+
{
97+
id: 'bandcamp.track.resolve',
98+
name: 'Bandcamp Track URL resolution',
99+
input: {
100+
type: 'resolve',
101+
value:
102+
'https://carpenterbrut.bandcamp.com/track/maniac-feat-yann-ligner'
103+
},
104+
expect: {
105+
result: { type: 'track' },
106+
metadata: {
107+
title: { op: 'matches', value: 'MANIAC' },
108+
author: { op: 'matches', value: 'Carpenter Brut' },
109+
sourceName: { op: 'equals', value: 'bandcamp' }
110+
}
111+
}
112+
},
113+
{
114+
id: 'bandcamp.album.resolve',
115+
name: 'Bandcamp Album URL resolution',
116+
input: {
117+
type: 'resolve',
118+
value: 'https://carpenterbrut.bandcamp.com/album/trilogy'
119+
},
120+
expect: {
121+
result: { type: 'playlist' },
122+
collection: { minItems: 10 },
123+
metadata: {
124+
author: { op: 'matches', value: 'Carpenter Brut' }
125+
}
126+
}
127+
},
128+
{
129+
id: 'bandcamp.search.bcsearch',
130+
name: 'Bandcamp Search query (bcsearch:)',
131+
input: {
132+
type: 'search',
133+
value: 'carpenter brut maniac',
134+
searchType: 'bcsearch'
135+
},
136+
expect: {
137+
result: { type: 'search' },
138+
collection: { minItems: 1 }
139+
}
140+
}
141+
]
142+
90143
/**
91144
* Shared worker runtime provided by the source manager.
92145
*/
@@ -156,13 +209,21 @@ export default class BandcampSource {
156209
* @returns Search results, an empty payload, or a structured exception.
157210
*/
158211
public async search(query: string): Promise<SourceResult> {
212+
let cleanedQuery = query
213+
for (const term of this.searchTerms) {
214+
if (cleanedQuery.toLowerCase().startsWith(`${term.toLowerCase()}:`)) {
215+
cleanedQuery = cleanedQuery.slice(term.length + 1).trim()
216+
break
217+
}
218+
}
219+
159220
try {
160221
const request = await makeRequest(BANDCAMP_SEARCH_API_URL, {
161222
method: 'POST',
162223
disableBodyCompression: true,
163224
headers: REQUEST_HEADERS,
164225
body: {
165-
search_text: query,
226+
search_text: cleanedQuery,
166227
search_filter: 't',
167228
full_page: true,
168229
fan_id: null

0 commit comments

Comments
 (0)