diff --git a/composables/billing/importMatches.test.ts b/composables/billing/importMatches.test.ts new file mode 100644 index 0000000..207affc --- /dev/null +++ b/composables/billing/importMatches.test.ts @@ -0,0 +1,25 @@ +import { expect, test } from 'bun:test' +import { readFileSync } from 'node:fs' +import { scopeImportMatches } from './importMatches' + +const matches = ['copilot', 'devin'].map(provider => ({ target_provider: provider, target_model: 'shared-model', source_provider: 'catalog', source_model: `${provider}-match` })) +test('batch scoping keeps stored matches available when switching back', () => { + const original = structuredClone(matches) + const preview = (providers: string[]) => scopeImportMatches(providers.map(provider => ({ provider, model: 'shared-model' })), matches).map(match => match.source_model) + expect(preview(['copilot', 'devin'])).toEqual(['copilot-match', 'devin-match']) + expect(preview(['devin'])).toEqual(['devin-match']) + expect(preview([])).toEqual([]) + expect(preview(['copilot'])).toEqual(['copilot-match']) + expect(matches).toEqual(original) +}) +test('provider identity ignores whitespace/case but model IDs retain case', () => { + expect(scopeImportMatches([{ provider: ' Copilot ', model: ' shared-model ' }], matches)).toEqual([matches[0]!]) + expect(scopeImportMatches([{ provider: 'copilot', model: 'Shared-Model' }], matches)).toEqual([]) +}) +test('preview integration scopes the payload without clearing stored choices or weakening validation', () => { + const source = readFileSync(new URL('./useBillingPriceImport.ts', import.meta.url), 'utf8') + expect(source).toContain('match_overrides: scopeImportMatches(targets, matchOverrides).map(') + expect(source).not.toContain('overrideKeys.some(key => !targetKeys.includes(key))') + expect(source).toContain('new Set(overrideKeys).size !== overrideKeys.length') + expect(source).toContain('controller.signal.aborted || version !== inputVersion') +}) diff --git a/composables/billing/importMatches.ts b/composables/billing/importMatches.ts new file mode 100644 index 0000000..578e94f --- /dev/null +++ b/composables/billing/importMatches.ts @@ -0,0 +1,9 @@ +type ImportTarget = { provider: string; model: string } +type ImportMatch = { target_provider: string; target_model: string; source_provider: string; source_model: string } +const identity = (provider: string, model: string) => `${provider.trim().toLowerCase()}::${model.trim()}` + +// Keep stored catalog choices, but send only overrides belonging to this batch. +export function scopeImportMatches(targets: ImportTarget[], matches: ImportMatch[]): ImportMatch[] { + const requested = new Set(targets.map(target => identity(target.provider, target.model))) + return matches.filter(match => requested.has(identity(match.target_provider, match.target_model))) +} diff --git a/composables/billing/useBillingPriceImport.ts b/composables/billing/useBillingPriceImport.ts index 064422b..54b1a51 100644 --- a/composables/billing/useBillingPriceImport.ts +++ b/composables/billing/useBillingPriceImport.ts @@ -1,4 +1,5 @@ import { resolveImportSelection } from './importSelection' +import { scopeImportMatches } from './importMatches' import { billingImportApplySupportError, createBillingImportIdempotencyKey, billingImportStorageKey, isDefinitiveBillingImportFailure, readBillingImportPending, validateBillingImportResult, writeBillingImportPending, type BillingImportPendingApply } from '~/composables/billingImport' import { billingCatalogSources, defaultBillingImportPreferences, readBillingImportPreferences, writeBillingImportPreferences, type BillingCatalogSource, type BillingImportPreferences } from '~/composables/billingPreferences' import { computed, ref, reactive, watch, onMounted, onScopeDispose, provide, inject, type InjectionKey } from 'vue' @@ -275,7 +276,7 @@ const validationError = computed(() => { const rowKeys = rowMultipliers.map(row => row.row_key.trim()) if (rowMultipliers.some(row => !row.row_key.trim() || !positive(row.multiplier)) || new Set(rowKeys).size !== rowKeys.length) return t('billing.import.err.rowMultiplier') const overrideKeys = matchOverrides.map(item => `${item.target_provider.trim().toLowerCase()}::${item.target_model.trim()}`) - if (matchOverrides.some(item => !item.target_provider.trim() || !item.target_model.trim() || !item.source_provider.trim() || !item.source_model.trim()) || new Set(overrideKeys).size !== overrideKeys.length || overrideKeys.some(key => !targetKeys.includes(key))) return t('billing.import.err.override') + if (matchOverrides.some(item => !item.target_provider.trim() || !item.target_model.trim() || !item.source_provider.trim() || !item.source_model.trim()) || new Set(overrideKeys).size !== overrideKeys.length) return t('billing.import.err.override') return '' }) const requiresOverwrite = computed(() => (preview.value?.rows || []).some((row: any) => selected.value.includes(row.row_key) && row.action === 'overwrite')) @@ -307,7 +308,7 @@ async function previewPrices(updates = false, preferredSelection?: string[]) { multiplier_rules: multiplierRules.map((rule, index) => ({ id: rule.id || `rule-${index + 1}`, label: rule.label.trim(), match_mode: rule.match_mode, pattern: rule.pattern.trim(), multiplier: rule.multiplier })), row_multipliers: Object.fromEntries(rowMultipliers.map(row => [row.row_key.trim(), row.multiplier])) }, - match_overrides: matchOverrides.map(item => ({ target_provider: item.target_provider.trim(), target_model: item.target_model.trim(), source_provider: item.source_provider.trim(), source_model: item.source_model.trim() })) + match_overrides: scopeImportMatches(targets, matchOverrides).map(item => ({ target_provider: item.target_provider.trim(), target_model: item.target_model.trim(), source_provider: item.source_provider.trim(), source_model: item.source_model.trim() })) } }) if (disposed || session !== sessionVersion || controller.signal.aborted || version !== inputVersion) return