Skip to content

Commit d9319ad

Browse files
don't use generated helper funcs for determining spec, just hand roll it - add test
1 parent a2a6797 commit d9319ad

File tree

2 files changed

+88
-12
lines changed

2 files changed

+88
-12
lines changed

src/control/__tests__/configureIndex.test.ts

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { configureIndex } from '../configureIndex';
1+
import { configureIndex, getIndexSpecType } from '../configureIndex';
22
import { ManageIndexesApi } from '../../pinecone-generated-ts-fetch/db_control';
33
import type {
44
ConfigureIndexOperationRequest,
@@ -275,3 +275,66 @@ describe('configureIndex', () => {
275275
expect(IOA.configureIndex).not.toHaveBeenCalled();
276276
});
277277
});
278+
279+
describe('getIndexSpecType', () => {
280+
// IndexModelSpecFromJSON merges BYOC, PodBased, Serverless FromJSON results,
281+
// so spec can have pod/serverless/byoc all present with some undefined.
282+
// getIndexSpecType must classify by defined value, not key presence.
283+
284+
test('returns serverless when only serverless has a defined value (merged FromJSON shape)', () => {
285+
const spec = {
286+
pod: undefined,
287+
serverless: {
288+
cloud: 'aws',
289+
region: 'us-east-1',
290+
readCapacity: { mode: 'OnDemand', status: { state: 'Ready' } },
291+
},
292+
byoc: undefined,
293+
};
294+
expect(getIndexSpecType(spec as Parameters<typeof getIndexSpecType>[0])).toBe(
295+
'serverless',
296+
);
297+
});
298+
299+
test('returns pod when only pod has a defined value (merged FromJSON shape)', () => {
300+
const spec = {
301+
pod: {
302+
environment: 'us-east1-gcp',
303+
replicas: 4,
304+
shards: 1,
305+
pods: 4,
306+
podType: 'p2.x2',
307+
},
308+
serverless: undefined,
309+
byoc: undefined,
310+
};
311+
expect(getIndexSpecType(spec as Parameters<typeof getIndexSpecType>[0])).toBe(
312+
'pod',
313+
);
314+
});
315+
316+
test('returns byoc when only byoc has a defined value (merged FromJSON shape)', () => {
317+
const spec = {
318+
pod: undefined,
319+
serverless: undefined,
320+
byoc: {
321+
environment: 'my-env',
322+
readCapacity: { mode: 'OnDemand', status: { state: 'Ready' } },
323+
},
324+
};
325+
expect(getIndexSpecType(spec as Parameters<typeof getIndexSpecType>[0])).toBe(
326+
'byoc',
327+
);
328+
});
329+
330+
test('returns unknown for null or non-object', () => {
331+
expect(
332+
getIndexSpecType(null as unknown as Parameters<typeof getIndexSpecType>[0]),
333+
).toBe('unknown');
334+
expect(
335+
getIndexSpecType(
336+
undefined as unknown as Parameters<typeof getIndexSpecType>[0],
337+
),
338+
).toBe('unknown');
339+
});
340+
});

src/control/configureIndex.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import {
22
ManageIndexesApi,
33
IndexModel,
4+
type IndexModelSpec,
45
ConfigureIndexRequest,
56
ConfigureIndexRequestEmbed,
67
ConfigureIndexRequestSpec,
78
X_PINECONE_API_VERSION,
8-
instanceOfBYOC,
9-
instanceOfPodBased,
10-
instanceOfServerless,
119
} from '../pinecone-generated-ts-fetch/db_control';
1210
import { PineconeArgumentError } from '../errors';
1311
import type { IndexName } from './types';
@@ -125,22 +123,37 @@ export const configureIndex = (api: ManageIndexesApi) => {
125123
};
126124

127125
/**
128-
* Helper function to determine the spec type of an index using generated helper functions.
129-
* @param spec - The IndexModelSpec from describeIndex
126+
* Determines the spec type of an index by which spec key has a defined value.
127+
*
128+
* @param spec - The IndexModelSpec from describeIndex (may have multiple keys, only one defined)
130129
* @returns The spec type: 'pod', 'serverless', or 'byoc'
130+
* @internal Exported for testing
131131
*/
132-
const getIndexSpecType = (
133-
spec: any,
132+
export const getIndexSpecType = (
133+
spec: IndexModelSpec,
134134
): 'pod' | 'serverless' | 'byoc' | 'unknown' => {
135-
if (instanceOfPodBased(spec)) {
136-
return 'pod';
135+
if (spec == null || typeof spec !== 'object') {
136+
return 'unknown';
137137
}
138-
if (instanceOfServerless(spec)) {
138+
// Classify by which key has a defined object value (order is arbitrary but deterministic).
139+
// Use 'in' to narrow the union before reading the property.
140+
if (
141+
'serverless' in spec &&
142+
spec.serverless != null &&
143+
typeof spec.serverless === 'object'
144+
) {
139145
return 'serverless';
140146
}
141-
if (instanceOfBYOC(spec)) {
147+
if (
148+
'byoc' in spec &&
149+
spec.byoc != null &&
150+
typeof spec.byoc === 'object'
151+
) {
142152
return 'byoc';
143153
}
154+
if ('pod' in spec && spec.pod != null && typeof spec.pod === 'object') {
155+
return 'pod';
156+
}
144157
return 'unknown';
145158
};
146159

0 commit comments

Comments
 (0)