Spaces:
Running
Running
Download server/rejectedTokens.test.ts from Felladrin/MiniSearch: direct link, hf CLI and curl.
- Browser
- Download file 1.73 kB
-
https://huggingface.co/spaces/Felladrin/MiniSearch/resolve/main/server/rejectedTokens.test.ts
- Command line
-
hf download hf://spaces/Felladrin/MiniSearch/server/rejectedTokens.test.ts
-
curl -L -o rejectedTokens.test.ts https://huggingface.co/spaces/Felladrin/MiniSearch/resolve/main/server/rejectedTokens.test.ts
1.73 kB
| import { beforeEach, describe, expect, it, vi } from "vitest"; | |
| describe("rejectedTokens", () => { | |
| beforeEach(() => { | |
| vi.resetModules(); | |
| }); | |
| it("refuses a token it recorded and nothing else", async () => { | |
| const { addRejectedToken, isRejectedToken } = await import( | |
| "./rejectedTokens" | |
| ); | |
| expect(isRejectedToken("a")).toBe(false); | |
| addRejectedToken("a"); | |
| expect(isRejectedToken("a")).toBe(true); | |
| expect(isRejectedToken("b")).toBe(false); | |
| }); | |
| it("counts each distinct token once, however often it is recorded", async () => { | |
| const { addRejectedToken, getRejectedTokenCount } = await import( | |
| "./rejectedTokens" | |
| ); | |
| addRejectedToken("a"); | |
| addRejectedToken("a"); | |
| addRejectedToken("b"); | |
| expect(getRejectedTokenCount()).toBe(2); | |
| }); | |
| it("evicts the oldest token once the cap is reached, so the set stays bounded", async () => { | |
| const { addRejectedToken, isRejectedToken, MAX_REJECTED_TOKENS } = | |
| await import("./rejectedTokens"); | |
| for (let i = 0; i <= MAX_REJECTED_TOKENS; i++) { | |
| addRejectedToken(`token-${i}`); | |
| } | |
| expect(isRejectedToken("token-0")).toBe(false); | |
| expect(isRejectedToken(`token-${MAX_REJECTED_TOKENS}`)).toBe(true); | |
| }); | |
| it("does not evict anything when an already recorded token is recorded again at the cap", async () => { | |
| const { addRejectedToken, isRejectedToken, MAX_REJECTED_TOKENS } = | |
| await import("./rejectedTokens"); | |
| for (let i = 0; i < MAX_REJECTED_TOKENS; i++) { | |
| addRejectedToken(`token-${i}`); | |
| } | |
| addRejectedToken("token-5"); | |
| expect(isRejectedToken("token-0")).toBe(true); | |
| expect(isRejectedToken(`token-${MAX_REJECTED_TOKENS - 1}`)).toBe(true); | |
| }); | |
| }); | |