Fix sharing.test.ts for discriminated union schema#130
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
f475c88
into
copilot/release-new-version-please-work
There was a problem hiding this comment.
Pull request overview
This PR updates the test suite in sharing.test.ts to align with schema changes that introduced a discriminated union between CriteriaSharingRuleSchema and OwnerSharingRuleSchema. The changes ensure all 24 tests pass with the new schema structure.
Changes:
- Updated
sharedWithfrom simple string to structured object withtypeandvaluefields - Renamed
criteriafield toconditionfor criteria-based rules - Added
ownedByfield for owner-based rules - Removed
'manual'and'guest'as valid rule types (now only'owner'and'criteria') - Added
'full'as a valid access level alongside'read'and'edit'
| }); | ||
|
|
||
| it('should accept manual sharing rule', () => { | ||
| it('should accept user-specific sharing rule', () => { |
There was a problem hiding this comment.
The test name "should accept user-specific sharing rule" is misleading. This test is actually validating a criteria-based sharing rule that happens to share with a user recipient. The test name suggests it's testing a different rule type, but 'user-specific' is not a rule type in the schema - it's just using 'user' as a recipient type. Consider renaming to something like "should accept criteria rule with user recipient" to more accurately describe what's being tested.
| it('should accept user-specific sharing rule', () => { | |
| it('should accept criteria rule with user recipient', () => { |
| expect(rule.sharedWith.type).toBe('user'); | ||
| }); | ||
|
|
||
| it('should accept guest sharing rule', () => { |
There was a problem hiding this comment.
The test name "should accept guest sharing rule" is misleading. After the schema changes, 'guest' is no longer a sharing rule type - it's now just a recipient type within the ShareRecipientType enum. This test is actually validating a criteria-based sharing rule that shares with guest recipients. Consider renaming to something like "should accept criteria rule with guest recipient" to accurately reflect the current schema structure.
| it('should accept guest sharing rule', () => { | |
| it('should accept criteria rule with guest recipient', () => { |
| it('should reject sharing rule without required fields', () => { | ||
| expect(() => SharingRuleSchema.parse({ | ||
| object: 'account', | ||
| sharedWith: 'group_id', | ||
| type: 'criteria', | ||
| condition: 'status = "Active"', | ||
| sharedWith: { type: 'group', value: 'group_id' }, | ||
| })).toThrow(); | ||
|
|
||
| expect(() => SharingRuleSchema.parse({ | ||
| name: 'test_rule', | ||
| sharedWith: 'group_id', | ||
| type: 'criteria', | ||
| condition: 'status = "Active"', | ||
| sharedWith: { type: 'group', value: 'group_id' }, | ||
| })).toThrow(); | ||
|
|
||
| expect(() => SharingRuleSchema.parse({ | ||
| name: 'test_rule', | ||
| object: 'account', | ||
| type: 'criteria', | ||
| condition: 'status = "Active"', | ||
| })).toThrow(); | ||
| }); |
There was a problem hiding this comment.
The test coverage for required fields should include validating that discriminated union requirements are enforced. Specifically, missing tests for:
- Owner-based rule without
ownedByfield should be rejected - Criteria-based rule without
conditionfield should be rejected
These are critical validations for the discriminated union schema. Consider adding test cases like:
- Testing that
{ name: 'test', object: 'account', type: 'owner', sharedWith: {...} }(missingownedBy) throws an error - Testing that
{ name: 'test', object: 'account', type: 'criteria', sharedWith: {...} }(missingcondition) throws an error
| it('should accept user-specific sharing rule', () => { | ||
| const rule = SharingRuleSchema.parse({ | ||
| name: 'manual_share', | ||
| name: 'user_specific_share', | ||
| object: 'opportunity', | ||
| type: 'manual', | ||
| type: 'criteria', | ||
| condition: 'stage != "Closed Won"', | ||
| accessLevel: 'edit', | ||
| sharedWith: 'user_john_doe', | ||
| sharedWith: { type: 'user', value: 'john_doe' }, | ||
| }); | ||
|
|
||
| expect(rule.type).toBe('manual'); | ||
| expect(rule.sharedWith.type).toBe('user'); | ||
| }); | ||
|
|
||
| it('should accept guest sharing rule', () => { | ||
| const rule = SharingRuleSchema.parse({ | ||
| name: 'public_access', | ||
| object: 'knowledge_article', | ||
| type: 'guest', | ||
| type: 'criteria', | ||
| condition: 'published = true', | ||
| accessLevel: 'read', | ||
| sharedWith: 'guest_users', | ||
| sharedWith: { type: 'guest', value: 'guest_users' }, | ||
| }); | ||
|
|
||
| expect(rule.type).toBe('guest'); | ||
| expect(rule.sharedWith.type).toBe('guest'); | ||
| }); |
There was a problem hiding this comment.
The tests don't cover the 'role_and_subordinates' recipient type, which is defined in ShareRecipientType enum (sharing.zod.ts:41). Consider adding a test case to validate that sharing rules can use this recipient type, e.g., sharedWith: { type: 'role_and_subordinates', value: 'sales_manager' }
Tests in
sharing.test.tswere failing due to schema changes that introduced a discriminated union betweenCriteriaSharingRuleSchemaandOwnerSharingRuleSchema.Changes
Updated test data structure:
sharedWith: string →{ type: ShareRecipientType, value: string }criteriafield renamed tocondition(criteria-based rules)ownedByfield (owner-based rules)'manual'and'guest'as valid rule types'full'as valid access levelBefore:
After:
All 24 sharing tests now pass.
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.