* feat: upgrade Prisma to 6.16.0 with no-rust engine - Update Prisma packages to 6.16.0 - Add PostgreSQL adapter dependency - Configure engineType: 'client' and provider: 'prisma-client' in schema - Update Prisma client instantiation with PostgreSQL adapter - Remove binaryTargets from generators (not needed with library engine) - Fix schema view issue by removing @id decorator from BookingTimeStatusDenormalized - Fix ESLint warning by removing non-null assertion Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * Web app running but types wrecked * web app running but build and type issues * Removed the connection pool * Fixed zod type issue * Fixed types in booking reference extension * Fixed test issues * Type checks passing it seems * Using cjs as moduleFormat * Fixing Prisma undefined * fix: update prismock initialization for Prisma 6.16 compatibility - Add @prisma/internals dependency for getDMMF() - Restructure prismock initialization to use createPrismock() with DMMF - Create Proxy that's returned from mock factory for proper spy support - Fixes 89 failing unit tests with 'Cannot read properties of undefined (reading datamodel)' error - Based on workaround from prismock issue #1482 All unit tests now pass (375 test files, 3323 tests passed) Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * fix: cast serviceAccountKey to Prisma.InputJsonValue in bookingScenario.ts - Apply type cast at lines 2493 and 2535 - Fixes type errors from Prisma 6.16 upgrade - Follows established pattern from delegationCredential.ts - Add eslint-disable for pre-existing any types - Rename unused appStoreLookupKey parameter to satisfy lint - All 3323 tests passing Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * chore: remove whitespace-only lines from bookingScenario.ts - Remove blank lines where eslint-disable comments were replaced - Cleanup from pre-commit hook formatting Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * Update is-prisma-available-check.ts * fix: remove datasources config when using Prisma Driver Adapters - Update customPrisma to create new adapter when datasources URL is provided - Remove datasources config from API v2 Prisma services (already in adapter) - Fixes 'Custom datasource configuration is not compatible with Prisma Driver Adapters' error Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * fix: use Pool instances for PrismaPg adapters in index.ts - Create Pool instance before passing to PrismaPg adapter - Update customPrisma to create Pool for custom connection strings - Matches working pattern from API v2 services - Fixes 'Invalid `prisma.$queryRawUnsafe()` invocation' error Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * Not using queryRawUnsafe * Trying anything at this point * Make sure the DB is ready first * Don't auto run migrations in CI mode * Revert "Make sure the DB is ready first" This reverts commit 2b20bd45c974f3d7e07d8b904bc7fcdae37cce03. * Dynamic import of prisma * Commenting where it seems to break * Backwards compatability for API v2 * fix: add explicit type annotations for map callbacks in API v2 - Add type annotation for map parameter in memberships.repository.ts - Add type annotation for map parameter in stripe.service.ts - Fixes implicit 'any' type errors from stricter Prisma 6.16.0 type inference Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * fix: add explicit type annotations for API v2 map callbacks - users.repository.ts:292: add Profile & { user: User } type - memberships.service.ts:19-20: add Membership type to filter callbacks Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * fix: add explicit type annotation for attributeToUser in organizations-users.repository.ts - organizations-users.repository.ts:63: add AttributeToUser with nested relations type Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * fix: add explicit Membership type annotations in teams.repository.ts Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * fix: use API v2 dedicated Prisma client to support adapter in PrismaClientOptions Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * Running API v2 build commands together so they all get the space size var * Fixing Maximum call depth exceeded error * fixed type issues * Trying to make the seed more stable * Revert "Trying to make the seed more stable" This reverts commit 1fd4495e6af7acd7981cda7dedec3168979b0e9d. * Fixed path to prisma client * Fixed type check * Fix eslint warnings * fix: externalize @prisma/adapter-pg and pg in platform-libraries Vite config - Add @prisma/adapter-pg and pg to external dependencies list - Add corresponding globals for these packages - Fix Prisma client aliases to point to packages/prisma/client instead of node_modules - Add Node.js resolve conditions to prefer Node.js exports - Keep commonjsOptions.include for proper CommonJS transformation - Add eslint-disable for __dirname in Vite config file - Remove problematic prettier/prettier eslint comment This fixes the 'Extensions.defineExtension is unable to run in this browser environment' error when running yarn generate-swagger in apps/api/v2 after upgrading to Prisma v6.16 with the no-rust engine approach. Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * fix: update Prisma imports in API v2 services to use package path - Change imports from '../../../generated/prisma/client' to '@calcom/prisma/client' - Fixes CI error: Cannot find module '../../../../../packages/prisma/generated/prisma/client.ts' - Aligns with backwards compatibility re-export structure after Prisma v6.16 upgrade Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * fix: remove .ts extension from Prisma client path mapping in tsconfig - Remove file extension from @calcom/prisma/client path mapping - Fixes runtime error: Cannot find module '../../../../../packages/prisma/generated/prisma/client.ts' - TypeScript path mappings should not include file extensions per best practices - Allows Node.js to correctly resolve to .js files at runtime Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * fix: resolve Prisma 6.16.0 type incompatibilities in bookingScenario tests - Changed InputPayment.data type from PaymentData to Prisma.InputJsonValue - Changed createCredentials key parameter from JsonValue to InputJsonValue - Removed unused PaymentData type definition - Resolves type errors at lines 709 and 1088 without using 'as any' casts Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * fix: remove non-existent Watchlist fields from test fixtures - Remove createdById from isLockedOrBlocked.test.ts (lines 15, 20) - Remove severity and createdById from _post.test.ts (line 110) - These fields don't exist in Watchlist model schema after Prisma 6.16.0 upgrade - Resolves TS2353 errors without using 'as any' casts Relates to PR #23816 Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * fix: api v2 imports generated prisma and platform libraries * fix: resolve type errors from Prisma 6.16 upgrade - Add missing markdownToSafeHTML import in AppCard.tsx - Fix organizationId null handling in fresh-booking.test.ts - Remove non-existent createdById field from Watchlist test utils Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * Put back some external rollups * Added back the resolve conditions * Stop using Pool directly * chore: remove prisma bookingReferenceExtension and update calls * fix: organizations-admin-not-team-member-event-types.e2e-spec.ts * chore: bring back POOL in api v2 prisma clients * chore: remove Pool but await connect * fixup! chore: remove Pool but await connect * chore: bring back Pool on all clients * chore: end pool manually * chore: test with pool max 1 * chore: e2e test prisma max pool of 1 connection * chore: give more control over pool for prisma module with env * remove pool from base prisma client * chore: prisma client in libraries use pool * Fixed types * chore: log pool events and improve pooling * Fixing some types and tests * Changing the parsing of USE_POOL * fix: ensure Prisma client is connected before seeding to prevent transaction errors Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * chore: adjust pools * chore: add process.env.USE_POOL to libraries vite config * fix: v1 _patch reference check bookingRef on the booking find * fix: v1 get references deleted null for system admin * test: add integration tests for bookingReference soft-delete behavior - Add bookingReference.integration-test.ts to test repository methods - Add handleDeleteCredential.integration-test.ts to test credential deletion cascade - Add booking-references.integration-test.ts for API v1 integration tests - All tests verify soft-delete behavior without using mocks - Tests use real database operations to ensure soft-deleted records persist - Cover scenarios: replacing references, credential deletion, querying with filters Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * refactor: convert booking-references test to actual API endpoint testing - Modified _get.ts to export handler function for testing - Refactored integration test to call API handler instead of directly testing Prisma - Added timestamps to test data to avoid conflicts - Tests now verify API layer correctly filters soft-deleted references - All 4 tests passing Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * fix: add explicit prisma.$connect() call to seed-insights script With Prisma 6.16 and the PostgreSQL adapter, scripts need to explicitly call $connect() before running database operations to ensure the connection pool is properly initialized. This prevents 'Transaction already closed' errors. Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * fix: add $connect() to main() execution in seed-insights Both main() and createPerformanceData() entry points need explicit prisma.$connect() calls with the Prisma 6.16 PostgreSQL adapter. Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * fix: always use connection pool for Prisma PostgreSQL adapter Enable connection pooling by default for the Prisma adapter to prevent transaction state issues during seed operations. Without a pool, each operation creates a new connection which can lead to 'Transaction already closed' errors during heavy database operations like seeding. Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * Revert "fix: always use connection pool for Prisma PostgreSQL adapter" This reverts commit 6724bb08e42bc0a94846069de83b04db0aeb8e8b. * fix: enable connection pool for db-seed in cache-db action Set USE_POOL=true when running yarn db-seed to use connection pooling with the Prisma PostgreSQL adapter. This prevents 'Transaction already closed' errors during seeding by maintaining stable database connections. Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * fix: add safety check for undefined ownerForEvent in seed script Prevent 'Cannot read properties of undefined' error when orgMembersInDBWithProfileId is empty. This can happen if organization members fail to create or when there's a duplicate constraint violation causing early return. Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * fix: v1 _patch reference check bookingRef * fix: increase pool size and add timeout settings to prevent transaction errors - Increase max connections from 5 to 10 - Add connectionTimeoutMillis: 30000 (30 seconds) - Add statement_timeout: 60000 (60 seconds) These settings help prevent 'Unknown transaction status' errors during heavy database operations like seeding by giving transactions more time to complete and allowing more concurrent connections. Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * Revert "fix: increase pool size and add timeout settings to prevent transaction errors" This reverts commit 148264f1f1861dfb09a082937a3e2b49e78fc41a. * fix: remove standalone execution in seed-app-store to prevent premature disconnect The seed-app-store.ts file had a standalone main() call at the bottom that would execute immediately when imported, including a prisma.$disconnect() in its .finally() block. This caused issues because: 1. seed.ts imports and calls mainAppStore() 2. The import triggers the standalone main() execution 3. This standalone execution disconnects prisma after completion 4. seed.ts then tries to call mainHugeEventTypesSeed() but prisma is disconnected 5. This leads to 'Unknown transaction status' errors Fixed by removing the standalone execution since mainAppStore() is already called programmatically from seed.ts which manages the connection lifecycle. Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * fix: use require.main check to prevent premature disconnect when imported Added require.main === module check so seed-app-store.ts: - Runs standalone with proper connection management when executed directly via 'yarn seed-app-store' or 'ts-node seed-app-store.ts' - Does NOT run standalone when imported as a module by seed.ts, preventing premature prisma disconnect This fixes 'Unknown transaction status' errors while maintaining backward compatibility for direct execution. Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * fix: seed apps before creating users to prevent foreign key constraint violation Reordered seeding operations to call mainAppStore() before main() because: - main() creates users with credentials that reference apps via appId foreign key - mainAppStore() seeds the App table with app records - Apps must exist before credentials can reference them This fixes the 'Foreign key constraint violated on Credential_appId_fkey' error that occurred when creating credentials before the apps they reference existed. Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * Apply suggestion from @cubic-dev-ai[bot] Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * Removing functional changes of deleted: null * Apply suggestion from @keithwillcode * refactor: move seedAppData call to bottom of main() in seed.ts Moved seedAppData() call from seed-app-store.ts to the bottom of main() in seed.ts to ensure the 'pro' user is created before attempting to create routing form data for them. Changes: - Exported seedAppData function from seed-app-store.ts - Removed seedAppData() call from the main() export in seed-app-store.ts - Added seedAppData() call at the bottom of main() in seed.ts - Updated standalone execution in seed-app-store.ts to still call seedAppData() when run directly via 'yarn seed-app-store' This ensures proper ordering: apps seeded → users created → routing form data created for existing users. Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * refactor: move routing form seeding from seed-app-store.ts to seed.ts Moved the routing form seeding logic (previously in seedAppData function) from seed-app-store.ts to be inline at the bottom of main() in seed.ts. This ensures the 'pro' user is created before attempting to create routing form data for them. Changes: - Removed seedAppData function and seededForm export from seed-app-store.ts - Removed import of seedAppData from seed.ts - Added routing form seeding logic inline at bottom of main() in seed.ts Seeding order: apps → users (including 'pro') → routing forms Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * fix: add deleted: null filter to bookingReference update operations - Add deleted: null filter to API v1 PATCH endpoint to prevent updating soft-deleted booking references - Add deleted: null filter to DailyVideo updateMeetingTokenIfExpired and setEnableRecordingUIAndUserIdForOrganizer - Add comprehensive test coverage for PATCH endpoint soft-delete behavior - Tests verify that soft-deleted booking references cannot be updated - Tests verify that only active booking references can be updated successfully Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * revert: remove deleted: null filters to preserve existing functionality Per @keithwillcode's feedback, reverting the soft-delete filtering changes to preserve existing functionality in this PR. This PR should focus only on the Prisma upgrade itself. - Reverted API v1 PATCH endpoint change - Reverted DailyVideo adapter changes (updateMeetingTokenIfExpired and setEnableRecordingUIAndUserIdForOrganizer) - Removed test file that was added for soft-delete behavior testing Addresses comments: - https://github.com/calcom/cal.com/pull/23816#discussion_r2448854197 - https://github.com/calcom/cal.com/pull/23816#discussion_r2448860594 - https://github.com/calcom/cal.com/pull/23816#discussion_r2448860833 Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * test: restore and update booking reference tests to match existing functionality Updated tests to verify existing behavior where PATCH endpoint can update booking references regardless of their deleted status. This matches the current implementation after reverting the deleted: null filters. Changes: - Restored test file that was previously deleted - Updated PATCH tests to expect successful updates of soft-deleted references - Renamed test suite to 'Existing functionality' to clarify intent - Tests now verify that the PATCH endpoint preserves existing behavior Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * test: rename booking-references test to integration-test The test requires a database connection and should run in the integration test job, not the unit test job. Renamed from .test.ts to .integration-test.ts to match the repository's testing conventions. Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: cal.com <morgan@cal.com> Co-authored-by: Morgan <33722304+ThyMinimalDev@users.noreply.github.com> Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
1775 lines
44 KiB
TypeScript
1775 lines
44 KiB
TypeScript
import { expect, describe, it, vi } from "vitest";
|
|
|
|
import type { AttributesQueryValue, RaqbChild } from "@calcom/lib/raqb/types";
|
|
import type { Attribute } from "@calcom/lib/service/attribute/server/getAttributes";
|
|
import { AttributeType } from "@calcom/prisma/enums";
|
|
import { RoutingFormFieldType } from "@calcom/routing-forms/lib/FieldTypes";
|
|
|
|
import { getValueOfAttributeOption, acrossQueryValueCompatiblity } from "./raqbUtils";
|
|
|
|
const { resolveQueryValue } = acrossQueryValueCompatiblity;
|
|
|
|
// Mock the getFieldResponseValueAsLabel
|
|
vi.mock("@calcom/app-store/routing-forms/lib/getFieldResponseValueAsLabel", () => ({
|
|
getFieldResponseValueAsLabel: ({ fieldResponseValue }: { field: any; fieldResponseValue: any }) => {
|
|
// For testing, just return the value as-is
|
|
return fieldResponseValue;
|
|
},
|
|
}));
|
|
|
|
// Test Data Builders for AttributesQueryValue
|
|
const createQueryValueRule = (overrides?: Partial<RaqbChild>): RaqbChild => ({
|
|
type: "rule",
|
|
properties: {
|
|
field: "default-field",
|
|
operator: "select_equals",
|
|
value: ["default-value"],
|
|
valueSrc: ["value"],
|
|
valueError: [null],
|
|
valueType: ["select"],
|
|
},
|
|
...overrides,
|
|
});
|
|
|
|
const createAttributesQueryValue = (overrides?: {
|
|
id?: string;
|
|
type?: "group" | "switch_group";
|
|
children1?: Record<string, RaqbChild>;
|
|
properties?: any;
|
|
}): AttributesQueryValue => ({
|
|
id: overrides?.id || "test-id",
|
|
type: overrides?.type || "group",
|
|
children1: overrides?.children1 || {},
|
|
properties: overrides?.properties,
|
|
});
|
|
|
|
const createQueryValueWithRule = ({
|
|
ruleId,
|
|
field,
|
|
operator,
|
|
value,
|
|
valueSrc = ["value"],
|
|
valueError = [null],
|
|
valueType = ["select"],
|
|
}: {
|
|
ruleId: string;
|
|
field: string;
|
|
operator: string;
|
|
value: any[];
|
|
valueSrc?: string[];
|
|
valueError?: (string | null)[];
|
|
valueType?: string[];
|
|
}): AttributesQueryValue => {
|
|
return createAttributesQueryValue({
|
|
children1: {
|
|
[ruleId]: createQueryValueRule({
|
|
type: "rule",
|
|
properties: {
|
|
field,
|
|
operator,
|
|
value,
|
|
valueSrc,
|
|
valueError,
|
|
valueType,
|
|
},
|
|
}),
|
|
},
|
|
});
|
|
};
|
|
|
|
const createComplexQueryValue = ({
|
|
id,
|
|
rules,
|
|
}: {
|
|
id?: string;
|
|
rules: Array<{
|
|
ruleId: string;
|
|
field: string;
|
|
operator: string;
|
|
value: any[];
|
|
valueSrc?: string[];
|
|
valueError?: (string | null)[];
|
|
valueType?: string[];
|
|
}>;
|
|
}): AttributesQueryValue => {
|
|
const children1: Record<string, RaqbChild> = {};
|
|
|
|
rules.forEach((rule) => {
|
|
children1[rule.ruleId] = createQueryValueRule({
|
|
type: "rule",
|
|
properties: {
|
|
field: rule.field,
|
|
operator: rule.operator,
|
|
value: rule.value,
|
|
valueSrc: rule.valueSrc || ["value"],
|
|
valueError: rule.valueError || [null],
|
|
valueType: rule.valueType || ["select"],
|
|
},
|
|
});
|
|
});
|
|
|
|
return createAttributesQueryValue({
|
|
id,
|
|
children1,
|
|
});
|
|
};
|
|
|
|
const createNestedGroupQueryValue = ({
|
|
groups,
|
|
}: {
|
|
groups: Array<{
|
|
groupId: string;
|
|
rules: Array<{
|
|
ruleId: string;
|
|
field: string;
|
|
operator?: string;
|
|
value: any[];
|
|
valueSrc?: string[];
|
|
valueError?: (string | null)[];
|
|
valueType?: string[];
|
|
}>;
|
|
}>;
|
|
}): AttributesQueryValue => {
|
|
const children1: Record<string, RaqbChild> = {};
|
|
|
|
groups.forEach((group) => {
|
|
const groupChildren: Record<string, RaqbChild> = {};
|
|
|
|
group.rules.forEach((rule) => {
|
|
groupChildren[rule.ruleId] = createQueryValueRule({
|
|
type: "rule",
|
|
properties: {
|
|
field: rule.field,
|
|
operator: rule.operator || "select_equals",
|
|
value: rule.value,
|
|
valueSrc: rule.valueSrc || ["value"],
|
|
valueError: rule.valueError || [null],
|
|
valueType: rule.valueType || ["select"],
|
|
},
|
|
});
|
|
});
|
|
|
|
children1[group.groupId] = {
|
|
type: "group",
|
|
children1: groupChildren,
|
|
} as RaqbChild;
|
|
});
|
|
|
|
return createAttributesQueryValue({
|
|
type: "group",
|
|
children1,
|
|
});
|
|
};
|
|
|
|
describe("getValueOfAttributeOption", () => {
|
|
it("should return non-array value for non-array input - It is a requirement for RAQB to not unnecessarily transform single value to an array of one item", () => {
|
|
const input = { value: "option1", isGroup: false, contains: [] };
|
|
const result = getValueOfAttributeOption(input);
|
|
expect(result).toEqual("option1");
|
|
});
|
|
|
|
it("should return flat array of values for simple options", () => {
|
|
const input = [
|
|
{ value: "option1", isGroup: false, contains: [] },
|
|
{ value: "option2", isGroup: false, contains: [] },
|
|
];
|
|
|
|
const result = getValueOfAttributeOption(input);
|
|
expect(result).toEqual(["option1", "option2"]);
|
|
});
|
|
|
|
it("should flatten nested options from contains array", () => {
|
|
const input = [
|
|
{
|
|
value: "group1",
|
|
isGroup: true,
|
|
contains: [
|
|
{ value: "suboption1", id: "opt-1", slug: "option-1" },
|
|
{ value: "suboption2", id: "opt-2", slug: "option-2" },
|
|
],
|
|
},
|
|
];
|
|
|
|
const result = getValueOfAttributeOption(input);
|
|
expect(result).toEqual(["suboption1", "suboption2"]);
|
|
});
|
|
|
|
it("should handle mix of simple and nested options", () => {
|
|
const input = [
|
|
{ value: "option1", isGroup: false, contains: [] },
|
|
{
|
|
value: "group1",
|
|
isGroup: true,
|
|
contains: [
|
|
{ value: "suboption1", id: "opt-1", slug: "option-1" },
|
|
{ value: "suboption2", id: "opt-2", slug: "option-2" },
|
|
],
|
|
},
|
|
{ value: "option2", isGroup: false, contains: [] },
|
|
];
|
|
|
|
const result = getValueOfAttributeOption(input);
|
|
expect(result).toEqual(["option1", "suboption1", "suboption2", "option2"]);
|
|
});
|
|
|
|
it("should remove duplicate values", () => {
|
|
const input = [
|
|
{ value: "option1", isGroup: false, contains: [] },
|
|
{
|
|
value: "group1",
|
|
isGroup: true,
|
|
contains: [
|
|
{ value: "option1", id: "opt-1", slug: "option-1" },
|
|
{ value: "suboption2", id: "opt-2", slug: "option-2" },
|
|
],
|
|
},
|
|
];
|
|
|
|
const result = getValueOfAttributeOption(input);
|
|
expect(result).toEqual(["option1", "suboption2"]);
|
|
});
|
|
|
|
it("should handle empty input array", () => {
|
|
const input: {
|
|
value: string;
|
|
isGroup: boolean;
|
|
contains: { value: string; id: string; slug: string }[];
|
|
}[] = [];
|
|
|
|
const result = getValueOfAttributeOption(input);
|
|
expect(result).toEqual([]);
|
|
});
|
|
|
|
it("should still use contains if contains is empty and isGroup=true", () => {
|
|
const input = [{ value: "group1", isGroup: true, contains: [] }];
|
|
|
|
const result = getValueOfAttributeOption(input);
|
|
expect(result).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("resolveQueryValue", () => {
|
|
const mockFields = [
|
|
{ id: "location", type: RoutingFormFieldType.MULTI_SELECT, label: "Location", options: [] },
|
|
{ id: "city", type: RoutingFormFieldType.SINGLE_SELECT, label: "City", options: [] },
|
|
];
|
|
|
|
// Mock attributes for testing
|
|
const mockAttributes: Attribute[] = [
|
|
{
|
|
id: "attr-1",
|
|
name: "Location",
|
|
slug: "location",
|
|
type: AttributeType.MULTI_SELECT,
|
|
options: [
|
|
{ id: "opt-1", value: "New York", slug: "new-york" },
|
|
{ id: "opt-2", value: "London", slug: "london" },
|
|
],
|
|
},
|
|
{
|
|
id: "attr-2",
|
|
name: "City",
|
|
slug: "city",
|
|
type: AttributeType.SINGLE_SELECT,
|
|
options: [
|
|
{ id: "opt-3", value: "Mumbai", slug: "mumbai" },
|
|
{ id: "opt-4", value: "Delhi", slug: "delhi" },
|
|
],
|
|
},
|
|
];
|
|
|
|
describe("attribute option ID replacement", () => {
|
|
it("should replace attribute option IDs with lowercase values", () => {
|
|
const queryValue = createQueryValueWithRule({
|
|
ruleId: "rule1",
|
|
field: "attr-1",
|
|
operator: "select_equals",
|
|
value: ["opt-1"],
|
|
});
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: undefined,
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: expect.objectContaining({
|
|
field: "attr-1",
|
|
operator: "select_equals",
|
|
value: ["new york"], // Should be lowercase
|
|
}),
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should replace multiple attribute option IDs in the same value array", () => {
|
|
const queryValue = createQueryValueWithRule({
|
|
ruleId: "rule1",
|
|
field: "attr-1",
|
|
operator: "multiselect_some_in",
|
|
value: [["opt-1", "opt-2", "opt-3", "opt-4"]],
|
|
valueType: ["multiselect"],
|
|
});
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: undefined,
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: expect.objectContaining({
|
|
field: "attr-1",
|
|
operator: "multiselect_some_in",
|
|
value: [["new york", "london", "mumbai", "delhi"]],
|
|
}),
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should handle attribute option IDs in nested rules", () => {
|
|
const queryValue = createComplexQueryValue({
|
|
rules: [
|
|
{
|
|
ruleId: "rule1",
|
|
field: "attr-1",
|
|
operator: "select_equals",
|
|
value: ["opt-1"],
|
|
},
|
|
{
|
|
ruleId: "rule2",
|
|
field: "attr-2",
|
|
operator: "select_equals",
|
|
value: ["opt-3"],
|
|
},
|
|
],
|
|
});
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: undefined,
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: expect.objectContaining({
|
|
value: ["new york"],
|
|
}),
|
|
},
|
|
rule2: {
|
|
type: "rule",
|
|
properties: expect.objectContaining({
|
|
value: ["mumbai"],
|
|
}),
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should replace attribute option IDs before processing field templates", () => {
|
|
const queryValue = createQueryValueWithRule({
|
|
ruleId: "rule1",
|
|
field: "attr-1",
|
|
operator: "multiselect_some_in",
|
|
value: [["opt-1", "{field:location}"]],
|
|
valueType: ["multiselect"],
|
|
});
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: {
|
|
fields: mockFields,
|
|
response: {
|
|
location: { value: ["Paris"], label: "Paris" },
|
|
},
|
|
},
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: expect.objectContaining({
|
|
value: [["new york", "paris"]], // opt-1 replaced with "new york", field template with "paris"
|
|
}),
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should handle attribute option IDs with special characters", () => {
|
|
const specialAttributes: Attribute[] = [
|
|
{
|
|
id: "attr-special",
|
|
name: "Special",
|
|
slug: "special",
|
|
type: AttributeType.SINGLE_SELECT,
|
|
options: [
|
|
{ id: "opt-special-1", value: "São Paulo", slug: "sao-paulo" },
|
|
{ id: "opt-special-2", value: "Zürich", slug: "zurich" },
|
|
],
|
|
},
|
|
];
|
|
|
|
const queryValue = createQueryValueWithRule({
|
|
ruleId: "rule1",
|
|
field: "attr-special",
|
|
operator: "select_equals",
|
|
value: ["opt-special-1"],
|
|
});
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: undefined,
|
|
attributes: specialAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: expect.objectContaining({
|
|
value: ["são paulo"], // Should preserve special characters but be lowercase
|
|
}),
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should not replace IDs that don't match any attribute options", () => {
|
|
const queryValue = createQueryValueWithRule({
|
|
ruleId: "rule1",
|
|
field: "attr-1",
|
|
operator: "select_equals",
|
|
value: ["non-existent-id", "opt-1"],
|
|
});
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: undefined,
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: expect.objectContaining({
|
|
value: ["non-existent-id", "new york"], // Only opt-1 should be replaced
|
|
}),
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should handle empty attributes array", () => {
|
|
const queryValue = createQueryValueWithRule({
|
|
ruleId: "rule1",
|
|
field: "attr-1",
|
|
operator: "select_equals",
|
|
value: ["opt-1"],
|
|
});
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: undefined,
|
|
attributes: [], // No attributes
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: expect.objectContaining({
|
|
value: ["opt-1"], // Should remain unchanged
|
|
}),
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should handle attributes with no options", () => {
|
|
const attributesWithoutOptions: Attribute[] = [
|
|
{
|
|
id: "attr-no-options",
|
|
name: "No Options",
|
|
slug: "no-options",
|
|
type: AttributeType.TEXT,
|
|
options: [], // Empty options array
|
|
},
|
|
];
|
|
|
|
const queryValue = createQueryValueWithRule({
|
|
ruleId: "rule1",
|
|
field: "attr-no-options",
|
|
operator: "text_equals",
|
|
value: ["some-value"],
|
|
});
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: undefined,
|
|
attributes: attributesWithoutOptions,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: expect.objectContaining({
|
|
value: ["some-value"], // Should remain unchanged
|
|
}),
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should replace option IDs in complex nested group structures", () => {
|
|
const queryValue = createNestedGroupQueryValue({
|
|
groups: [
|
|
{
|
|
groupId: "group1",
|
|
rules: [
|
|
{
|
|
ruleId: "rule1",
|
|
field: "attr-1",
|
|
operator: "multiselect_some_in",
|
|
value: [["opt-1", "opt-2"]],
|
|
valueType: ["multiselect"],
|
|
},
|
|
{
|
|
ruleId: "rule2",
|
|
field: "attr-2",
|
|
operator: "select_equals",
|
|
value: ["opt-3"],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
});
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: undefined,
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
group1: {
|
|
type: "group",
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: expect.objectContaining({
|
|
value: [["new york", "london"]],
|
|
}),
|
|
},
|
|
rule2: {
|
|
type: "rule",
|
|
properties: expect.objectContaining({
|
|
value: ["mumbai"],
|
|
}),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should handle option IDs mixed with field templates in double-nested arrays", () => {
|
|
const queryValue = createQueryValueWithRule({
|
|
ruleId: "rule1",
|
|
field: "attr-1",
|
|
operator: "multiselect_some_in",
|
|
value: [["opt-1", "{field:city}", "opt-2", "{field:location}"]],
|
|
valueType: ["multiselect"],
|
|
});
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: {
|
|
fields: mockFields,
|
|
response: {
|
|
city: { value: "Tokyo", label: "Tokyo" },
|
|
location: { value: ["Berlin", "Paris"], label: "Berlin, Paris" },
|
|
},
|
|
},
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: expect.objectContaining({
|
|
value: [["new york", "tokyo", "london", "berlin", "paris"]],
|
|
}),
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
});
|
|
|
|
it("should handle simple field template replacement with single value", () => {
|
|
const queryValue = createQueryValueWithRule({
|
|
ruleId: "a8a89bba",
|
|
field: "city",
|
|
operator: "select_equals",
|
|
value: ["{field:city}"],
|
|
});
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: {
|
|
fields: mockFields,
|
|
response: {
|
|
city: { label: "Mumbai", value: "Mumbai" },
|
|
},
|
|
},
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
a8a89bba: {
|
|
type: "rule",
|
|
properties: expect.objectContaining({
|
|
field: "city",
|
|
operator: "select_equals",
|
|
value: ["mumbai"],
|
|
}),
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should handle field template in double-nested array for multiselect", () => {
|
|
const queryValue = createQueryValueWithRule({
|
|
ruleId: "rule1",
|
|
field: "location",
|
|
operator: "multiselect_some_in",
|
|
value: [["{field:location}"]],
|
|
valueType: ["multiselect"],
|
|
});
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: {
|
|
fields: mockFields,
|
|
response: {
|
|
location: { label: "Delhi", value: "Delhi" },
|
|
},
|
|
},
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: expect.objectContaining({
|
|
field: "location",
|
|
operator: "multiselect_some_in",
|
|
value: [["delhi"]],
|
|
}),
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should handle mixed array with field template and regular values", () => {
|
|
const queryValue = {
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "location",
|
|
operator: "multiselect_some_in",
|
|
value: [["{field:location}", "Chennai"]],
|
|
},
|
|
},
|
|
},
|
|
} as unknown as AttributesQueryValue;
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: {
|
|
fields: mockFields,
|
|
response: {
|
|
location: { value: "Delhi", label: "Delhi" }, // Single value, not array
|
|
},
|
|
},
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "location",
|
|
operator: "multiselect_some_in",
|
|
value: [["delhi", "Chennai"]], // Single value replaced
|
|
},
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should handle multiple field templates in the same array", () => {
|
|
const queryValue = {
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "multifield",
|
|
operator: "multiselect_some_in",
|
|
value: [["{field:location}", "{field:city}"]],
|
|
},
|
|
},
|
|
},
|
|
} as unknown as AttributesQueryValue;
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: {
|
|
fields: mockFields,
|
|
response: {
|
|
location: { value: ["Delhi"], label: "Delhi" },
|
|
city: { value: "Mumbai", label: "Mumbai" },
|
|
},
|
|
},
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "multifield",
|
|
operator: "multiselect_some_in",
|
|
value: [["delhi", "mumbai"]],
|
|
},
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should handle field template with no response value", () => {
|
|
const queryValue = {
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "location",
|
|
operator: "select_equals",
|
|
value: ["{field:location}"],
|
|
},
|
|
},
|
|
},
|
|
} as unknown as AttributesQueryValue;
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: {
|
|
fields: mockFields,
|
|
response: {}, // No response for location field
|
|
},
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "location",
|
|
operator: "select_equals",
|
|
value: ["{field:location}"], // Should remain unchanged
|
|
},
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should handle field template with unknown field", () => {
|
|
const queryValue = {
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "unknown",
|
|
operator: "select_equals",
|
|
value: ["{field:unknown}"],
|
|
},
|
|
},
|
|
},
|
|
} as unknown as AttributesQueryValue;
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: {
|
|
fields: mockFields,
|
|
response: {
|
|
unknown: { value: "test", label: "test" },
|
|
},
|
|
},
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "unknown",
|
|
operator: "select_equals",
|
|
value: ["{field:unknown}"], // Should remain unchanged
|
|
},
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should handle complex nested structure", () => {
|
|
const queryValue = createAttributesQueryValue({
|
|
id: "test",
|
|
type: "group",
|
|
children1: {
|
|
rule1: createQueryValueRule({
|
|
type: "rule",
|
|
properties: {
|
|
field: "attr1",
|
|
operator: "multiselect_some_in",
|
|
value: [["{field:location}", "Fixed-Value"]],
|
|
valueType: ["multiselect"],
|
|
},
|
|
}),
|
|
},
|
|
});
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: {
|
|
fields: mockFields,
|
|
response: {
|
|
location: { value: ["Delhi", "Haryana"], label: "Delhi, Haryana" },
|
|
},
|
|
},
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
id: "test",
|
|
type: "group",
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: expect.objectContaining({
|
|
field: "attr1",
|
|
value: [["delhi", "haryana", "Fixed-Value"]],
|
|
operator: "multiselect_some_in",
|
|
}),
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should handle empty array values", () => {
|
|
const queryValue = {
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "location",
|
|
operator: "multiselect_some_in",
|
|
value: [["{field:location}"]],
|
|
},
|
|
},
|
|
},
|
|
} as unknown as AttributesQueryValue;
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: {
|
|
fields: mockFields,
|
|
response: {
|
|
location: { value: [], label: "" }, // Empty array
|
|
},
|
|
},
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "location",
|
|
operator: "multiselect_some_in",
|
|
value: [[]], // Should be empty array
|
|
},
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should handle no dynamicFieldValueOperands", () => {
|
|
const queryValue = {
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "location",
|
|
value: ["{field:location}"],
|
|
},
|
|
},
|
|
},
|
|
} as unknown as AttributesQueryValue;
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: undefined,
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(queryValue); // Should return unchanged
|
|
});
|
|
|
|
it("should preserve case for non-template values in mixed arrays", () => {
|
|
const queryValue = {
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "location",
|
|
operator: "multiselect_some_in",
|
|
value: [["{field:location}", "Chennai", "MUMBAI"]],
|
|
},
|
|
},
|
|
},
|
|
} as unknown as AttributesQueryValue;
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: {
|
|
fields: mockFields,
|
|
response: {
|
|
location: { value: ["Delhi"], label: "Delhi" },
|
|
},
|
|
},
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "location",
|
|
operator: "multiselect_some_in",
|
|
value: [["delhi", "Chennai", "MUMBAI"]], // Only field template value is lowercased
|
|
},
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should handle deeply nested field templates", () => {
|
|
const queryValue = createNestedGroupQueryValue({
|
|
groups: [
|
|
{
|
|
groupId: "group1",
|
|
rules: [
|
|
{
|
|
ruleId: "rule1",
|
|
field: "location",
|
|
value: [["{field:location}", "Fixed1"]],
|
|
valueType: ["multiselect"],
|
|
},
|
|
{
|
|
ruleId: "rule2",
|
|
field: "city",
|
|
value: [["{field:city}", "Fixed2"]],
|
|
valueType: ["multiselect"],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
});
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: {
|
|
fields: mockFields,
|
|
response: {
|
|
location: { value: ["Delhi", "Mumbai"], label: "Delhi, Mumbai" },
|
|
city: { value: "Chennai", label: "Chennai" },
|
|
},
|
|
},
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
type: "group",
|
|
children1: {
|
|
group1: {
|
|
type: "group",
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: expect.objectContaining({
|
|
field: "location",
|
|
value: [["delhi", "mumbai", "Fixed1"]],
|
|
}),
|
|
},
|
|
rule2: {
|
|
type: "rule",
|
|
properties: expect.objectContaining({
|
|
field: "city",
|
|
value: [["chennai", "Fixed2"]],
|
|
}),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should handle field template with array containing special characters", () => {
|
|
const queryValue = {
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "location",
|
|
value: [["{field:location}"]],
|
|
},
|
|
},
|
|
},
|
|
} as unknown as AttributesQueryValue;
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: {
|
|
fields: mockFields,
|
|
response: {
|
|
location: { value: ["New York", "São Paulo", "Zürich"], label: "New York, São Paulo, Zürich" },
|
|
},
|
|
},
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "location",
|
|
value: [["new york", "são paulo", "zürich"]],
|
|
},
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should handle field template in arrays at different nesting levels", () => {
|
|
const queryValue = {
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "city",
|
|
value: ["{field:city}"], // Single nested
|
|
},
|
|
},
|
|
rule2: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "location",
|
|
value: [["{field:location}"]], // Double nested
|
|
},
|
|
},
|
|
rule3: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "city",
|
|
value: [[["{field:city}"]]], // Triple nested
|
|
},
|
|
},
|
|
},
|
|
} as unknown as AttributesQueryValue;
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: {
|
|
fields: mockFields,
|
|
response: {
|
|
location: { value: ["Delhi"], label: "Delhi" },
|
|
city: { value: "Mumbai", label: "Mumbai" },
|
|
},
|
|
},
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "city",
|
|
value: ["mumbai"],
|
|
},
|
|
},
|
|
rule2: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "location",
|
|
value: [["delhi"]],
|
|
},
|
|
},
|
|
rule3: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "city",
|
|
value: [[["mumbai"]]],
|
|
},
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should handle null and undefined values gracefully", () => {
|
|
const queryValue = {
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "mixed",
|
|
value: [["{field:location}", null, "{field:city}"]],
|
|
},
|
|
},
|
|
},
|
|
} as unknown as AttributesQueryValue;
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: {
|
|
fields: mockFields,
|
|
response: {
|
|
location: { value: ["Delhi"], label: "Delhi" },
|
|
city: { value: null as any, label: "" }, // null value
|
|
},
|
|
},
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "mixed",
|
|
value: [["delhi", null, "{field:city}"]], // city remains as template due to null value
|
|
},
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should handle numeric values from fields", () => {
|
|
const numericFields = [
|
|
{ id: "age", type: RoutingFormFieldType.NUMBER, label: "Age", options: [] },
|
|
{ id: "count", type: RoutingFormFieldType.NUMBER, label: "Count", options: [] },
|
|
];
|
|
|
|
const queryValue = {
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "numbers",
|
|
value: [["{field:age}", "{field:count}", 42]],
|
|
},
|
|
},
|
|
},
|
|
} as unknown as AttributesQueryValue;
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: {
|
|
fields: numericFields,
|
|
response: {
|
|
age: { value: 25, label: "25" },
|
|
count: { value: 100, label: "100" },
|
|
},
|
|
},
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "numbers",
|
|
value: [["25", "100", 42]],
|
|
},
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should handle very large arrays efficiently", () => {
|
|
const largeArray = new Array(100).fill("item");
|
|
const queryValue = {
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "location",
|
|
value: [["{field:location}", ...largeArray]],
|
|
},
|
|
},
|
|
},
|
|
} as unknown as AttributesQueryValue;
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: {
|
|
fields: mockFields,
|
|
response: {
|
|
location: { value: ["Delhi", "Mumbai"], label: "Delhi, Mumbai" },
|
|
},
|
|
},
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result.children1?.rule1.properties?.value[0]).toHaveLength(102); // 2 from field + 100 items
|
|
expect(result.children1?.rule1.properties?.value[0].slice(0, 2)).toEqual(["delhi", "mumbai"]);
|
|
});
|
|
|
|
it("should handle edge case with empty string field values", () => {
|
|
const queryValue = {
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "location",
|
|
value: [["{field:location}"]],
|
|
},
|
|
},
|
|
},
|
|
} as unknown as AttributesQueryValue;
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: {
|
|
fields: mockFields,
|
|
response: {
|
|
location: { value: ["", " ", "Delhi"], label: ", , Delhi" }, // Empty and whitespace strings
|
|
},
|
|
},
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "location",
|
|
value: [["", " ", "delhi"]],
|
|
},
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should handle field templates with hyphens and underscores in field names", () => {
|
|
const specialFields = [
|
|
{ id: "field-with-dashes", type: RoutingFormFieldType.TEXT, label: "Dashed Field", options: [] },
|
|
{
|
|
id: "field_with_underscores",
|
|
type: RoutingFormFieldType.TEXT,
|
|
label: "Underscored Field",
|
|
options: [],
|
|
},
|
|
];
|
|
|
|
const queryValue = {
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "special",
|
|
value: ["{field:field-with-dashes}", "{field:field_with_underscores}"],
|
|
},
|
|
},
|
|
},
|
|
} as unknown as AttributesQueryValue;
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: {
|
|
fields: specialFields,
|
|
response: {
|
|
"field-with-dashes": { value: "Value2", label: "Value2" },
|
|
field_with_underscores: { value: "Value3", label: "Value3" },
|
|
},
|
|
},
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "special",
|
|
value: ["value2", "value3"],
|
|
},
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should handle mixed field templates and fixed values in complex scenarios", () => {
|
|
const queryValue = {
|
|
children1: {
|
|
group1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "mixed1",
|
|
value: [["{field:location}", "Fixed1", "{field:city}"]],
|
|
},
|
|
},
|
|
group2: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "mixed2",
|
|
value: [["Fixed2", "{field:location}"]],
|
|
},
|
|
},
|
|
group3: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "mixed3",
|
|
value: [["{field:city}", "{field:location}", "Fixed3", "{field:city}"]],
|
|
},
|
|
},
|
|
},
|
|
} as unknown as AttributesQueryValue;
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: {
|
|
fields: mockFields,
|
|
response: {
|
|
location: { value: ["Delhi", "Mumbai"], label: "Delhi, Mumbai" },
|
|
city: { value: "Chennai", label: "Chennai" },
|
|
},
|
|
},
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
group1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "mixed1",
|
|
value: [["delhi", "mumbai", "Fixed1", "chennai"]],
|
|
},
|
|
},
|
|
group2: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "mixed2",
|
|
value: [["Fixed2", "delhi", "mumbai"]],
|
|
},
|
|
},
|
|
group3: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "mixed3",
|
|
value: [["chennai", "delhi", "mumbai", "Fixed3", "chennai"]],
|
|
},
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should handle field templates in different JSON value types", () => {
|
|
const queryValue = {
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "city",
|
|
value: "{field:city}",
|
|
},
|
|
},
|
|
rule2: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "city",
|
|
value: ["{field:city}"],
|
|
},
|
|
},
|
|
rule3: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "location",
|
|
value: [["{field:location}"]],
|
|
},
|
|
},
|
|
rule4: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "mixed",
|
|
customProp: "{field:city}",
|
|
nested: {
|
|
value: ["{field:location}"],
|
|
},
|
|
},
|
|
},
|
|
},
|
|
} as unknown as AttributesQueryValue;
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: {
|
|
fields: mockFields,
|
|
response: {
|
|
location: { value: ["Delhi", "Mumbai"], label: "Delhi, Mumbai" },
|
|
city: { value: "Chennai", label: "Chennai" },
|
|
},
|
|
},
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "city",
|
|
value: "chennai",
|
|
},
|
|
},
|
|
rule2: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "city",
|
|
value: ["chennai"],
|
|
},
|
|
},
|
|
rule3: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "location",
|
|
value: [["delhi", "mumbai"]],
|
|
},
|
|
},
|
|
rule4: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "mixed",
|
|
customProp: "chennai",
|
|
nested: {
|
|
value: ["delhi", "mumbai"],
|
|
},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should handle empty response object", () => {
|
|
const queryValue = {
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "location",
|
|
value: [["{field:location}", "Fixed"]],
|
|
},
|
|
},
|
|
},
|
|
} as unknown as AttributesQueryValue;
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: {
|
|
fields: mockFields,
|
|
response: {}, // Empty response
|
|
},
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "location",
|
|
value: [["{field:location}", "Fixed"]], // Template unchanged
|
|
},
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should handle response with empty field value object", () => {
|
|
const queryValue = {
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "location",
|
|
value: [["{field:location}"]],
|
|
},
|
|
},
|
|
},
|
|
} as unknown as AttributesQueryValue;
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: {
|
|
fields: mockFields,
|
|
response: {
|
|
location: {} as any, // Empty object, no value property
|
|
},
|
|
},
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "location",
|
|
value: [["{field:location}"]], // Template unchanged
|
|
},
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should handle multiple occurrences of the same field template", () => {
|
|
const queryValue = {
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "city",
|
|
value: ["{field:city}"],
|
|
},
|
|
},
|
|
rule2: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "city",
|
|
value: ["{field:city}"],
|
|
},
|
|
},
|
|
rule3: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "city",
|
|
value: [["{field:city}", "{field:city}"]],
|
|
},
|
|
},
|
|
},
|
|
} as unknown as AttributesQueryValue;
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: {
|
|
fields: mockFields,
|
|
response: {
|
|
city: { value: "Mumbai", label: "Mumbai" },
|
|
},
|
|
},
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "city",
|
|
value: ["mumbai"],
|
|
},
|
|
},
|
|
rule2: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "city",
|
|
value: ["mumbai"],
|
|
},
|
|
},
|
|
rule3: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "city",
|
|
value: [["mumbai", "mumbai"]],
|
|
},
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should handle invalid query structure gracefully", () => {
|
|
// Test with an invalid query structure
|
|
const invalidQueryValue = null as unknown as AttributesQueryValue;
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue: invalidQueryValue,
|
|
dynamicFieldValueOperands: {
|
|
fields: mockFields,
|
|
response: {
|
|
city: { value: "Mumbai", label: "Mumbai" },
|
|
},
|
|
},
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
expect(result).toBeNull();
|
|
});
|
|
|
|
it("should handle valid JSON that causes processing errors gracefully", () => {
|
|
const queryValue = {
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "nonexistent",
|
|
value: ["{field:nonexistent}"],
|
|
},
|
|
},
|
|
rule2: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "city",
|
|
customField: "{field:city}",
|
|
},
|
|
},
|
|
},
|
|
} as unknown as AttributesQueryValue;
|
|
|
|
const result = resolveQueryValue({
|
|
queryValue,
|
|
dynamicFieldValueOperands: {
|
|
fields: mockFields,
|
|
response: {
|
|
city: { value: "Mumbai", label: "Mumbai" },
|
|
},
|
|
},
|
|
attributes: mockAttributes,
|
|
});
|
|
|
|
// Non-existent field should remain as-is, existing field should be resolved
|
|
expect(result).toEqual(
|
|
expect.objectContaining({
|
|
children1: {
|
|
rule1: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "nonexistent",
|
|
value: ["{field:nonexistent}"],
|
|
},
|
|
},
|
|
rule2: {
|
|
type: "rule",
|
|
properties: {
|
|
field: "city",
|
|
customField: "mumbai",
|
|
},
|
|
},
|
|
},
|
|
})
|
|
);
|
|
});
|
|
});
|