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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
import { ContextStorePageType, type CommandMenuContextApi } from '@/types';
import { evaluateConditionalAvailabilityExpression } from '../evaluateConditionalAvailabilityExpression';

const buildContext = (
  overrides: Partial<CommandMenuContextApi> = {},
): CommandMenuContextApi => ({
  pageType: ContextStorePageType.Index,
  isInSidePanel: false,
  isDashboardPageLayoutInEditMode: false,
  isLayoutCustomizationModeEnabled: false,
  favoriteRecordIds: [],
  isSelectAll: false,
  hasAnySoftDeleteFilterOnView: false,
  numberOfSelectedRecords: 0,
  objectPermissions: {
    objectMetadataId: '',
    canReadObjectRecords: true,
    canUpdateObjectRecords: true,
    canSoftDeleteObjectRecords: true,
    canDestroyObjectRecords: false,
    restrictedFields: {},
    rowLevelPermissionPredicates: [],
    rowLevelPermissionPredicateGroups: [],
  },
  selectedRecords: [],
  featureFlags: {},
  permissionFlags: {},
  targetObjectReadPermissions: {},
  targetObjectWritePermissions: {},
  canImpersonate: false,
  canAccessFullAdminPanel: false,
  objectMetadataItem: {},
  objectMetadataLabel: '',
  ...overrides,
});

