single tag exists, connected to one asset, and is not deleted

This commit is contained in:
Jorge Montejo 2025-10-03 20:46:08 +02:00
parent 0aaaaa17bc
commit b7a08f9531
2 changed files with 29 additions and 3 deletions

View file

@ -53,6 +53,7 @@ import { MemoryTable } from 'src/schema/tables/memory.table';
import { PersonTable } from 'src/schema/tables/person.table'; import { PersonTable } from 'src/schema/tables/person.table';
import { SessionTable } from 'src/schema/tables/session.table'; import { SessionTable } from 'src/schema/tables/session.table';
import { StackTable } from 'src/schema/tables/stack.table'; import { StackTable } from 'src/schema/tables/stack.table';
import { TagAssetTable } from 'src/schema/tables/tag-asset.table';
import { TagTable } from 'src/schema/tables/tag.table'; import { TagTable } from 'src/schema/tables/tag.table';
import { UserTable } from 'src/schema/tables/user.table'; import { UserTable } from 'src/schema/tables/user.table';
import { BASE_SERVICE_DEPENDENCIES, BaseService } from 'src/services/base.service'; import { BASE_SERVICE_DEPENDENCIES, BaseService } from 'src/services/base.service';
@ -248,6 +249,18 @@ export class MediumTestContext<S extends BaseService = BaseService> {
const result = await this.get(TagRepository).create(tag); const result = await this.get(TagRepository).create(tag);
return { tag, result }; return { tag, result };
} }
async newTagAsset(tagBulkAssets: { tagIds: string[], assetIds: string[] }) {
const tagsAssets: Insertable<TagAssetTable>[] = [];
for (const tagsId of tagBulkAssets.tagIds) {
for (const assetsId of tagBulkAssets.assetIds) {
tagsAssets.push({ tagsId, assetsId });
}
}
const result = await this.get(TagRepository).upsertAssetIds(tagsAssets);
return { tagsAssets, result };
}
} }
export class SyncTestContext extends MediumTestContext<SyncService> { export class SyncTestContext extends MediumTestContext<SyncService> {

View file

@ -1,4 +1,5 @@
import { Kysely } from "kysely"; import { Kysely } from "kysely";
import { JobStatus } from "src/enum";
import { AccessRepository } from "src/repositories/access.repository"; import { AccessRepository } from "src/repositories/access.repository";
import { EventRepository } from "src/repositories/event.repository"; import { EventRepository } from "src/repositories/event.repository";
import { LoggingRepository } from "src/repositories/logging.repository"; import { LoggingRepository } from "src/repositories/logging.repository";
@ -32,16 +33,28 @@ describe(TagService.name, () => {
describe('deleteEmptyTags', () => { describe('deleteEmptyTags', () => {
it('single tag exists, not connected to any assets, and is deleted', async () => { it('single tag exists, not connected to any assets, and is deleted', async () => {
const { sut, ctx } = setup(); const { sut, ctx } = setup();
const tagRepo = ctx.get(TagRepository);
const { user } = await ctx.newUser(); const { user } = await ctx.newUser();
const auth = factory.auth({ user });
const { tag } = await ctx.newTag({ userId: user.id, value: 'tag-1' }); const { tag } = await ctx.newTag({ userId: user.id, value: 'tag-1' });
const tagRepo = ctx.get(TagRepository);
await expect(tagRepo.getByValue(user.id, 'tag-1')).resolves.toEqual(expect.objectContaining({ id: tag.id })); await expect(tagRepo.getByValue(user.id, 'tag-1')).resolves.toEqual(expect.objectContaining({ id: tag.id }));
await expect(sut.remove(auth, tag.id)).resolves.toBeUndefined(); await expect(sut.handleTagCleanup()).resolves.toBe(JobStatus.Success);
await expect(tagRepo.getByValue(user.id, 'tag-1')).resolves.toBeUndefined(); await expect(tagRepo.getByValue(user.id, 'tag-1')).resolves.toBeUndefined();
}); });
it('single tag exists, connected to one asset, and is not deleted', async () => {
const { sut, ctx } = setup();
const { user } = await ctx.newUser();
const { asset } = await ctx.newAsset({ ownerId: user.id });
const { tag } = await ctx.newTag({ userId: user.id, value: 'tag-1' });
const tagRepo = ctx.get(TagRepository);
await ctx.newTagAsset({ tagIds: [tag.id], assetIds: [asset.id] });
await expect(tagRepo.getByValue(user.id, 'tag-1')).resolves.toEqual(expect.objectContaining({ id: tag.id }));
await expect(sut.handleTagCleanup()).resolves.toBe(JobStatus.Success);
await expect(tagRepo.getByValue(user.id, 'tag-1')).resolves.toEqual(expect.objectContaining({ id: tag.id }));
})
}) })
}) })