Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions src/modules/nft/__tests__/application/repository/repository.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import NFTRepository from "../../../repositories/nft.repository";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ’‘ Verification agent

🧩 Analysis chain

Fix import: repository exports are named, not default.

nft.repository.ts exposes export class NFTRepository, not a default export. Use a named import.

Apply:

-import NFTRepository from "../../../repositories/nft.repository";
+import { NFTRepository } from "../../../repositories/nft.repository";

Verify with:


🏁 Script executed:

#!/bin/bash
rg -nP 'export\s+default\s+NFTRepository\b|export\s+default\s+\w*NFTRepository' src/modules/nft/repositories/nft.repository.ts || true
echo "Legacy method names present?"
rg -nP 'async\s+(createNFT|getNFTById|getNFTByUserId|deleteNFT)\s*\(' src/modules/nft/repositories/nft.repository.ts || echo "None"
echo "Current methods present:"
rg -nP 'async\s+(create|findById|findByUserId|update|delete)\s*\(' src/modules/nft/repositories/nft.repository.ts

Length of output: 749


Fix import to use named export for NFTRepository
src/modules/nft/__tests__/application/repository/repository.test.ts currently imports NFTRepository as a default export, but src/modules/nft/repositories/nft.repository.ts exports it by name. Update as follows:

-import NFTRepository from "../../../repositories/nft.repository";
+import { NFTRepository } from "../../../repositories/nft.repository";
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
import NFTRepository from "../../../repositories/nft.repository";
import { NFTRepository } from "../../../repositories/nft.repository";
πŸ€– Prompt for AI Agents
In src/modules/nft/__tests__/application/repository/repository.test.ts around
line 1, the test imports NFTRepository as a default export but the
implementation file exports it as a named export; update the import statement to
use a named import (e.g., import { NFTRepository } from
"../../../repositories/nft.repository";) so the test imports match the
repository's named export.


// filepath: src/modules/nft/_tests_/application/repository/repository.test.ts
// Note: We recommend installing an extension to run jest tests.

