|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { clampPriorityMs } from "./priority"; |
| 3 | + |
| 4 | +const INT4_MIN = -2_147_483_648; |
| 5 | +const INT4_MAX = 2_147_483_647; |
| 6 | + |
| 7 | +describe("clampPriorityMs", () => { |
| 8 | + it("converts seconds to milliseconds for in-range values", () => { |
| 9 | + expect(clampPriorityMs(10)).toBe(10_000); |
| 10 | + expect(clampPriorityMs(0.5)).toBe(500); |
| 11 | + }); |
| 12 | + |
| 13 | + it("rounds a sub-millisecond fractional priority to an integer", () => { |
| 14 | + expect(clampPriorityMs(0.0005)).toBe(1); |
| 15 | + expect(clampPriorityMs(0.00049)).toBe(0); |
| 16 | + expect(Number.isInteger(clampPriorityMs(0.0005))).toBe(true); |
| 17 | + }); |
| 18 | + |
| 19 | + it("clamps a value that would overflow INT4 down to the column max", () => { |
| 20 | + const priority = 31_536_000; |
| 21 | + expect(priority * 1_000).toBeGreaterThan(INT4_MAX); |
| 22 | + expect(clampPriorityMs(priority)).toBe(INT4_MAX); |
| 23 | + }); |
| 24 | + |
| 25 | + it("leaves the largest safe priority untouched", () => { |
| 26 | + expect(clampPriorityMs(2_147_483)).toBe(2_147_483_000); |
| 27 | + }); |
| 28 | + |
| 29 | + it("clamps a large negative priority to the column min", () => { |
| 30 | + expect(clampPriorityMs(-3_000_000)).toBe(INT4_MIN); |
| 31 | + }); |
| 32 | + |
| 33 | + it("keeps every result inside the INT4 range", () => { |
| 34 | + for (const priority of [-1e12, -5, -0.3, 0, 0.7, 5, 1234.5678, 1e12]) { |
| 35 | + const result = clampPriorityMs(priority); |
| 36 | + expect(Number.isInteger(result)).toBe(true); |
| 37 | + expect(result).toBeGreaterThanOrEqual(INT4_MIN); |
| 38 | + expect(result).toBeLessThanOrEqual(INT4_MAX); |
| 39 | + } |
| 40 | + }); |
| 41 | +}); |
0 commit comments