|
| 1 | +import { jest } from "@jest/globals"; |
| 2 | +import * as charStream from "./charStream"; |
| 3 | +import { Typescript } from "./languages"; |
| 4 | + |
| 5 | +describe("charStream", () => { |
| 6 | + let mockFullStop: jest.Mock; |
| 7 | + |
| 8 | + async function getCharGenerator(chars: string[]) { |
| 9 | + return (async function* () { |
| 10 | + for (const char of chars) { |
| 11 | + yield char; |
| 12 | + } |
| 13 | + })(); |
| 14 | + } |
| 15 | + |
| 16 | + async function getFilteredChars(results: AsyncGenerator<string>) { |
| 17 | + const output = []; |
| 18 | + for await (const char of results) { |
| 19 | + output.push(char); |
| 20 | + } |
| 21 | + return output; |
| 22 | + } |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + mockFullStop = jest.fn(); |
| 26 | + }); |
| 27 | + |
| 28 | + describe("onlyWhitespaceAfterEndOfLine", () => { |
| 29 | + const endOfLineChar = Typescript.endOfLine[0]; |
| 30 | + |
| 31 | + it("should stop at end of line if non-whitespace follows", async () => { |
| 32 | + const charGenerator = await getCharGenerator([ |
| 33 | + `Hello${endOfLineChar}World`, |
| 34 | + ]); |
| 35 | + |
| 36 | + const result = charStream.onlyWhitespaceAfterEndOfLine( |
| 37 | + charGenerator, |
| 38 | + [endOfLineChar], |
| 39 | + mockFullStop, |
| 40 | + ); |
| 41 | + const filteredChars = await getFilteredChars(result); |
| 42 | + |
| 43 | + expect(filteredChars.join("")).toBe(`Hello${endOfLineChar}`); |
| 44 | + expect(mockFullStop).toHaveBeenCalledTimes(1); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should continue past end of line if only whitespace follows", async () => { |
| 48 | + const charGenerator = await getCharGenerator([ |
| 49 | + `Hello${endOfLineChar} World`, |
| 50 | + ]); |
| 51 | + const result = charStream.onlyWhitespaceAfterEndOfLine( |
| 52 | + charGenerator, |
| 53 | + [endOfLineChar], |
| 54 | + mockFullStop, |
| 55 | + ); |
| 56 | + const filteredChars = await getFilteredChars(result); |
| 57 | + expect(filteredChars.join("")).toBe(`Hello${endOfLineChar} World`); |
| 58 | + expect(mockFullStop).not.toHaveBeenCalled(); |
| 59 | + }); |
| 60 | + |
| 61 | + it("should handle end of line at the end of chunk", async () => { |
| 62 | + const charGenerator = await getCharGenerator([ |
| 63 | + `Hello${endOfLineChar}`, |
| 64 | + "World", |
| 65 | + ]); |
| 66 | + const result = charStream.onlyWhitespaceAfterEndOfLine( |
| 67 | + charGenerator, |
| 68 | + [endOfLineChar], |
| 69 | + mockFullStop, |
| 70 | + ); |
| 71 | + const filteredChars = await getFilteredChars(result); |
| 72 | + expect(filteredChars.join("")).toBe(`Hello${endOfLineChar}`); |
| 73 | + expect(mockFullStop).toHaveBeenCalledTimes(1); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe("noFirstCharNewline", () => { |
| 78 | + it("should remove leading newline", async () => { |
| 79 | + const charGenerator = await getCharGenerator(["\nHello"]); |
| 80 | + const result = charStream.noFirstCharNewline(charGenerator); |
| 81 | + const filteredChars = await getFilteredChars(result); |
| 82 | + expect(filteredChars.join("")).toBe(""); |
| 83 | + }); |
| 84 | + |
| 85 | + it("should keep content if no leading newline", async () => { |
| 86 | + const charGenerator = await getCharGenerator(["Hello\nWorld"]); |
| 87 | + const result = charStream.noFirstCharNewline(charGenerator); |
| 88 | + const filteredChars = await getFilteredChars(result); |
| 89 | + expect(filteredChars.join("")).toBe("Hello\nWorld"); |
| 90 | + }); |
| 91 | + |
| 92 | + it("should remove leading carriage return", async () => { |
| 93 | + const charGenerator = await getCharGenerator(["\rHello"]); |
| 94 | + const result = charStream.noFirstCharNewline(charGenerator); |
| 95 | + const filteredChars = await getFilteredChars(result); |
| 96 | + expect(filteredChars.join("")).toBe(""); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe("stopAtStopTokens", () => { |
| 101 | + it("should stop at the first occurrence of a stop token", async () => { |
| 102 | + const charGenerator = await getCharGenerator(["Hello<|endoftext|>World"]); |
| 103 | + const result = charStream.stopAtStopTokens(charGenerator, [ |
| 104 | + "<|endoftext|>", |
| 105 | + ]); |
| 106 | + const filteredChars = await getFilteredChars(result); |
| 107 | + expect(filteredChars.join("")).toBe("Hello"); |
| 108 | + }); |
| 109 | + |
| 110 | + it("should return all content if no stop tokens are provided", async () => { |
| 111 | + const charGenerator = await getCharGenerator(["Hello<|endoftext|>World"]); |
| 112 | + const result = charStream.stopAtStopTokens(charGenerator, []); |
| 113 | + const filteredChars = await getFilteredChars(result); |
| 114 | + expect(filteredChars.join("")).toBe("Hello<|endoftext|>World"); |
| 115 | + }); |
| 116 | + |
| 117 | + it("should handle stop tokens that span multiple chunks", async () => { |
| 118 | + const charGenerator = await getCharGenerator([ |
| 119 | + "Hello<|", |
| 120 | + "endoftext|>World", |
| 121 | + ]); |
| 122 | + const result = charStream.stopAtStopTokens(charGenerator, [ |
| 123 | + "<|endoftext|>", |
| 124 | + ]); |
| 125 | + const filteredChars = await getFilteredChars(result); |
| 126 | + expect(filteredChars.join("")).toBe("Hello"); |
| 127 | + }); |
| 128 | + |
| 129 | + it("should yield remaining characters in buffer if no stop token is found", async () => { |
| 130 | + const charGenerator = await getCharGenerator(["Hello", "World"]); |
| 131 | + const result = charStream.stopAtStopTokens(charGenerator, [ |
| 132 | + "<|endoftext|>", |
| 133 | + ]); |
| 134 | + const filteredChars = await getFilteredChars(result); |
| 135 | + expect(filteredChars.join("")).toBe("HelloWorld"); |
| 136 | + }); |
| 137 | + }); |
| 138 | +}); |
0 commit comments