describe("NFTRepository - reference-related operations", () => {
let repository: any;
let mockModel: any;
let mockPrisma: any;

beforeEach(() => {
repository = new NFTRepository();

mockModel = {
create: jest.fn(),
findUnique: jest.fn(),
findMany: jest.fn(),
delete: jest.fn(),
};
Comment on lines +14 to +19
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add missing Prisma count mock.

findByUserId calls nFT.count. Without this, tests for pagination will break.

         mockModel = {
             create: jest.fn(),
             findUnique: jest.fn(),
             findMany: jest.fn(),
             delete: jest.fn(),
+            count: jest.fn(),
         };
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
mockModel = {
create: jest.fn(),
findUnique: jest.fn(),
findMany: jest.fn(),
delete: jest.fn(),
};
mockModel = {
create: jest.fn(),
findUnique: jest.fn(),
findMany: jest.fn(),
delete: jest.fn(),
count: jest.fn(),
};
πŸ€– Prompt for AI Agents
In src/modules/nft/__tests__/application/repository/repository.test.ts around
lines 14 to 19, the test's mockModel is missing a Prisma-style count method that
repository.findByUserId invokes for pagination; add a count mock (e.g., count:
jest.fn()) to mockModel and ensure tests that assert pagination stub the count
mock's return value appropriately so pagination paths exercise expected
behavior.


// Some codebases use nft while others nFT for the Prisma model accessor.
// Provide both to make tests resilient to either shape.
mockPrisma = {
nft: mockModel,
nFT: mockModel,
};

// Inject the mocked prisma into the repository instance regardless of how it's referenced inside.
(repository as any).prisma = mockPrisma;
});

it("should expose CRUD methods", () => {
expect(typeof repository.createNFT).toBe("function");
expect(typeof repository.getNFTById).toBe("function");
expect(typeof repository.getNFTByUserId).toBe("function");
expect(typeof repository.deleteNFT).toBe("function");
});
Comment on lines +32 to +37
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Method names don’t exist on NFTRepository.

The repository exposes create, findById, findByUserId, and delete. Update assertions.

-        expect(typeof repository.createNFT).toBe("function");
-        expect(typeof repository.getNFTById).toBe("function");
-        expect(typeof repository.getNFTByUserId).toBe("function");
-        expect(typeof repository.deleteNFT).toBe("function");
+        expect(typeof repository.create).toBe("function");
+        expect(typeof repository.findById).toBe("function");
+        expect(typeof repository.findByUserId).toBe("function");
+        expect(typeof repository.delete).toBe("function");
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
it("should expose CRUD methods", () => {
expect(typeof repository.createNFT).toBe("function");
expect(typeof repository.getNFTById).toBe("function");
expect(typeof repository.getNFTByUserId).toBe("function");
expect(typeof repository.deleteNFT).toBe("function");
});
it("should expose CRUD methods", () => {
expect(typeof repository.create).toBe("function");
expect(typeof repository.findById).toBe("function");
expect(typeof repository.findByUserId).toBe("function");
expect(typeof repository.delete).toBe("function");
});
πŸ€– Prompt for AI Agents
In src/modules/nft/__tests__/application/repository/repository.test.ts around
lines 32 to 37, the test asserts non-existent method names (createNFT,
getNFTById, getNFTByUserId, deleteNFT); update the assertions to check the
actual NFTRepository method names: expect(typeof
repository.create).toBe("function"); expect(typeof
repository.findById).toBe("function"); expect(typeof
repository.findByUserId).toBe("function"); and expect(typeof
repository.delete).toBe("function").


it("createNFT() - creates an NFT and returns its reference", async () => {
const payload = {
userId: "user-1",
organizationId: "org-1",
description: "An NFT for testing",
isMinted: false,
};

const created = { id: "nft-1", ...payload };
mockModel.create.mockResolvedValue(created);

const result = await repository.createNFT(payload);

expect(mockModel.create).toHaveBeenCalled();
// Common Prisma pattern: create({ data: payload })
expect(mockModel.create.mock.calls[0][0]).toEqual(expect.objectContaining({ data: payload }));
expect(result).toEqual(created);
});
Comment on lines +39 to +56
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Align create test with repository contract and domain mapping.

  • Method is create, not createNFT.
  • Payload fields should match what create actually writes (userId, organizationId, description).
  • The repository returns a domain NFT instance, not the raw Prisma object.
-    it("createNFT() - creates an NFT and returns its reference", async () => {
+    it("create() - creates an NFT and returns mapped domain entity", async () => {
         const payload = {
             userId: "user-1",
             organizationId: "org-1",
             description: "An NFT for testing",
-            isMinted: false,
         };
 
-        const created = { id: "nft-1", ...payload };
+        const created = { id: "nft-1", ...payload };
         mockModel.create.mockResolvedValue(created);
 
-        const result = await repository.createNFT(payload);
+        const result = await repository.create(payload);
 
-        expect(mockModel.create).toHaveBeenCalled();
-        // Common Prisma pattern: create({ data: payload })
-        expect(mockModel.create.mock.calls[0][0]).toEqual(expect.objectContaining({ data: payload }));
-        expect(result).toEqual(created);
+        expect(mockModel.create).toHaveBeenCalledWith(expect.objectContaining({ data: payload }));
+        expect(result).toMatchObject({
+            id: created.id,
+            userId: payload.userId,
+            organizationId: payload.organizationId,
+            description: payload.description,
+        });
     });
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
it("createNFT() - creates an NFT and returns its reference", async () => {
const payload = {
userId: "user-1",
organizationId: "org-1",
description: "An NFT for testing",
isMinted: false,
};
const created = { id: "nft-1", ...payload };
mockModel.create.mockResolvedValue(created);
const result = await repository.createNFT(payload);
expect(mockModel.create).toHaveBeenCalled();
// Common Prisma pattern: create({ data: payload })
expect(mockModel.create.mock.calls[0][0]).toEqual(expect.objectContaining({ data: payload }));
expect(result).toEqual(created);
});
it("create() - creates an NFT and returns mapped domain entity", async () => {
const payload = {
userId: "user-1",
organizationId: "org-1",
description: "An NFT for testing",
};
const created = { id: "nft-1", ...payload };
mockModel.create.mockResolvedValue(created);
const result = await repository.create(payload);
expect(mockModel.create).toHaveBeenCalledWith(expect.objectContaining({ data: payload }));
expect(result).toMatchObject({
id: created.id,
userId: payload.userId,
organizationId: payload.organizationId,
description: payload.description,
});
});
πŸ€– Prompt for AI Agents
In src/modules/nft/__tests__/application/repository/repository.test.ts around
lines 39 to 56, the test incorrectly references createNFT, uses an extra
isMinted field, and expects the raw Prisma object back; update the test to call
repository.create, build the payload only with fields the repo writes (userId,
organizationId, description), mock model.create to resolve the raw DB object,
and assert mockModel.create was called with an objectContaining({ data: payload
}); finally, change the expected result to the repository's domain NFT instance
(apply the same mapping the repository uses or instantiate the NFT domain
object) and assert the returned value equals that domain instance.


it("getNFTById() - retrieves an NFT using its ID", async () => {
const nft = { id: "nft-2", userId: "user-2", description: "by id" };
mockModel.findUnique.mockResolvedValue(nft);

const result = await repository.getNFTById("nft-2");

expect(mockModel.findUnique).toHaveBeenCalled();
// Common Prisma pattern: findUnique({ where: { id } })
expect(mockModel.findUnique.mock.calls[0][0]).toEqual(expect.objectContaining({ where: { id: "nft-2" } }));
expect(result).toEqual(nft);
});
Comment on lines +58 to +68
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Use the correct read method and assert on mapped entity.

Method is findById, which maps to domain NFT.

-    it("getNFTById() - retrieves an NFT using its ID", async () => {
+    it("findById() - retrieves an NFT using its ID", async () => {
         const nft = { id: "nft-2", userId: "user-2", description: "by id" };
         mockModel.findUnique.mockResolvedValue(nft);
 
-        const result = await repository.getNFTById("nft-2");
+        const result = await repository.findById("nft-2");
 
         expect(mockModel.findUnique).toHaveBeenCalled();
         // Common Prisma pattern: findUnique({ where: { id } })
         expect(mockModel.findUnique.mock.calls[0][0]).toEqual(expect.objectContaining({ where: { id: "nft-2" } }));
-        expect(result).toEqual(nft);
+        expect(result).toMatchObject(nft);
     });
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
it("getNFTById() - retrieves an NFT using its ID", async () => {
const nft = { id: "nft-2", userId: "user-2", description: "by id" };
mockModel.findUnique.mockResolvedValue(nft);
const result = await repository.getNFTById("nft-2");
expect(mockModel.findUnique).toHaveBeenCalled();
// Common Prisma pattern: findUnique({ where: { id } })
expect(mockModel.findUnique.mock.calls[0][0]).toEqual(expect.objectContaining({ where: { id: "nft-2" } }));
expect(result).toEqual(nft);
});
it("findById() - retrieves an NFT using its ID", async () => {
const nft = { id: "nft-2", userId: "user-2", description: "by id" };
mockModel.findUnique.mockResolvedValue(nft);
const result = await repository.findById("nft-2");
expect(mockModel.findUnique).toHaveBeenCalled();
// Common Prisma pattern: findUnique({ where: { id } })
expect(mockModel.findUnique.mock.calls[0][0]).toEqual(expect.objectContaining({ where: { id: "nft-2" } }));
expect(result).toMatchObject(nft);
});
πŸ€– Prompt for AI Agents
In src/modules/nft/__tests__/application/repository/repository.test.ts around
lines 58 to 68, the test uses the wrong model method and asserts the raw DB
object; change the mock and expectations to use the repository's read method
findById (mockModel.findById) and have the mock return the DB row, then assert
the repository returns the mapped domain NFT (e.g., an instance or object
matching the NFT shape) β€” update mockModel.findUnique.mockResolvedValue to
mockModel.findById.mockResolvedValue, replace calls/expectations checking
findUnique and its call args with findById and its expected argument (id or { id
}), and change the final expect to compare result to the mapped NFT domain
object rather than the raw DB row.


it("getNFTByUserId() - retrieves NFTs owned by a specific user", async () => {
const nfts = [
{ id: "nft-a", userId: "owner-1", description: "A" },
{ id: "nft-b", userId: "owner-1", description: "B" },
];
mockModel.findMany.mockResolvedValue(nfts);

const result = await repository.getNFTByUserId("owner-1");

expect(mockModel.findMany).toHaveBeenCalled();
// Common Prisma pattern: findMany({ where: { userId } })
expect(mockModel.findMany.mock.calls[0][0]).toEqual(expect.objectContaining({ where: { userId: "owner-1" } }));
expect(result).toEqual(nfts);
});
Comment on lines +70 to +83
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Account for pagination and total count in findByUserId.

findByUserId(userId, page, pageSize) returns { nfts, total } and calls both findMany and count.

-    it("getNFTByUserId() - retrieves NFTs owned by a specific user", async () => {
+    it("findByUserId() - retrieves NFTs owned by a specific user with pagination", async () => {
         const nfts = [
             { id: "nft-a", userId: "owner-1", description: "A" },
             { id: "nft-b", userId: "owner-1", description: "B" },
         ];
         mockModel.findMany.mockResolvedValue(nfts);
+        mockModel.count.mockResolvedValue(nfts.length);
 
-        const result = await repository.getNFTByUserId("owner-1");
+        const result = await repository.findByUserId("owner-1", 1, 2);
 
         expect(mockModel.findMany).toHaveBeenCalled();
-        // Common Prisma pattern: findMany({ where: { userId } })
-        expect(mockModel.findMany.mock.calls[0][0]).toEqual(expect.objectContaining({ where: { userId: "owner-1" } }));
-        expect(result).toEqual(nfts);
+        expect(mockModel.findMany.mock.calls[0][0]).toEqual(
+            expect.objectContaining({
+                where: { userId: "owner-1" },
+                skip: 0,
+                take: 2,
+                orderBy: { createdAt: "desc" },
+            })
+        );
+        expect(mockModel.count).toHaveBeenCalledWith({ where: { userId: "owner-1" } });
+        expect(result.total).toBe(nfts.length);
+        expect(result.nfts).toHaveLength(nfts.length);
+        expect(result.nfts[0]).toMatchObject(nfts[0]);
     });
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
it("getNFTByUserId() - retrieves NFTs owned by a specific user", async () => {
const nfts = [
{ id: "nft-a", userId: "owner-1", description: "A" },
{ id: "nft-b", userId: "owner-1", description: "B" },
];
mockModel.findMany.mockResolvedValue(nfts);
const result = await repository.getNFTByUserId("owner-1");
expect(mockModel.findMany).toHaveBeenCalled();
// Common Prisma pattern: findMany({ where: { userId } })
expect(mockModel.findMany.mock.calls[0][0]).toEqual(expect.objectContaining({ where: { userId: "owner-1" } }));
expect(result).toEqual(nfts);
});
it("findByUserId() - retrieves NFTs owned by a specific user with pagination", async () => {
const nfts = [
{ id: "nft-a", userId: "owner-1", description: "A" },
{ id: "nft-b", userId: "owner-1", description: "B" },
];
mockModel.findMany.mockResolvedValue(nfts);
mockModel.count.mockResolvedValue(nfts.length);
const result = await repository.findByUserId("owner-1", 1, 2);
expect(mockModel.findMany).toHaveBeenCalled();
expect(mockModel.findMany.mock.calls[0][0]).toEqual(
expect.objectContaining({
where: { userId: "owner-1" },
skip: 0,
take: 2,
orderBy: { createdAt: "desc" },
})
);
expect(mockModel.count).toHaveBeenCalledWith({ where: { userId: "owner-1" } });
expect(result.total).toBe(nfts.length);
expect(result.nfts).toHaveLength(nfts.length);
expect(result.nfts[0]).toMatchObject(nfts[0]);
});
πŸ€– Prompt for AI Agents
In src/modules/nft/__tests__/application/repository/repository.test.ts around
lines 70 to 83, the test assumes repository.getNFTByUserId returns only an array
but the real implementation returns an object { nfts, total } and calls both
findMany and count; update the test to
mockModel.count.mockResolvedValue(totalNumber), adjust mockModel.findMany to
resolve the nfts, call repository.getNFTByUserId with page and pageSize args,
assert mockModel.findMany was called with pagination args (skip and take derived
from page/pageSize) and mockModel.count was called with the userId filter, and
finally expect the result toEqual({ nfts, total: totalNumber }).


it("deleteNFT() - removes an NFT and ensures it is no longer retrievable", async () => {
const deleted = { id: "nft-del", userId: "owner-del", description: "to be deleted" };
mockModel.delete.mockResolvedValue(deleted);

const result = await repository.deleteNFT("nft-del");

expect(mockModel.delete).toHaveBeenCalled();
// Common Prisma pattern: delete({ where: { id } })
expect(mockModel.delete.mock.calls[0][0]).toEqual(expect.objectContaining({ where: { id: "nft-del" } }));
expect(result).toEqual(deleted);

// Simulate subsequent retrieval returns null (not found)
mockModel.findUnique.mockResolvedValueOnce(null);
const afterDelete = await repository.getNFTById("nft-del");
expect(afterDelete).toBeNull();
});
Comment on lines +85 to +100
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

delete returns void; use findById to verify post-delete.

Method is delete(id: string): Promise<void>. Adjust expectations.

-    it("deleteNFT() - removes an NFT and ensures it is no longer retrievable", async () => {
+    it("delete() - removes an NFT and ensures it is no longer retrievable", async () => {
         const deleted = { id: "nft-del", userId: "owner-del", description: "to be deleted" };
         mockModel.delete.mockResolvedValue(deleted);
 
-        const result = await repository.deleteNFT("nft-del");
+        await repository.delete("nft-del");
 
         expect(mockModel.delete).toHaveBeenCalled();
         // Common Prisma pattern: delete({ where: { id } })
-        expect(mockModel.delete.mock.calls[0][0]).toEqual(expect.objectContaining({ where: { id: "nft-del" } }));
-        expect(result).toEqual(deleted);
+        expect(mockModel.delete.mock.calls[0][0]).toEqual(expect.objectContaining({ where: { id: "nft-del" } }));
 
         // Simulate subsequent retrieval returns null (not found)
         mockModel.findUnique.mockResolvedValueOnce(null);
-        const afterDelete = await repository.getNFTById("nft-del");
+        const afterDelete = await repository.findById("nft-del");
         expect(afterDelete).toBeNull();
     });
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
it("deleteNFT() - removes an NFT and ensures it is no longer retrievable", async () => {
const deleted = { id: "nft-del", userId: "owner-del", description: "to be deleted" };
mockModel.delete.mockResolvedValue(deleted);
const result = await repository.deleteNFT("nft-del");
expect(mockModel.delete).toHaveBeenCalled();
// Common Prisma pattern: delete({ where: { id } })
expect(mockModel.delete.mock.calls[0][0]).toEqual(expect.objectContaining({ where: { id: "nft-del" } }));
expect(result).toEqual(deleted);
// Simulate subsequent retrieval returns null (not found)
mockModel.findUnique.mockResolvedValueOnce(null);
const afterDelete = await repository.getNFTById("nft-del");
expect(afterDelete).toBeNull();
});
it("delete() - removes an NFT and ensures it is no longer retrievable", async () => {
const deleted = { id: "nft-del", userId: "owner-del", description: "to be deleted" };
mockModel.delete.mockResolvedValue(deleted);
// Since delete() returns void, just await it
await repository.delete("nft-del");
expect(mockModel.delete).toHaveBeenCalled();
// Common Prisma pattern: delete({ where: { id } })
expect(mockModel.delete.mock.calls[0][0])
.toEqual(expect.objectContaining({ where: { id: "nft-del" } }));
// Simulate subsequent retrieval returns null (not found)
mockModel.findUnique.mockResolvedValueOnce(null);
const afterDelete = await repository.findById("nft-del");
expect(afterDelete).toBeNull();
});
πŸ€– Prompt for AI Agents
src/modules/nft/__tests__/application/repository/repository.test.ts around lines
85-100: The test assumes delete returns the deleted record but the
repository.delete method signature is Promise<void>; change the mock and
assertions so mockModel.delete.mockResolvedValueOnce(undefined) (or no value),
keep verifying it was called with the Prisma-style argument
(expect(mockModel.delete.mock.calls[0][0]).toEqual(expect.objectContaining({
where: { id: "nft-del" } }))), assert the returned result is undefined (or
removed) instead of the deleted object, then simulate retrieval by setting
mockModel.findUnique.mockResolvedValueOnce(null) and asserting
repository.getNFTById("nft-del") returns null to verify post-delete behavior.

});