Fix row.spec.ts

This commit is contained in:
Sam Rose 2025-02-04 10:43:49 +00:00
parent a3c4dede60
commit 72a645b80e
No known key found for this signature in database
2 changed files with 42 additions and 15 deletions

View File

@ -1,5 +1,12 @@
import { Feature, License, Quotas } from "@budibase/types" import {
Feature,
License,
MonthlyQuotaName,
QuotaType,
QuotaUsageType,
} from "@budibase/types"
import cloneDeep from "lodash/cloneDeep" import cloneDeep from "lodash/cloneDeep"
import merge from "lodash/merge"
let CLOUD_FREE_LICENSE: License let CLOUD_FREE_LICENSE: License
let UNLIMITED_LICENSE: License let UNLIMITED_LICENSE: License
@ -27,18 +34,19 @@ export function initInternal(opts: {
export interface UseLicenseOpts { export interface UseLicenseOpts {
features?: Feature[] features?: Feature[]
quotas?: Quotas monthlyQuotas?: [MonthlyQuotaName, number][]
} }
// LICENSES // LICENSES
export const useLicense = (license: License, opts?: UseLicenseOpts) => { export const useLicense = (license: License, opts?: UseLicenseOpts) => {
if (opts) { if (opts?.features) {
if (opts.features) { license.features.push(...opts.features)
license.features.push(...opts.features) }
} if (opts?.monthlyQuotas) {
if (opts.quotas) { for (const [name, value] of opts.monthlyQuotas) {
license.quotas = opts.quotas license.quotas[QuotaType.USAGE][QuotaUsageType.MONTHLY][name].value =
value
} }
} }
@ -57,12 +65,9 @@ export const useCloudFree = () => {
// FEATURES // FEATURES
const useFeature = (feature: Feature) => { const useFeature = (feature: Feature, extra?: Partial<UseLicenseOpts>) => {
const license = cloneDeep(getCachedLicense() || UNLIMITED_LICENSE) const license = cloneDeep(getCachedLicense() || UNLIMITED_LICENSE)
const opts: UseLicenseOpts = { const opts: UseLicenseOpts = merge({ features: [feature] }, extra)
features: [feature],
}
return useLicense(license, opts) return useLicense(license, opts)
} }
@ -102,8 +107,12 @@ export const useAppBuilders = () => {
return useFeature(Feature.APP_BUILDERS) return useFeature(Feature.APP_BUILDERS)
} }
export const useBudibaseAI = () => { export const useBudibaseAI = (opts?: { monthlyQuota?: number }) => {
return useFeature(Feature.BUDIBASE_AI) return useFeature(Feature.BUDIBASE_AI, {
monthlyQuotas: [
[MonthlyQuotaName.BUDIBASE_AI_CREDITS, opts?.monthlyQuota || 1000],
],
})
} }
export const useAICustomConfigs = () => { export const useAICustomConfigs = () => {

View File

@ -39,6 +39,7 @@ import {
RowExportFormat, RowExportFormat,
RelationSchemaField, RelationSchemaField,
FormulaResponseType, FormulaResponseType,
MonthlyQuotaName,
} from "@budibase/types" } from "@budibase/types"
import { generator, mocks } from "@budibase/backend-core/tests" import { generator, mocks } from "@budibase/backend-core/tests"
import _, { merge } from "lodash" import _, { merge } from "lodash"
@ -169,6 +170,18 @@ if (descriptions.length) {
) )
} }
const resetRowUsage = async () => {
await config.doInContext(
undefined,
async () =>
await quotas.setUsage(
0,
StaticQuotaName.ROWS,
QuotaUsageType.STATIC
)
)
}
const getRowUsage = async () => { const getRowUsage = async () => {
const { total } = await config.doInContext(undefined, () => const { total } = await config.doInContext(undefined, () =>
quotas.getCurrentUsageValues( quotas.getCurrentUsageValues(
@ -206,6 +219,10 @@ if (descriptions.length) {
table = await config.api.table.save(defaultTable()) table = await config.api.table.save(defaultTable())
}) })
beforeEach(async () => {
await resetRowUsage()
})
describe("create", () => { describe("create", () => {
it("creates a new row successfully", async () => { it("creates a new row successfully", async () => {
const rowUsage = await getRowUsage() const rowUsage = await getRowUsage()
@ -3317,6 +3334,7 @@ if (descriptions.length) {
beforeAll(async () => { beforeAll(async () => {
mocks.licenses.useBudibaseAI() mocks.licenses.useBudibaseAI()
mocks.licenses.useAICustomConfigs() mocks.licenses.useAICustomConfigs()
envCleanup = setEnv({ envCleanup = setEnv({
OPENAI_API_KEY: "sk-abcdefghijklmnopqrstuvwxyz1234567890abcd", OPENAI_API_KEY: "sk-abcdefghijklmnopqrstuvwxyz1234567890abcd",
}) })