Files
WeHub Mirror 6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
WeHub snapshot of cb28d14c6f2c081de7a0d8729a8c816c9adef67a
2026-08-10 11:17:50 +08:00

96 lines
3.6 KiB
TypeScript

/**
* @vitest-environment node
*/
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'
import * as billingAttributionModule from '@/lib/billing/core/billing-attribution'
import * as usageLogModule from '@/lib/billing/core/usage-log'
import * as thresholdBillingModule from '@/lib/billing/threshold-billing'
import * as embeddingModelsModule from '@/lib/knowledge/embedding-models'
import { recordSearchEmbeddingUsage } from '@/lib/knowledge/embeddings'
import * as tokenizationModule from '@/lib/tokenization'
import * as providersUtilsModule from '@/providers/utils'
/**
* Spy on the real module namespaces instead of vi.mock: under `isolate: false`
* `@/lib/knowledge/embeddings` is a shared consumer cached across test files,
* so vi.mock here would bind this file's fixtures into it for every later
* file. Patching the real namespaces (and restoring afterAll) is the only
* wiring that composes.
*/
const mockRecordUsage = vi
.spyOn(usageLogModule, 'recordUsage')
.mockResolvedValue(undefined as never)
const mockToBillingContext = vi.spyOn(billingAttributionModule, 'toBillingContext')
const mockCheckAndBillPayerOverageThreshold = vi
.spyOn(thresholdBillingModule, 'checkAndBillPayerOverageThreshold')
.mockResolvedValue(undefined as never)
const mockCalculateCost = vi.spyOn(providersUtilsModule, 'calculateCost')
const estimateTokenCountSpy = vi
.spyOn(tokenizationModule, 'estimateTokenCount')
.mockReturnValue({ count: 100 } as never)
const getEmbeddingModelInfoSpy = vi
.spyOn(embeddingModelsModule, 'getEmbeddingModelInfo')
.mockReturnValue({ tokenizerProvider: 'openai' } as never)
afterAll(() => {
mockRecordUsage.mockRestore()
mockToBillingContext.mockRestore()
mockCheckAndBillPayerOverageThreshold.mockRestore()
mockCalculateCost.mockRestore()
estimateTokenCountSpy.mockRestore()
getEmbeddingModelInfoSpy.mockRestore()
})
describe('recordSearchEmbeddingUsage', () => {
beforeEach(() => {
vi.clearAllMocks()
mockRecordUsage.mockResolvedValue(undefined as never)
mockCheckAndBillPayerOverageThreshold.mockResolvedValue(undefined as never)
estimateTokenCountSpy.mockReturnValue({ count: 100 } as never)
getEmbeddingModelInfoSpy.mockReturnValue({ tokenizerProvider: 'openai' } as never)
mockCalculateCost.mockReturnValue({ total: 0.01 } as never)
mockToBillingContext.mockReturnValue({
billingEntity: { type: 'organization', id: 'org-1' },
billingPeriod: {
start: new Date('2026-07-01T00:00:00.000Z'),
end: new Date('2026-08-01T00:00:00.000Z'),
},
})
})
it('records and bills against the attributed workspace payer', async () => {
await recordSearchEmbeddingUsage({
userId: 'actor-1',
workspaceId: 'ws-1',
embeddingModel: 'text-embedding-3-small',
query: 'test query',
isBYOK: false,
sourceReference: 'search-1',
billingAttribution: {
actorUserId: 'actor-1',
workspaceId: 'ws-1',
organizationId: 'org-1',
billedAccountUserId: 'owner-1',
billingEntity: { type: 'organization', id: 'org-1' },
billingPeriod: {
start: '2026-07-01T00:00:00.000Z',
end: '2026-08-01T00:00:00.000Z',
},
payerSubscription: null,
},
})
expect(mockRecordUsage).toHaveBeenCalledWith(
expect.objectContaining({
userId: 'actor-1',
workspaceId: 'ws-1',
billingEntity: { type: 'organization', id: 'org-1' },
})
)
expect(mockCheckAndBillPayerOverageThreshold).toHaveBeenCalledWith({
type: 'organization',
id: 'org-1',
})
})
})