Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/constant/protocol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const MIN_AGGREGATION_CONFIDENCE =
0.5;
35 changes: 35 additions & 0 deletions src/utils/confidence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
MIN_AGGREGATION_CONFIDENCE,
} from '../constants/protocol';

export function applyConfidenceFloor(
confidence: number,
): number {
if (
Number.isNaN(confidence)
) {
return (
MIN_AGGREGATION_CONFIDENCE
);
}

return Math.max(
confidence,
MIN_AGGREGATION_CONFIDENCE,
);
}

export function normalizeConfidence(
confidence: number,
): number {
if (
!Number.isFinite(confidence)
) {
return 0.5;
}

return Math.min(
1,
Math.max(confidence, 0.5),
);
}
60 changes: 60 additions & 0 deletions test/aggregationConfidence.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
describe(
'aggregation confidence floor',
() => {
it(
'clamps confidence below 0.5',
() => {
expect(
applyConfidenceFloor(
0.2,
),
).toBe(0.5);
},
);
},
);

it(
'preserves confidence above floor',
() => {
expect(
applyConfidenceFloor(
0.91,
),
).toBe(0.91);
},
);

it(
'handles NaN safely',
() => {
expect(
applyConfidenceFloor(
NaN,
),
).toBe(0.5);
},
);

it(
'clamps negative values',
() => {
expect(
applyConfidenceFloor(
-5,
),
).toBe(0.5);
},
);

it(
'handles infinite values safely',
() => {
expect(
normalizeConfidence(
Infinity,
),
).toBe(1);
},
);

34 changes: 34 additions & 0 deletions test/claims.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Test, TestingModule } from "@nestjs/testing";
import { INestApplication } from "@nestjs/common";
import * as request from "supertest";

import { AppModule } from "@/src/app.module";

describe("Claims (e2e)", () => {
let app: INestApplication;

beforeAll(async () => {
const moduleFixture: TestingModule =
await Test.createTestingModule({
imports: [AppModule],
}).compile();

app = moduleFixture.createNestApplication();
await app.init();
});

afterAll(async () => {
await app.close();
});

it("GET /claims/:id should return 404 for unknown id", async () => {
const unknownId = "non-existent-id";

await request(app.getHttpServer())
.get(`/claims/${unknownId}`)
.expect(404)
.expect((res) => {
expect(res.body.message).toContain("not found");
});
});
});
Loading