|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import type { Entity } from '../generated/prisma/client.js'; |
| 3 | +import { AllPublicAccessTransformer, baseEntityTransformer } from './default.js'; |
| 4 | + |
| 5 | +describe('baseEntityTransformer', () => { |
| 6 | + it('should transform entity to standard entity shape', () => { |
| 7 | + const entity: Entity = { |
| 8 | + id: 1, |
| 9 | + rocrateId: 'http://example.com/entity/123', |
| 10 | + name: 'Test Entity', |
| 11 | + description: 'A test entity description', |
| 12 | + entityType: 'http://pcdm.org/models#Collection', |
| 13 | + memberOf: 'http://example.com/parent', |
| 14 | + rootCollection: 'http://example.com/root', |
| 15 | + metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/', |
| 16 | + contentLicenseId: 'https://creativecommons.org/licenses/by-sa/4.0/', |
| 17 | + createdAt: new Date('2025-01-01'), |
| 18 | + updatedAt: new Date('2025-01-02'), |
| 19 | + rocrate: { '@context': 'test' }, |
| 20 | + meta: { test: 'data' }, |
| 21 | + }; |
| 22 | + |
| 23 | + const result = baseEntityTransformer(entity); |
| 24 | + |
| 25 | + expect(result).toEqual({ |
| 26 | + id: 'http://example.com/entity/123', |
| 27 | + name: 'Test Entity', |
| 28 | + description: 'A test entity description', |
| 29 | + entityType: 'http://pcdm.org/models#Collection', |
| 30 | + memberOf: 'http://example.com/parent', |
| 31 | + rootCollection: 'http://example.com/root', |
| 32 | + metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/', |
| 33 | + contentLicenseId: 'https://creativecommons.org/licenses/by-sa/4.0/', |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should handle null memberOf and rootCollection', () => { |
| 38 | + const entity: Entity = { |
| 39 | + id: 1, |
| 40 | + rocrateId: 'http://example.com/collection', |
| 41 | + name: 'Top Collection', |
| 42 | + description: 'A top-level collection', |
| 43 | + entityType: 'http://pcdm.org/models#Collection', |
| 44 | + memberOf: null, |
| 45 | + rootCollection: null, |
| 46 | + metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/', |
| 47 | + contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/', |
| 48 | + createdAt: new Date(), |
| 49 | + updatedAt: new Date(), |
| 50 | + rocrate: {}, |
| 51 | + meta: null, |
| 52 | + }; |
| 53 | + |
| 54 | + const result = baseEntityTransformer(entity); |
| 55 | + |
| 56 | + expect(result.memberOf).toBeNull(); |
| 57 | + expect(result.rootCollection).toBeNull(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should exclude database-specific fields', () => { |
| 61 | + const entity: Entity = { |
| 62 | + id: 1, |
| 63 | + rocrateId: 'http://example.com/entity/456', |
| 64 | + name: 'Test', |
| 65 | + description: 'Test', |
| 66 | + entityType: 'http://pcdm.org/models#Object', |
| 67 | + memberOf: null, |
| 68 | + rootCollection: null, |
| 69 | + metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/', |
| 70 | + contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/', |
| 71 | + createdAt: new Date(), |
| 72 | + updatedAt: new Date(), |
| 73 | + rocrate: { test: 'value' }, |
| 74 | + meta: { storage: 'path' }, |
| 75 | + }; |
| 76 | + |
| 77 | + const result = baseEntityTransformer(entity); |
| 78 | + |
| 79 | + // Result should have 'id' (mapped from rocrateId), but not the numeric database id |
| 80 | + expect(result.id).toBe('http://example.com/entity/456'); |
| 81 | + expect(result).not.toHaveProperty('createdAt'); |
| 82 | + expect(result).not.toHaveProperty('updatedAt'); |
| 83 | + expect(result).not.toHaveProperty('rocrate'); |
| 84 | + expect(result).not.toHaveProperty('meta'); |
| 85 | + expect(Object.keys(result)).toEqual([ |
| 86 | + 'id', |
| 87 | + 'name', |
| 88 | + 'description', |
| 89 | + 'entityType', |
| 90 | + 'memberOf', |
| 91 | + 'rootCollection', |
| 92 | + 'metadataLicenseId', |
| 93 | + 'contentLicenseId', |
| 94 | + ]); |
| 95 | + }); |
| 96 | +}); |
| 97 | + |
| 98 | +describe('AllPublicAccessTransformer', () => { |
| 99 | + it('should grant full access to metadata and content', () => { |
| 100 | + const standardEntity = { |
| 101 | + id: 'http://example.com/entity/123', |
| 102 | + name: 'Test Entity', |
| 103 | + description: 'A test entity', |
| 104 | + entityType: 'http://pcdm.org/models#File', |
| 105 | + memberOf: 'http://example.com/parent', |
| 106 | + rootCollection: 'http://example.com/root', |
| 107 | + metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/', |
| 108 | + contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/', |
| 109 | + }; |
| 110 | + |
| 111 | + const result = AllPublicAccessTransformer(standardEntity); |
| 112 | + |
| 113 | + expect(result).toEqual({ |
| 114 | + ...standardEntity, |
| 115 | + access: { |
| 116 | + metadata: true, |
| 117 | + content: true, |
| 118 | + }, |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should preserve all standard entity fields', () => { |
| 123 | + const standardEntity = { |
| 124 | + id: 'http://example.com/entity/789', |
| 125 | + name: 'Another Entity', |
| 126 | + description: 'Another description', |
| 127 | + entityType: 'http://schema.org/Person', |
| 128 | + memberOf: null, |
| 129 | + rootCollection: null, |
| 130 | + metadataLicenseId: 'https://creativecommons.org/publicdomain/zero/1.0/', |
| 131 | + contentLicenseId: 'https://creativecommons.org/publicdomain/zero/1.0/', |
| 132 | + }; |
| 133 | + |
| 134 | + const result = AllPublicAccessTransformer(standardEntity); |
| 135 | + |
| 136 | + expect(result.id).toBe(standardEntity.id); |
| 137 | + expect(result.name).toBe(standardEntity.name); |
| 138 | + expect(result.description).toBe(standardEntity.description); |
| 139 | + expect(result.entityType).toBe(standardEntity.entityType); |
| 140 | + expect(result.memberOf).toBe(standardEntity.memberOf); |
| 141 | + expect(result.rootCollection).toBe(standardEntity.rootCollection); |
| 142 | + expect(result.metadataLicenseId).toBe(standardEntity.metadataLicenseId); |
| 143 | + expect(result.contentLicenseId).toBe(standardEntity.contentLicenseId); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should not add contentAuthorizationUrl for public access', () => { |
| 147 | + const standardEntity = { |
| 148 | + id: 'http://example.com/entity/public', |
| 149 | + name: 'Public Entity', |
| 150 | + description: 'Fully public', |
| 151 | + entityType: 'http://pcdm.org/models#Collection', |
| 152 | + memberOf: null, |
| 153 | + rootCollection: null, |
| 154 | + metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/', |
| 155 | + contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/', |
| 156 | + }; |
| 157 | + |
| 158 | + const result = AllPublicAccessTransformer(standardEntity); |
| 159 | + |
| 160 | + expect(result.access.contentAuthorizationUrl).toBeUndefined(); |
| 161 | + }); |
| 162 | +}); |
0 commit comments