|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { ContentProcessingContext } from "../types"; |
| 3 | +import { HtmlDomParserMiddleware } from "./HtmlDomParserMiddleware"; |
| 4 | +import { HtmlPlaywrightMiddleware } from "./HtmlPlaywrightMiddleware"; |
| 5 | +import { HtmlSmartProcessorMiddleware } from "./HtmlSmartProcessorMiddleware"; |
| 6 | + |
| 7 | +// Mock the underlying processors |
| 8 | +vi.mock("./HtmlDomParserMiddleware"); |
| 9 | +vi.mock("./HtmlPlaywrightMiddleware"); |
| 10 | + |
| 11 | +// Get typed mocks |
| 12 | +const MockedHtmlDomParserMiddleware = vi.mocked(HtmlDomParserMiddleware); |
| 13 | +const MockedHtmlPlaywrightMiddleware = vi.mocked(HtmlPlaywrightMiddleware); |
| 14 | + |
| 15 | +describe("HtmlSmartProcessorMiddleware", () => { |
| 16 | + const mockNext = vi.fn().mockResolvedValue(undefined); |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + // Clear mocks before each test to ensure isolation |
| 20 | + MockedHtmlDomParserMiddleware.mockClear(); |
| 21 | + MockedHtmlPlaywrightMiddleware.mockClear(); |
| 22 | + // Also clear any mocks on the process methods if they were added to prototypes (though we'll avoid that now) |
| 23 | + // It's safer to ensure the instances returned by the mock constructor have their method mocks cleared if necessary, |
| 24 | + // but vi.mock usually handles this. Let's rely on constructor mockClear for now. |
| 25 | + mockNext.mockClear(); |
| 26 | + }); |
| 27 | + |
| 28 | + // Helper to create a basic context |
| 29 | + const createContext = ( |
| 30 | + scrapeMode?: "fetch" | "playwright" | "auto", |
| 31 | + ): ContentProcessingContext => ({ |
| 32 | + content: "<html><body>Test</body></html>", |
| 33 | + contentType: "text/html", |
| 34 | + source: "http://example.com", |
| 35 | + metadata: {}, |
| 36 | + links: [], |
| 37 | + errors: [], |
| 38 | + options: { |
| 39 | + url: "http://example.com", |
| 40 | + library: "test-lib", |
| 41 | + version: "1.0.0", |
| 42 | + scrapeMode, // Pass the mode here |
| 43 | + }, |
| 44 | + }); |
| 45 | + |
| 46 | + it("should use HtmlDomParserMiddleware when scrapeMode is 'fetch'", async () => { |
| 47 | + const context = createContext("fetch"); |
| 48 | + const middleware = new HtmlSmartProcessorMiddleware(); |
| 49 | + |
| 50 | + await middleware.process(context, mockNext); |
| 51 | + |
| 52 | + // Verify constructor calls happened during middleware instantiation |
| 53 | + expect(MockedHtmlDomParserMiddleware).toHaveBeenCalledTimes(1); |
| 54 | + expect(MockedHtmlPlaywrightMiddleware).toHaveBeenCalledTimes(1); |
| 55 | + |
| 56 | + // Get the instances created by the constructor |
| 57 | + const domInstance = MockedHtmlDomParserMiddleware.mock.instances[0]; |
| 58 | + const playwrightInstance = MockedHtmlPlaywrightMiddleware.mock.instances[0]; |
| 59 | + |
| 60 | + // Check that the correct instance's process method was called |
| 61 | + expect(domInstance.process).toHaveBeenCalledTimes(1); |
| 62 | + expect(domInstance.process).toHaveBeenCalledWith(context, mockNext); |
| 63 | + expect(playwrightInstance.process).not.toHaveBeenCalled(); |
| 64 | + }); |
| 65 | + |
| 66 | + it("should use HtmlPlaywrightMiddleware when scrapeMode is 'playwright'", async () => { |
| 67 | + const context = createContext("playwright"); |
| 68 | + const middleware = new HtmlSmartProcessorMiddleware(); |
| 69 | + |
| 70 | + await middleware.process(context, mockNext); |
| 71 | + |
| 72 | + // Verify constructor calls happened during middleware instantiation |
| 73 | + expect(MockedHtmlDomParserMiddleware).toHaveBeenCalledTimes(1); |
| 74 | + expect(MockedHtmlPlaywrightMiddleware).toHaveBeenCalledTimes(1); |
| 75 | + |
| 76 | + // Get the instances created by the constructor |
| 77 | + const domInstance = MockedHtmlDomParserMiddleware.mock.instances[0]; |
| 78 | + const playwrightInstance = MockedHtmlPlaywrightMiddleware.mock.instances[0]; |
| 79 | + |
| 80 | + // Check that the correct instance's process method was called |
| 81 | + expect(playwrightInstance.process).toHaveBeenCalledTimes(1); |
| 82 | + expect(playwrightInstance.process).toHaveBeenCalledWith(context, mockNext); |
| 83 | + expect(domInstance.process).not.toHaveBeenCalled(); |
| 84 | + }); |
| 85 | + |
| 86 | + it("should use HtmlPlaywrightMiddleware when scrapeMode is 'auto'", async () => { |
| 87 | + const context = createContext("auto"); |
| 88 | + const middleware = new HtmlSmartProcessorMiddleware(); |
| 89 | + |
| 90 | + await middleware.process(context, mockNext); |
| 91 | + |
| 92 | + // Verify constructor calls happened during middleware instantiation |
| 93 | + expect(MockedHtmlDomParserMiddleware).toHaveBeenCalledTimes(1); |
| 94 | + expect(MockedHtmlPlaywrightMiddleware).toHaveBeenCalledTimes(1); |
| 95 | + |
| 96 | + // Get the instances created by the constructor |
| 97 | + const domInstance = MockedHtmlDomParserMiddleware.mock.instances[0]; |
| 98 | + const playwrightInstance = MockedHtmlPlaywrightMiddleware.mock.instances[0]; |
| 99 | + |
| 100 | + // Check that the correct instance's process method was called |
| 101 | + expect(playwrightInstance.process).toHaveBeenCalledTimes(1); |
| 102 | + expect(playwrightInstance.process).toHaveBeenCalledWith(context, mockNext); |
| 103 | + expect(domInstance.process).not.toHaveBeenCalled(); |
| 104 | + }); |
| 105 | + |
| 106 | + it("should default to 'auto' (Playwright) when scrapeMode is undefined", async () => { |
| 107 | + const context = createContext(undefined); // Explicitly undefined |
| 108 | + const middleware = new HtmlSmartProcessorMiddleware(); |
| 109 | + |
| 110 | + await middleware.process(context, mockNext); |
| 111 | + |
| 112 | + // Verify constructor calls happened during middleware instantiation |
| 113 | + expect(MockedHtmlDomParserMiddleware).toHaveBeenCalledTimes(1); |
| 114 | + expect(MockedHtmlPlaywrightMiddleware).toHaveBeenCalledTimes(1); |
| 115 | + |
| 116 | + // Get the instances created by the constructor |
| 117 | + const domInstance = MockedHtmlDomParserMiddleware.mock.instances[0]; |
| 118 | + const playwrightInstance = MockedHtmlPlaywrightMiddleware.mock.instances[0]; |
| 119 | + |
| 120 | + // Check that the correct instance's process method was called |
| 121 | + expect(playwrightInstance.process).toHaveBeenCalledTimes(1); |
| 122 | + expect(playwrightInstance.process).toHaveBeenCalledWith(context, mockNext); |
| 123 | + expect(domInstance.process).not.toHaveBeenCalled(); |
| 124 | + }); |
| 125 | +}); |
0 commit comments