describe('evaluateConditionalAvailabilityExpression', () => {
  describe('arrayLength for favoriteRecordIds', () => {
    it('should evaluate arrayLength(favoriteRecordIds) < numberOfSelectedRecords when some selected are not favorites', () => {
      const expression =
        'arrayLength(favoriteRecordIds) < numberOfSelectedRecords and noneDefined(selectedRecords, "deletedAt") and not hasAnySoftDeleteFilterOnView';

      const context = buildContext({
        favoriteRecordIds: ['id-1'],
        numberOfSelectedRecords: 2,
        selectedRecords: [
          { id: 'id-1', createdAt: '', updatedAt: '', deletedAt: null },
          { id: 'id-2', createdAt: '', updatedAt: '', deletedAt: null },
        ],
        hasAnySoftDeleteFilterOnView: false,
      });

      expect(
        evaluateConditionalAvailabilityExpression(expression, context),
      ).toBe(true);
    });

    it('should deny when all selected are already favorites', () => {
      const expression =
        'arrayLength(favoriteRecordIds) < numberOfSelectedRecords and noneDefined(selectedRecords, "deletedAt") and not hasAnySoftDeleteFilterOnView';

      const context = buildContext({
        favoriteRecordIds: ['id-1', 'id-2'],
        numberOfSelectedRecords: 2,
        selectedRecords: [
          { id: 'id-1', createdAt: '', updatedAt: '', deletedAt: null },
          { id: 'id-2', createdAt: '', updatedAt: '', deletedAt: null },
        ],
        hasAnySoftDeleteFilterOnView: false,
      });

      expect(
        evaluateConditionalAvailabilityExpression(expression, context),
      ).toBe(false);
    });

    it('should evaluate arrayLength(favoriteRecordIds) == numberOfSelectedRecords when all selected are favorites', () => {
      const expression =
        'arrayLength(favoriteRecordIds) == numberOfSelectedRecords and noneDefined(selectedRecords, "deletedAt") and not hasAnySoftDeleteFilterOnView';

      const context = buildContext({
        favoriteRecordIds: ['id-1', 'id-2'],
        numberOfSelectedRecords: 2,
        selectedRecords: [
          { id: 'id-1', createdAt: '', updatedAt: '', deletedAt: null },
          { id: 'id-2', createdAt: '', updatedAt: '', deletedAt: null },
        ],
        hasAnySoftDeleteFilterOnView: false,
      });

      expect(
        evaluateConditionalAvailabilityExpression(expression, context),
      ).toBe(true);
    });

    it('should reject favoriteRecordIds.length (expr-eval-fork parses "length" as unary op)', () => {
      const expression = 'favoriteRecordIds.length < numberOfSelectedRecords';
      const context = buildContext({
        favoriteRecordIds: ['id-1'],
        numberOfSelectedRecords: 2,
      });

      expect(
        evaluateConditionalAvailabilityExpression(expression, context),
      ).toBe(false);
    });
  });

  describe('includesSome', () => {
    it('should return true when some records include the value in the array prop', () => {
      const expression = 'includesSome(selectedRecords, "statuses", "ACTIVE")';

      const context = buildContext({
        selectedRecords: [
          { id: 'id-1', statuses: ['ACTIVE'] },
          { id: 'id-2', statuses: ['DRAFT'] },
        ],
      });

      expect(
        evaluateConditionalAvailabilityExpression(expression, context),
      ).toBe(true);
    });

    it('should return false when no record includes the value in the array prop', () => {
      const expression = 'includesSome(selectedRecords, "statuses", "ACTIVE")';

      const context = buildContext({
        selectedRecords: [
          { id: 'id-1', statuses: ['DRAFT'] },
          { id: 'id-2', statuses: ['DRAFT'] },
        ],
      });

      expect(
        evaluateConditionalAvailabilityExpression(expression, context),
      ).toBe(false);
    });
  });

  describe('includesNone', () => {
    it('should return true when no record includes the value in the array prop', () => {
      const expression = 'includesNone(selectedRecords, "statuses", "ACTIVE")';

      const context = buildContext({
        selectedRecords: [
          { id: 'id-1', statuses: ['DRAFT'] },
          { id: 'id-2', statuses: ['DRAFT'] },
        ],
      });

      expect(
        evaluateConditionalAvailabilityExpression(expression, context),
      ).toBe(true);
    });

    it('should return false when any record includes the value in the array prop', () => {
      const expression = 'includesNone(selectedRecords, "statuses", "ACTIVE")';

      const context = buildContext({
        selectedRecords: [
          { id: 'id-1', statuses: ['ACTIVE'] },
          { id: 'id-2', statuses: ['DRAFT'] },
        ],
      });

      expect(
        evaluateConditionalAvailabilityExpression(expression, context),
      ).toBe(false);
    });

    it('should return false when all records include the value in the array prop', () => {
      const expression = 'includesNone(selectedRecords, "statuses", "ACTIVE")';

      const context = buildContext({
        selectedRecords: [
          { id: 'id-1', statuses: ['ACTIVE', 'DRAFT'] },
          { id: 'id-2', statuses: ['ACTIVE'] },
        ],
      });

      expect(
        evaluateConditionalAvailabilityExpression(expression, context),
      ).toBe(false);
    });
  });

  describe('empty array guard against vacuous truth', () => {
    it('should return false for every() on empty selectedRecords', () => {
      const context = buildContext({ selectedRecords: [] });

      expect(
        evaluateConditionalAvailabilityExpression(
          'every(selectedRecords, "active")',
          context,
        ),
      ).toBe(false);
    });

    it('should return false for everyDefined() on empty selectedRecords', () => {
      const context = buildContext({ selectedRecords: [] });

      expect(
        evaluateConditionalAvailabilityExpression(
          'everyDefined(selectedRecords, "name")',
          context,
        ),
      ).toBe(false);
    });

    it('should return false for none() on empty selectedRecords', () => {
      const context = buildContext({ selectedRecords: [] });

      expect(
        evaluateConditionalAvailabilityExpression(
          'none(selectedRecords, "deletedAt")',
          context,
        ),
      ).toBe(false);
    });

    it('should return false for noneDefined() on empty selectedRecords', () => {
      const context = buildContext({ selectedRecords: [] });

      expect(
        evaluateConditionalAvailabilityExpression(
          'noneDefined(selectedRecords, "deletedAt")',
          context,
        ),
      ).toBe(false);
    });

    it('should return false for everyEquals() on empty selectedRecords', () => {
      const context = buildContext({ selectedRecords: [] });

      expect(
        evaluateConditionalAvailabilityExpression(
          'everyEquals(selectedRecords, "status", "DRAFT")',
          context,
        ),
      ).toBe(false);
    });

    it('should return false for noneEquals() on empty selectedRecords', () => {
      const context = buildContext({ selectedRecords: [] });

      expect(
        evaluateConditionalAvailabilityExpression(
          'noneEquals(selectedRecords, "status", "ACTIVE")',
          context,
        ),
      ).toBe(false);
    });

    it('should return false for includesEvery() on empty selectedRecords', () => {
      const context = buildContext({ selectedRecords: [] });

      expect(
        evaluateConditionalAvailabilityExpression(
          'includesEvery(selectedRecords, "statuses", "ACTIVE")',
          context,
        ),
      ).toBe(false);
    });

    it('should return false for includesNone() on empty selectedRecords', () => {
      const context = buildContext({ selectedRecords: [] });

      expect(
        evaluateConditionalAvailabilityExpression(
          'includesNone(selectedRecords, "statuses", "ACTIVE")',
          context,
        ),
      ).toBe(false);
    });

    it('should return false for some() on empty selectedRecords', () => {
      const context = buildContext({ selectedRecords: [] });

      expect(
        evaluateConditionalAvailabilityExpression(
          'some(selectedRecords, "active")',
          context,
        ),
      ).toBe(false);
    });

    it('should return false for someDefined() on empty selectedRecords', () => {
      const context = buildContext({ selectedRecords: [] });

      expect(
        evaluateConditionalAvailabilityExpression(
          'someDefined(selectedRecords, "name")',
          context,
        ),
      ).toBe(false);
    });

    it('should return false for includesSome() on empty selectedRecords', () => {
      const context = buildContext({ selectedRecords: [] });

      expect(
        evaluateConditionalAvailabilityExpression(
          'includesSome(selectedRecords, "statuses", "ACTIVE")',
          context,
        ),
      ).toBe(false);
    });
  });

  describe('isSelectAll bypasses selectedRecords array checks', () => {
    it('should return true for "isSelectAll or noneDefined(...)" when isSelectAll is true and selectedRecords is empty', () => {
      const context = buildContext({
        isSelectAll: true,
        selectedRecords: [],
      });

      expect(
        evaluateConditionalAvailabilityExpression(
          'isSelectAll or noneDefined(selectedRecords, "deletedAt")',
          context,
        ),
      ).toBe(true);
    });

    it('should return true for "isSelectAll or everyDefined(...)" when isSelectAll is true and selectedRecords is empty', () => {
      const context = buildContext({
        isSelectAll: true,
        selectedRecords: [],
      });

      expect(
        evaluateConditionalAvailabilityExpression(
          'isSelectAll or everyDefined(selectedRecords, "deletedAt")',
          context,
        ),
      ).toBe(true);
    });

    it('should evaluate the full deleteRecords expression to true in select-all mode', () => {
      const expression =
        'numberOfSelectedRecords >= 1 and not hasAnySoftDeleteFilterOnView and objectPermissions.canSoftDeleteObjectRecords and (isSelectAll or noneDefined(selectedRecords, "deletedAt"))';

      const context = buildContext({
        isSelectAll: true,
        selectedRecords: [],
        numberOfSelectedRecords: 50,
        hasAnySoftDeleteFilterOnView: false,
        objectPermissions: {
          objectMetadataId: '',
          canReadObjectRecords: true,
          canUpdateObjectRecords: true,
          canSoftDeleteObjectRecords: true,
          canDestroyObjectRecords: false,
          restrictedFields: {},
          rowLevelPermissionPredicates: [],
          rowLevelPermissionPredicateGroups: [],
        },
      });

      expect(
        evaluateConditionalAvailabilityExpression(expression, context),
      ).toBe(true);
    });

    it('should still respect noneDefined when isSelectAll is false and records are present', () => {
      const expression =
        'isSelectAll or noneDefined(selectedRecords, "deletedAt")';

      const contextWithDeletedRecord = buildContext({
        isSelectAll: false,
        selectedRecords: [
          { id: 'id-1', createdAt: '', updatedAt: '', deletedAt: '2024-01-01' },
        ],
      });

      expect(
        evaluateConditionalAvailabilityExpression(
          expression,
          contextWithDeletedRecord,
        ),
      ).toBe(false);
    });
  });

  describe('activateWorkflow expression regression', () => {
    const activateWorkflowExpression =
      'everyDefined(selectedRecords, "currentVersion.trigger") and everyDefined(selectedRecords, "currentVersion.steps") and every(selectedRecords, "currentVersion.steps.length") and (everyEquals(selectedRecords, "currentVersion.status", "DRAFT") or includesNone(selectedRecords, "statuses", "ACTIVE")) and noneDefined(selectedRecords, "deletedAt")';

    it('should not show Activate for an already-active workflow', () => {
      const context = buildContext({
        selectedRecords: [
          {
            id: 'wf-1',
            deletedAt: null,
            statuses: ['ACTIVE'],
            currentVersion: {
              status: 'ACTIVE',
              trigger: { type: 'MANUAL' },
              steps: [{ id: 'step-1' }],
            },
          },
        ],
      });

      expect(
        evaluateConditionalAvailabilityExpression(
          activateWorkflowExpression,
          context,
        ),
      ).toBe(false);
    });

    it('should show Activate for a draft workflow with no active version', () => {
      const context = buildContext({
        selectedRecords: [
          {
            id: 'wf-1',
            deletedAt: null,
            statuses: ['DRAFT'],
            currentVersion: {
              status: 'DRAFT',
              trigger: { type: 'MANUAL' },
              steps: [{ id: 'step-1' }],
            },
          },
        ],
      });

      expect(
        evaluateConditionalAvailabilityExpression(
          activateWorkflowExpression,
          context,
        ),
      ).toBe(true);
    });

    it('should show Activate for a draft version when another version is active', () => {
      const context = buildContext({
        selectedRecords: [
          {
            id: 'wf-1',
            deletedAt: null,
            statuses: ['ACTIVE', 'DRAFT'],
            currentVersion: {
              status: 'DRAFT',
              trigger: { type: 'MANUAL' },
              steps: [{ id: 'step-1' }],
            },
          },
        ],
      });

      expect(
        evaluateConditionalAvailabilityExpression(
          activateWorkflowExpression,
          context,
        ),
      ).toBe(true);
    });
  });

  describe('permissionFlags gating', () => {
    it('should hide exportRecords when EXPORT_CSV permission flag is missing', () => {
      const context = buildContext({ permissionFlags: {} });

      expect(
        evaluateConditionalAvailabilityExpression(
          'permissionFlags.EXPORT_CSV',
          context,
        ),
      ).toBe(false);
    });

    it('should show exportRecords when EXPORT_CSV permission flag is present', () => {
      const context = buildContext({
        permissionFlags: { EXPORT_CSV: true },
      });

      expect(
        evaluateConditionalAvailabilityExpression(
          'permissionFlags.EXPORT_CSV',
          context,
        ),
      ).toBe(true);
    });

    it('should hide importRecords when IMPORT_CSV permission flag is missing even if soft-delete filter is off', () => {
      const context = buildContext({
        hasAnySoftDeleteFilterOnView: false,
        permissionFlags: {},
      });

      expect(
        evaluateConditionalAvailabilityExpression(
          'not hasAnySoftDeleteFilterOnView and permissionFlags.IMPORT_CSV',
          context,
        ),
      ).toBe(false);
    });

    it('should show importRecords when IMPORT_CSV permission flag is present and soft-delete filter is off', () => {
      const context = buildContext({
        hasAnySoftDeleteFilterOnView: false,
        permissionFlags: { IMPORT_CSV: true },
      });

      expect(
        evaluateConditionalAvailabilityExpression(
          'not hasAnySoftDeleteFilterOnView and permissionFlags.IMPORT_CSV',
          context,
        ),
      ).toBe(true);
    });
  });

  describe('pageType gating for index-only commands', () => {
    it('should hide createNewView on a record page', () => {
      const context = buildContext({
        pageType: ContextStorePageType.Record,
        hasAnySoftDeleteFilterOnView: false,
      });

      expect(
        evaluateConditionalAvailabilityExpression(
          'pageType == "INDEX_PAGE" and not hasAnySoftDeleteFilterOnView',
          context,
        ),
      ).toBe(false);
    });

    it('should show createNewView on the index page', () => {
      const context = buildContext({
        pageType: ContextStorePageType.Index,
        hasAnySoftDeleteFilterOnView: false,
      });

      expect(
        evaluateConditionalAvailabilityExpression(
          'pageType == "INDEX_PAGE" and not hasAnySoftDeleteFilterOnView',
          context,
        ),
      ).toBe(true);
    });

    it('should hide importRecords on a record page even with IMPORT_CSV permission', () => {
      const context = buildContext({
        pageType: ContextStorePageType.Record,
        hasAnySoftDeleteFilterOnView: false,
        permissionFlags: { IMPORT_CSV: true },
      });

      expect(
        evaluateConditionalAvailabilityExpression(
          'pageType == "INDEX_PAGE" and not hasAnySoftDeleteFilterOnView and permissionFlags.IMPORT_CSV',
          context,
        ),
      ).toBe(false);
    });

    it('should hide exportView on a record page even with EXPORT_CSV permission', () => {
      const context = buildContext({
        pageType: ContextStorePageType.Record,
        permissionFlags: { EXPORT_CSV: true },
      });

      expect(
        evaluateConditionalAvailabilityExpression(
          'pageType == "INDEX_PAGE" and permissionFlags.EXPORT_CSV',
          context,
        ),
      ).toBe(false);
    });

    it('should hide seeDeletedRecords on a record page', () => {
      const context = buildContext({
        pageType: ContextStorePageType.Record,
        hasAnySoftDeleteFilterOnView: false,
      });

      expect(
        evaluateConditionalAvailabilityExpression(
          'pageType == "INDEX_PAGE" and not hasAnySoftDeleteFilterOnView',
          context,
        ),
      ).toBe(false);
    });

    it('should hide hideDeletedRecords on a record page', () => {
      const context = buildContext({
        pageType: ContextStorePageType.Record,
        hasAnySoftDeleteFilterOnView: true,
      });

      expect(
        evaluateConditionalAvailabilityExpression(
          'pageType == "INDEX_PAGE" and hasAnySoftDeleteFilterOnView',
          context,
        ),
      ).toBe(false);
    });
  });
});