Skip to content

Commit 2c9ea33

Browse files
committed
fix: unconditionally add the standard answer with id 0
1 parent 6d0cbeb commit 2c9ea33

File tree

2 files changed

+32
-39
lines changed

2 files changed

+32
-39
lines changed

kleros-sdk/src/utils/getDispute.ts

+10-12
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,16 @@ export const getDispute = async (disputeParameters: GetDisputeParameters): Promi
5656

5757
const populatedTemplate = populateTemplate(templateData, data);
5858

59-
// Ensure Refuse to Arbitrate option exists
60-
if (!populatedTemplate.answers?.some((answer) => answer.id && Number(answer.id) === 0)) {
61-
populatedTemplate.answers = [
62-
{
63-
id: "0x0",
64-
title: "Refuse to Arbitrate / Invalid",
65-
description: "Refuse to Arbitrate / Invalid",
66-
reserved: true,
67-
},
68-
...(populatedTemplate.answers || []),
69-
];
70-
}
59+
// Filter out any existing answer with id 0 and add our standard Refuse to Arbitrate option
60+
populatedTemplate.answers = [
61+
{
62+
id: "0x0",
63+
title: "Refuse to Arbitrate / Invalid",
64+
description: "Refuse to Arbitrate / Invalid",
65+
reserved: true,
66+
},
67+
...(populatedTemplate.answers?.filter((answer) => answer.id && Number(answer.id) !== 0) || []),
68+
];
7169

7270
return populatedTemplate;
7371
};

kleros-sdk/test/getDispute.test.ts

+22-27
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ describe("getDispute", () => {
1212
const mockCoreSubgraph = "https://api.thegraph.com/subgraphs/name/kleros/core";
1313
const mockDtrSubgraph = "https://api.thegraph.com/subgraphs/name/kleros/dtr";
1414

15+
const standardRefuseToArbitrateAnswer = {
16+
id: "0x0",
17+
title: "Refuse to Arbitrate / Invalid",
18+
description: "Refuse to Arbitrate / Invalid",
19+
reserved: true,
20+
};
21+
1522
const mockDisputeDetails = {
1623
dispute: {
1724
templateId: 1,
@@ -52,12 +59,7 @@ describe("getDispute", () => {
5259
});
5360

5461
expect(result?.answers).toHaveLength(1);
55-
expect(result?.answers[0]).toEqual({
56-
id: "0x0",
57-
title: "Refuse to Arbitrate / Invalid",
58-
description: "Refuse to Arbitrate / Invalid",
59-
reserved: true,
60-
});
62+
expect(result?.answers[0]).toEqual(standardRefuseToArbitrateAnswer);
6163
});
6264

6365
it("should add Refuse to Arbitrate option when it doesn't exist in answers", async () => {
@@ -98,18 +100,14 @@ describe("getDispute", () => {
98100
});
99101

100102
expect(result?.answers).toHaveLength(3);
101-
expect(result?.answers[0]).toEqual({
102-
id: "0x0",
103-
title: "Refuse to Arbitrate / Invalid",
104-
description: "Refuse to Arbitrate / Invalid",
105-
reserved: true,
106-
});
103+
expect(result?.answers[0]).toEqual(standardRefuseToArbitrateAnswer);
107104
expect(result?.answers[1].id).toBe("0x1");
108105
expect(result?.answers[2].id).toBe("0x2");
109106
});
110107

111-
it("should not add Refuse to Arbitrate option when it already exists with id 0x0", async () => {
112-
const mockTemplate = {
108+
it("should overwrite existing answer with id 0x0 or 0x00", async () => {
109+
// Test with 0x0
110+
const mockTemplate0x0 = {
113111
disputeTemplate: {
114112
templateData: JSON.stringify({
115113
title: "Test Dispute",
@@ -118,8 +116,8 @@ describe("getDispute", () => {
118116
answers: [
119117
{
120118
id: "0x0",
121-
title: "Refuse to Arbitrate / Invalid",
122-
description: "Refuse to Arbitrate / Invalid",
119+
title: "Custom Refuse Title",
120+
description: "Custom Refuse Description",
123121
reserved: true,
124122
},
125123
{
@@ -138,21 +136,20 @@ describe("getDispute", () => {
138136
};
139137

140138
vi.mocked(fetchDisputeDetails).mockResolvedValue(mockDisputeDetails);
141-
vi.mocked(fetchDisputeTemplateFromId).mockResolvedValue(mockTemplate);
139+
vi.mocked(fetchDisputeTemplateFromId).mockResolvedValue(mockTemplate0x0);
142140

143-
const result = await getDispute({
141+
let result = await getDispute({
144142
disputeId: mockDisputeId,
145143
coreSubgraph: mockCoreSubgraph,
146144
dtrSubgraph: mockDtrSubgraph,
147145
});
148146

149147
expect(result?.answers).toHaveLength(2);
150-
expect(result?.answers[0].id).toBe("0x0");
148+
expect(result?.answers[0]).toEqual(standardRefuseToArbitrateAnswer);
151149
expect(result?.answers[1].id).toBe("0x1");
152-
});
153150

154-
it("should not add Refuse to Arbitrate option when it already exists with id 0x00", async () => {
155-
const mockTemplate = {
151+
// Test with 0x00
152+
const mockTemplate0x00 = {
156153
disputeTemplate: {
157154
templateData: JSON.stringify({
158155
title: "Test Dispute",
@@ -180,18 +177,16 @@ describe("getDispute", () => {
180177
},
181178
};
182179

183-
vi.mocked(fetchDisputeDetails).mockResolvedValue(mockDisputeDetails);
184-
vi.mocked(fetchDisputeTemplateFromId).mockResolvedValue(mockTemplate);
180+
vi.mocked(fetchDisputeTemplateFromId).mockResolvedValue(mockTemplate0x00);
185181

186-
const result = await getDispute({
182+
result = await getDispute({
187183
disputeId: mockDisputeId,
188184
coreSubgraph: mockCoreSubgraph,
189185
dtrSubgraph: mockDtrSubgraph,
190186
});
191187

192188
expect(result?.answers).toHaveLength(2);
193-
expect(result?.answers[0].id).toBe("0x00");
194-
expect(result?.answers[0].title).toBe("Custom Refuse Title");
189+
expect(result?.answers[0]).toEqual(standardRefuseToArbitrateAnswer);
195190
expect(result?.answers[1].id).toBe("0x1");
196191
});
197192

0 commit comments

Comments
 (0)