diff --git a/server/test/medium/specs/services/tag.service.spec.ts b/server/test/medium/specs/services/tag.service.spec.ts index d1b0a245e1..6a7e9b7b96 100644 --- a/server/test/medium/specs/services/tag.service.spec.ts +++ b/server/test/medium/specs/services/tag.service.spec.ts @@ -1,14 +1,11 @@ import { Kysely } from "kysely"; import { JobStatus } from "src/enum"; import { AccessRepository } from "src/repositories/access.repository"; -import { EventRepository } from "src/repositories/event.repository"; import { LoggingRepository } from "src/repositories/logging.repository"; -import { StorageRepository } from "src/repositories/storage.repository"; import { TagRepository } from "src/repositories/tag.repository"; import { DB } from "src/schema"; import { TagService } from "src/services/tag.service"; import { newMediumService } from "test/medium.factory"; -import { factory } from "test/small.factory"; import { getKyselyDB } from "test/utils"; let defaultDatabase: Kysely; @@ -25,10 +22,6 @@ beforeAll(async () => { defaultDatabase = await getKyselyDB(); }); -// single tag exists, connected to one asset, and is not deleted -// hierarchical tag exists, and the parent is connected to an asset, and the child is deleted -// hierarchical tag exists, and only the child is connected to an asset, and nothing is deleted - describe(TagService.name, () => { describe('deleteEmptyTags', () => { it('single tag exists, not connected to any assets, and is deleted', async () => { @@ -56,5 +49,39 @@ describe(TagService.name, () => { await expect(tagRepo.getByValue(user.id, 'tag-1')).resolves.toEqual(expect.objectContaining({ id: tag.id })); }) + it('hierarchical tag exists, and the parent is connected to an asset, and the child is deleted', async () => { + const { sut, ctx } = setup(); + const { user } = await ctx.newUser(); + const { asset } = await ctx.newAsset({ ownerId: user.id }); + const { tag: parentTag } = await ctx.newTag({ userId: user.id, value: 'parent' }); + const { tag: childrenTag } = await ctx.newTag({ userId: user.id, value: 'child', parentId: parentTag.id }); + const tagRepo = ctx.get(TagRepository); + + await ctx.newTagAsset({ tagIds: [parentTag.id], assetIds: [asset.id] }); + + await expect(tagRepo.getByValue(user.id, 'parent')).resolves.toEqual(expect.objectContaining({ id: parentTag.id })); + await expect(tagRepo.getByValue(user.id, 'child')).resolves.toEqual(expect.objectContaining({ id: childrenTag.id })); + await expect(sut.handleTagCleanup()).resolves.toBe(JobStatus.Success); + await expect(tagRepo.getByValue(user.id, 'parent')).resolves.toEqual(expect.objectContaining({ id: parentTag.id })); + await expect(tagRepo.getByValue(user.id, 'child')).resolves.toBeUndefined(); + }) + + it('hierarchical tag exists, and only the child is connected to an asset, and nothing is deleted', async () => { + const { sut, ctx } = setup(); + const { user } = await ctx.newUser(); + const { asset } = await ctx.newAsset({ ownerId: user.id }); + const { tag: parentTag } = await ctx.newTag({ userId: user.id, value: 'parent' }); + const { tag: childrenTag } = await ctx.newTag({ userId: user.id, value: 'child', parentId: parentTag.id }); + const tagRepo = ctx.get(TagRepository); + + await ctx.newTagAsset({ tagIds: [childrenTag.id], assetIds: [asset.id] }); + + await expect(tagRepo.getByValue(user.id, 'parent')).resolves.toEqual(expect.objectContaining({ id: parentTag.id })); + await expect(tagRepo.getByValue(user.id, 'child')).resolves.toEqual(expect.objectContaining({ id: childrenTag.id })); + await expect(sut.handleTagCleanup()).resolves.toBe(JobStatus.Success); + await expect(tagRepo.getByValue(user.id, 'parent')).resolves.toEqual(expect.objectContaining({ id: parentTag.id })); + await expect(tagRepo.getByValue(user.id, 'child')).resolves.toEqual(expect.objectContaining({ id: childrenTag.id })); + }) + }) }) \ No newline at end of file