|
| 1 | +import { describe, expect, test } from "vitest"; |
| 2 | +import { chunkText } from "./chunkText"; |
| 3 | + |
| 4 | +describe("chunkText", () => { |
| 5 | + test("splits text into chunks of the given size", () => { |
| 6 | + expect(chunkText("abcdefgh", 3)).toEqual(["abc", "def", "gh"]); |
| 7 | + expect(chunkText("hello", 5)).toEqual(["hello"]); |
| 8 | + expect(chunkText("hello", 10)).toEqual(["hello"]); |
| 9 | + expect(chunkText("", 3)).toEqual([]); |
| 10 | + }); |
| 11 | + |
| 12 | + const text = "Hello world. How are you? I am fine."; |
| 13 | + |
| 14 | + test("preserves sentence shorter than chunk size", () => { |
| 15 | + const chunks = chunkText( |
| 16 | + text, |
| 17 | + 5, // shorter than sentence |
| 18 | + { preserveOnBreak: "sentence" } |
| 19 | + ); |
| 20 | + expect(chunks).toEqual([ |
| 21 | + "Hello", |
| 22 | + " worl", |
| 23 | + "d. ", |
| 24 | + "How a", |
| 25 | + "re yo", |
| 26 | + "u? ", |
| 27 | + "I am ", |
| 28 | + "fine.", |
| 29 | + ]); |
| 30 | + expect(chunks.some((chunk) => chunk.length > 5)).toBe(false); |
| 31 | + }); |
| 32 | + |
| 33 | + test("preserves sentence shorter than chunk size", () => { |
| 34 | + const chunks = chunkText(text, 25, { preserveOnBreak: "sentence" }); |
| 35 | + expect(chunks).toEqual(["Hello world. ", "How are you? I am fine."]); |
| 36 | + expect(chunks.some((chunk) => chunk.length > 25)).toBe(false); |
| 37 | + }); |
| 38 | + |
| 39 | + test("preserves words shorter than chunk size", () => { |
| 40 | + const chunks = chunkText(text, 25, { preserveOnBreak: "word" }); |
| 41 | + expect(chunks).toEqual(["Hello world. How are you?", " I am fine."]); |
| 42 | + expect(chunks.some((chunk) => chunk.length > 25)).toBe(false); |
| 43 | + }); |
| 44 | +}); |
0 commit comments