/ tests / pipeline / poc.test.js
poc.test.js
  1  /**
  2   * Tests for POC Module
  3   * Tests HTTP status code handling and error scenarios
  4   */
  5  
  6  import { test, describe } from 'node:test';
  7  import assert from 'node:assert';
  8  
  9  describe('POC Module - HTTP Status Handling', () => {
 10    describe('HTTP status code validation', () => {
 11      test('should identify 4xx errors as HTTP errors', () => {
 12        const testCases = [400, 401, 403, 404, 429, 499];
 13  
 14        testCases.forEach(statusCode => {
 15          const isError = statusCode >= 400;
 16          assert.strictEqual(isError, true, `${statusCode} should be treated as error`);
 17        });
 18      });
 19  
 20      test('should identify 5xx errors as HTTP errors', () => {
 21        const testCases = [500, 502, 503, 504];
 22  
 23        testCases.forEach(statusCode => {
 24          const isError = statusCode >= 400;
 25          assert.strictEqual(isError, true, `${statusCode} should be treated as error`);
 26        });
 27      });
 28  
 29      test('should NOT treat 2xx/3xx as errors', () => {
 30        const testCases = [200, 201, 204, 301, 302, 304];
 31  
 32        testCases.forEach(statusCode => {
 33          const isError = statusCode >= 400;
 34          assert.strictEqual(isError, false, `${statusCode} should NOT be treated as error`);
 35        });
 36      });
 37    });
 38  
 39    describe('Error handling logic', () => {
 40      test('should set aside sites with 403 errors for later retry', () => {
 41        const captureResult = {
 42          httpStatusCode: 403,
 43          error: null,
 44          screenshots: {},
 45          html: '<html></html>',
 46        };
 47  
 48        const shouldSkipProcessing = captureResult.httpStatusCode >= 400;
 49        assert.strictEqual(shouldSkipProcessing, true, 'Should skip processing for 403');
 50      });
 51  
 52      test('should set aside sites with 5xx errors for later retry', () => {
 53        const captureResult = {
 54          httpStatusCode: 503,
 55          error: null,
 56          screenshots: {},
 57          html: '<html></html>',
 58        };
 59  
 60        const shouldSkipProcessing = captureResult.httpStatusCode >= 400;
 61        assert.strictEqual(shouldSkipProcessing, true, 'Should skip processing for 503');
 62      });
 63  
 64      test('should process sites with 200 status normally', () => {
 65        const captureResult = {
 66          httpStatusCode: 200,
 67          error: null,
 68          screenshots: {
 69            desktop_above: Buffer.from('test'),
 70          },
 71          html: '<html></html>',
 72        };
 73  
 74        const shouldSkipProcessing = captureResult.httpStatusCode >= 400;
 75        assert.strictEqual(shouldSkipProcessing, false, 'Should NOT skip processing for 200');
 76      });
 77  
 78      test('should handle missing httpStatusCode gracefully', () => {
 79        const captureResult = {
 80          httpStatusCode: null,
 81          error: null,
 82          screenshots: {},
 83          html: '<html></html>',
 84        };
 85  
 86        // Should not crash when httpStatusCode is null
 87        const shouldSkipProcessing = !!(
 88          captureResult.httpStatusCode && captureResult.httpStatusCode >= 400
 89        );
 90        assert.strictEqual(shouldSkipProcessing, false, 'Should handle null status code');
 91      });
 92    });
 93  
 94    describe('Processing flow', () => {
 95      test('should skip scoring if HTTP error detected', () => {
 96        const captureResult = {
 97          httpStatusCode: 403,
 98          error: null,
 99          screenshots: {},
100          screenshotsUncropped: {},
101          html: '<html></html>',
102        };
103  
104        if (captureResult.httpStatusCode && captureResult.httpStatusCode >= 400) {
105          // Should not proceed to scoring
106          const shouldScore = false;
107          assert.strictEqual(shouldScore, false, 'Should not score sites with HTTP errors');
108        }
109      });
110  
111      test('should proceed to scoring if HTTP status is OK', () => {
112        const captureResult = {
113          httpStatusCode: 200,
114          error: null,
115          screenshots: {
116            desktop_above: Buffer.from('test'),
117          },
118          screenshotsUncropped: {
119            desktop_above: Buffer.from('test'),
120          },
121          html: '<html></html>',
122        };
123  
124        if (
125          !captureResult.error &&
126          (!captureResult.httpStatusCode || captureResult.httpStatusCode < 400)
127        ) {
128          const shouldScore = true;
129          assert.strictEqual(shouldScore, true, 'Should score sites with OK status');
130        }
131      });
132    });
133  });
134  
135  describe('Database Storage', () => {
136    describe('Column existence', () => {
137      test('should include http_status_code in INSERT statement', () => {
138        const columns = [
139          'domain',
140          'landing_page_url',
141          'keyword',
142          'screenshot_above_desktop',
143          'screenshot_below_desktop',
144          'screenshot_above_mobile',
145          'screenshot_above_desktop_uncropped',
146          'screenshot_below_desktop_uncropped',
147          'screenshot_above_mobile_uncropped',
148          'html_dom',
149          'http_status_code',
150          'conversion_score_json',
151          'conversion_score',
152        ];
153  
154        assert.ok(columns.includes('http_status_code'), 'Should include http_status_code column');
155        assert.ok(
156          columns.includes('screenshot_above_desktop_uncropped'),
157          'Should include uncropped column'
158        );
159      });
160    });
161  });