|
| 1 | +import { Kysely } from 'kysely'; |
| 2 | +import { JobStatus } from 'src/enum'; |
| 3 | +import { AccessRepository } from 'src/repositories/access.repository'; |
| 4 | +import { LoggingRepository } from 'src/repositories/logging.repository'; |
| 5 | +import { TagRepository } from 'src/repositories/tag.repository'; |
| 6 | +import { DB } from 'src/schema'; |
| 7 | +import { TagService } from 'src/services/tag.service'; |
| 8 | +import { upsertTags } from 'src/utils/tag'; |
| 9 | +import { newMediumService } from 'test/medium.factory'; |
| 10 | +import { getKyselyDB } from 'test/utils'; |
| 11 | + |
| 12 | +let defaultDatabase: Kysely<DB>; |
| 13 | + |
| 14 | +const setup = (db?: Kysely<DB>) => { |
| 15 | + return newMediumService(TagService, { |
| 16 | + database: db || defaultDatabase, |
| 17 | + real: [TagRepository, AccessRepository], |
| 18 | + mock: [LoggingRepository], |
| 19 | + }); |
| 20 | +}; |
| 21 | + |
| 22 | +beforeAll(async () => { |
| 23 | + defaultDatabase = await getKyselyDB(); |
| 24 | +}); |
| 25 | + |
| 26 | +describe(TagService.name, () => { |
| 27 | + describe('deleteEmptyTags', () => { |
| 28 | + it('single tag exists, not connected to any assets, and is deleted', async () => { |
| 29 | + const { sut, ctx } = setup(); |
| 30 | + const { user } = await ctx.newUser(); |
| 31 | + const tagRepo = ctx.get(TagRepository); |
| 32 | + const [tag] = await upsertTags(tagRepo, { userId: user.id, tags: ['tag-1'] }); |
| 33 | + |
| 34 | + await expect(tagRepo.getByValue(user.id, 'tag-1')).resolves.toEqual(expect.objectContaining({ id: tag.id })); |
| 35 | + await expect(sut.handleTagCleanup()).resolves.toBe(JobStatus.Success); |
| 36 | + await expect(tagRepo.getByValue(user.id, 'tag-1')).resolves.toBeUndefined(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('single tag exists, connected to one asset, and is not deleted', async () => { |
| 40 | + const { sut, ctx } = setup(); |
| 41 | + const { user } = await ctx.newUser(); |
| 42 | + const { asset } = await ctx.newAsset({ ownerId: user.id }); |
| 43 | + const tagRepo = ctx.get(TagRepository); |
| 44 | + const [tag] = await upsertTags(tagRepo, { userId: user.id, tags: ['tag-1'] }); |
| 45 | + |
| 46 | + await ctx.newTagAsset({ tagIds: [tag.id], assetIds: [asset.id] }); |
| 47 | + |
| 48 | + await expect(tagRepo.getByValue(user.id, 'tag-1')).resolves.toEqual(expect.objectContaining({ id: tag.id })); |
| 49 | + await expect(sut.handleTagCleanup()).resolves.toBe(JobStatus.Success); |
| 50 | + await expect(tagRepo.getByValue(user.id, 'tag-1')).resolves.toEqual(expect.objectContaining({ id: tag.id })); |
| 51 | + }); |
| 52 | + |
| 53 | + it('hierarchical tag exists, and the parent is connected to an asset, and the child is deleted', async () => { |
| 54 | + const { sut, ctx } = setup(); |
| 55 | + const { user } = await ctx.newUser(); |
| 56 | + const { asset } = await ctx.newAsset({ ownerId: user.id }); |
| 57 | + const tagRepo = ctx.get(TagRepository); |
| 58 | + const [parentTag, childTag] = await upsertTags(tagRepo, { userId: user.id, tags: ['parent', 'parent/child'] }); |
| 59 | + |
| 60 | + await ctx.newTagAsset({ tagIds: [parentTag.id], assetIds: [asset.id] }); |
| 61 | + |
| 62 | + await expect(tagRepo.getByValue(user.id, 'parent')).resolves.toEqual( |
| 63 | + expect.objectContaining({ id: parentTag.id }), |
| 64 | + ); |
| 65 | + await expect(tagRepo.getByValue(user.id, 'parent/child')).resolves.toEqual( |
| 66 | + expect.objectContaining({ id: childTag.id }), |
| 67 | + ); |
| 68 | + await expect(sut.handleTagCleanup()).resolves.toBe(JobStatus.Success); |
| 69 | + await expect(tagRepo.getByValue(user.id, 'parent')).resolves.toEqual( |
| 70 | + expect.objectContaining({ id: parentTag.id }), |
| 71 | + ); |
| 72 | + await expect(tagRepo.getByValue(user.id, 'parent/child')).resolves.toBeUndefined(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('hierarchical tag exists, and only the child is connected to an asset, and nothing is deleted', async () => { |
| 76 | + const { sut, ctx } = setup(); |
| 77 | + const { user } = await ctx.newUser(); |
| 78 | + const { asset } = await ctx.newAsset({ ownerId: user.id }); |
| 79 | + const tagRepo = ctx.get(TagRepository); |
| 80 | + const [parentTag, childTag] = await upsertTags(tagRepo, { userId: user.id, tags: ['parent', 'parent/child'] }); |
| 81 | + |
| 82 | + await ctx.newTagAsset({ tagIds: [childTag.id], assetIds: [asset.id] }); |
| 83 | + |
| 84 | + await expect(tagRepo.getByValue(user.id, 'parent')).resolves.toEqual( |
| 85 | + expect.objectContaining({ id: parentTag.id }), |
| 86 | + ); |
| 87 | + await expect(tagRepo.getByValue(user.id, 'parent/child')).resolves.toEqual( |
| 88 | + expect.objectContaining({ id: childTag.id }), |
| 89 | + ); |
| 90 | + await expect(sut.handleTagCleanup()).resolves.toBe(JobStatus.Success); |
| 91 | + await expect(tagRepo.getByValue(user.id, 'parent')).resolves.toEqual( |
| 92 | + expect.objectContaining({ id: parentTag.id }), |
| 93 | + ); |
| 94 | + await expect(tagRepo.getByValue(user.id, 'parent/child')).resolves.toEqual( |
| 95 | + expect.objectContaining({ id: childTag.id }), |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + it('hierarchical tag exists, and neither parent nor child is connected to an asset, and both are deleted', async () => { |
| 100 | + const { sut, ctx } = setup(); |
| 101 | + const { user } = await ctx.newUser(); |
| 102 | + const tagRepo = ctx.get(TagRepository); |
| 103 | + const [parentTag, childTag] = await upsertTags(tagRepo, { userId: user.id, tags: ['parent', 'parent/child'] }); |
| 104 | + |
| 105 | + await expect(tagRepo.getByValue(user.id, 'parent')).resolves.toEqual( |
| 106 | + expect.objectContaining({ id: parentTag.id }), |
| 107 | + ); |
| 108 | + await expect(tagRepo.getByValue(user.id, 'parent/child')).resolves.toEqual( |
| 109 | + expect.objectContaining({ id: childTag.id }), |
| 110 | + ); |
| 111 | + await expect(sut.handleTagCleanup()).resolves.toBe(JobStatus.Success); |
| 112 | + await expect(tagRepo.getByValue(user.id, 'parent/child')).resolves.toBeUndefined(); |
| 113 | + await expect(tagRepo.getByValue(user.id, 'parent')).resolves.toBeUndefined(); |
| 114 | + }); |
| 115 | + }); |
| 116 | +}); |
0 commit comments