Skip to content

Commit 7204ac2

Browse files
committed
fix(platform): Filter KV namespace lookups by exact title.
1 parent 042efd3 commit 7204ac2

3 files changed

Lines changed: 118 additions & 4 deletions

File tree

‎packages/void/src/cli/platform-cloudflare.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -610,9 +610,6 @@ export class PlatformCloudflareClient {
610610
const entries: Array<{ id?: unknown; title?: unknown }> = [];
611611
for (let page = 1; ; page++) {
612612
const query = new URLSearchParams({ page: String(page), per_page: '100' });
613-
if (title) {
614-
query.set('title', title);
615-
}
616613
const result = await this.request<ReadonlyArray<{ id?: unknown; title?: unknown }>>(
617614
`/accounts/${encodeURIComponent(accountId)}/storage/kv/namespaces?${query}`,
618615
);
@@ -621,10 +618,13 @@ export class PlatformCloudflareClient {
621618
break;
622619
}
623620
}
624-
return entries.map((entry) => ({
621+
const namespaces = entries.map((entry) => ({
625622
id: requiredString(entry.id, 'KV namespace id'),
626623
name: requiredString(entry.title, 'KV namespace title'),
627624
}));
625+
// The KV API lists all namespaces; it does not filter by a `title` parameter.
626+
// Filter only after pagination and validation, without hiding real duplicates.
627+
return title ? namespaces.filter((namespace) => namespace.name === title) : namespaces;
628628
}
629629

630630
async createKvNamespace(accountId: string, title: string): Promise<CloudflareNamedResource> {

‎packages/void/test/unit/platform-cloudflare.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,56 @@ describe('platform Cloudflare client', () => {
569569
expect(pages).toEqual(['1', '2']);
570570
});
571571

572+
it('filters KV namespaces by exact title instead of trusting an unsupported API filter', async () => {
573+
const entries = [
574+
{ id: 'routing-id', title: 'void-example-routing' },
575+
{ id: 'isr-id', title: 'void-example-isr-cache' },
576+
{ id: 'legacy-id', title: 'void-example-legacy-assets' },
577+
{ id: 'other-id', title: 'void-example-routing-other' },
578+
];
579+
const client = new PlatformCloudflareClient('token', {
580+
fetcher: async () => Response.json({ success: true, result: entries }),
581+
});
582+
await expect(client.listKvNamespaces('account', 'void-example-routing')).resolves.toEqual([
583+
{ id: 'routing-id', name: 'void-example-routing' },
584+
]);
585+
await expect(client.listKvNamespaces('account', 'missing')).resolves.toEqual([]);
586+
await expect(client.listKvNamespaces('account')).resolves.toHaveLength(4);
587+
});
588+
589+
it('keeps paginating before filtering and retains genuinely duplicate titles', async () => {
590+
const pages: Array<string> = [];
591+
const client = new PlatformCloudflareClient('token', {
592+
fetcher: async (input) => {
593+
const page = new URL(String(input)).searchParams.get('page')!;
594+
pages.push(page);
595+
const result =
596+
page === '1'
597+
? Array.from({ length: 100 }, (_, index) => ({
598+
id: `id-${index}`,
599+
title: index === 99 ? 'routing' : `unrelated-${index}`,
600+
}))
601+
: [{ id: 'another-routing', title: 'routing' }];
602+
return Response.json({ success: true, result });
603+
},
604+
});
605+
await expect(client.listKvNamespaces('account', 'routing')).resolves.toEqual([
606+
{ id: 'id-99', name: 'routing' },
607+
{ id: 'another-routing', name: 'routing' },
608+
]);
609+
expect(pages).toEqual(['1', '2']);
610+
});
611+
612+
it('does not turn malformed KV inventory into an empty search result', async () => {
613+
const client = new PlatformCloudflareClient('token', {
614+
fetcher: async () =>
615+
Response.json({ success: true, result: [{ id: 'unknown', title: null }] }),
616+
});
617+
await expect(client.listKvNamespaces('account', 'routing')).rejects.toThrow(
618+
'KV namespace title',
619+
);
620+
});
621+
572622
it('exhausts paginated collision checks for platform resource names', async () => {
573623
const calls: Array<string> = [];
574624
const client = new PlatformCloudflareClient('token', {

‎packages/void/test/unit/platform-plan.test.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,70 @@ function legacyInstallation(suffix = '0123456789ab') {
8080
}
8181

8282
describe('platform installation planning', () => {
83+
it('resumes the recorded namespaces using the real unfiltered KV API response', async () => {
84+
const initial = await createPlatformInstallPlan(client(), input, null);
85+
let manifest = createPlatformInstallationManifest({
86+
account: initial.account,
87+
deployment: initial.deployment,
88+
id: initial.installationId,
89+
ownershipNonce: '0123456789abcdef0123456789abcdef',
90+
platformVersion: '1.0.0',
91+
createdAt: '2026-09-17T00:00:00.000Z',
92+
});
93+
for (const [key, name] of [
94+
['routing-kv', initial.deployment.resources.routingKv],
95+
['isr-cache-kv', initial.deployment.resources.isrCacheKv],
96+
['legacy-assets-kv', initial.deployment.resources.legacyAssetsKv],
97+
]) {
98+
manifest = recordInstallationResource(
99+
manifest,
100+
{
101+
key,
102+
id: `${key}-id`,
103+
name,
104+
kind: 'kv-namespace',
105+
ownership: 'created',
106+
state: 'ready',
107+
configHash: 'test',
108+
},
109+
'2026-09-17T00:00:00.000Z',
110+
);
111+
}
112+
const entries = manifest.resources.map(({ id, name }) => ({ id, title: name }));
113+
const api = new PlatformCloudflareClient('test-token', {
114+
fetcher: async (url, init) => {
115+
expect(String(url)).toContain('/storage/kv/namespaces?');
116+
expect(init?.method ?? 'GET').toBe('GET');
117+
return Response.json({ success: true, result: entries });
118+
},
119+
});
120+
const inspected: Array<string> = [];
121+
const plannerClient = client({
122+
listKvNamespaces: api.listKvNamespaces.bind(api),
123+
getKvValue: async (_account: string, id: string) => {
124+
inspected.push(id);
125+
return id === 'routing-kv-id' ? JSON.stringify(manifest) : null;
126+
},
127+
});
128+
const resumed = await createPlatformInstallPlan(plannerClient, input, manifest);
129+
expect(inspected).toEqual(['routing-kv-id']);
130+
expect(resumed.actions.filter((action) => action.kind === 'kv-namespace')).toEqual(
131+
expect.arrayContaining(
132+
manifest.resources.map((resource) =>
133+
expect.objectContaining({
134+
action: 'reuse',
135+
id: resource.id,
136+
name: resource.name,
137+
}),
138+
),
139+
),
140+
);
141+
entries.push({ id: 'unowned-duplicate', title: initial.deployment.resources.routingKv });
142+
await expect(createPlatformInstallPlan(plannerClient, input, manifest)).rejects.toThrow(
143+
'More than one kv-namespace',
144+
);
145+
});
146+
83147
it.each([false, true])(
84148
'bolds names and labels, not account IDs or URLs (workers.dev: %s)',
85149
async (workersDev) => {

0 commit comments

Comments
 (0)