|
| 1 | +# Testing Guide for Feed Processing Functions |
| 2 | + |
| 3 | +This document describes the refactored, testable feed processing utilities and how to run tests. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The feed processing logic has been extracted into small, testable functions in `src/lib/`: |
| 8 | + |
| 9 | +- **`articleUtils.js`** - Pure functions for article hashing and scoring (no external dependencies) |
| 10 | +- **`feedUtils.js`** - Utility functions for feed processing (headers, Redis keys, validation, etc.) |
| 11 | + |
| 12 | +## Running Tests |
| 13 | + |
| 14 | +```bash |
| 15 | +node src/lib/feedUtils.test.js |
| 16 | +``` |
| 17 | + |
| 18 | +All tests use the test data in `testdata/test-cases.json` which contains expected inputs and outputs generated from the actual Node.js implementation. |
| 19 | + |
| 20 | +## Test Coverage |
| 21 | + |
| 22 | +### Article Functions (`articleUtils.js`) |
| 23 | + |
| 24 | +1. **`hash(article)`** - MD5 hash of article GUID |
| 25 | + - Tests: 3 test cases verifying hash consistency |
| 26 | + - Implementation matches `src/articles.js` exactly |
| 27 | + |
| 28 | +2. **`score(article)`** - Unix timestamp score |
| 29 | + - Tests: 3 test cases with different date field names (pubDate, pubdate, date) |
| 30 | + - Implementation matches `src/articles.js` exactly |
| 31 | + |
| 32 | +### Feed Functions (`feedUtils.js`) |
| 33 | + |
| 34 | +1. **`buildRequestHeaders(storedFeed)`** - Builds HTTP headers for conditional GET |
| 35 | + - Tests: 4 test cases (no headers, If-Modified-Since, If-None-Match, both) |
| 36 | + |
| 37 | +2. **`buildRedisKeys(feedURI)`** - Creates Redis key names |
| 38 | + - Tests: 2 test cases with different feed URLs |
| 39 | + |
| 40 | +3. **`buildArticleKey(hash)`** - Creates article key for Redis sorted set |
| 41 | + - Tests: 1 test case verifying format |
| 42 | + |
| 43 | +4. **`processArticle(article, feedURI, hashFn, scoreFn)`** - Adds computed fields |
| 44 | + - Tests: 1 test case verifying hash, score, and feedurl are added |
| 45 | + |
| 46 | +5. **`shouldStoreArticle(oldScore, newScore)`** - Determines if article needs S3 storage |
| 47 | + - Tests: 4 test cases (new article, changed score, unchanged score, type coercion) |
| 48 | + |
| 49 | +6. **`isValidArticle(article)`** - Validates article has required fields |
| 50 | + - Tests: 4 test cases (valid, missing guid, missing description, null) |
| 51 | + |
| 52 | +7. **`extractFeedMetadata(meta)`** - Extracts title and link from parser meta |
| 53 | + - Tests: 1 test case |
| 54 | + |
| 55 | +8. **`extractArticleIds(articleKeys)`** - Strips "article:" prefix from Redis keys |
| 56 | + - Tests: 1 test case |
| 57 | + |
| 58 | +## Test Data Format |
| 59 | + |
| 60 | +The `testdata/test-cases.json` file contains test cases organized by function: |
| 61 | + |
| 62 | +```json |
| 63 | +{ |
| 64 | + "hash_function_tests": [...], |
| 65 | + "score_function_tests": [...], |
| 66 | + "request_headers_tests": [...], |
| 67 | + ... |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +Each test case has: |
| 72 | +- `description` - Human-readable test description |
| 73 | +- `input` - Input value(s) for the function |
| 74 | +- `expected` - Expected output value |
| 75 | + |
| 76 | +## Adding New Tests |
| 77 | + |
| 78 | +1. Add test data to `testdata/test-cases.json` |
| 79 | +2. Add corresponding test code in `src/lib/feedUtils.test.js` |
| 80 | +3. Run tests to verify |
| 81 | + |
| 82 | +## Future Work |
| 83 | + |
| 84 | +Next steps: |
| 85 | +1. Refactor `src/feeds.js` to use these utility functions |
| 86 | +2. Add integration tests for Redis and S3 operations |
| 87 | +3. Create Go implementation with matching behavior (in `feedfetcher/` directory) |
| 88 | +4. Create Go tests that use the same `testdata/test-cases.json` file |
| 89 | + |
| 90 | +## Why These Functions? |
| 91 | + |
| 92 | +These functions were extracted because they are: |
| 93 | +1. **Pure or nearly pure** - Deterministic output for given input |
| 94 | +2. **Core business logic** - Critical for feed processing correctness |
| 95 | +3. **Reusable** - Can be used by both Node.js and Go implementations |
| 96 | +4. **Independently testable** - No mocking of Redis/S3 needed |
| 97 | + |
| 98 | +The goal is to ensure both Node.js and Go implementations produce identical results for: |
| 99 | +- Article hashing (critical for deduplication) |
| 100 | +- Article scoring (critical for sorting) |
| 101 | +- Request headers (critical for conditional GET optimization) |
| 102 | +- Redis key naming (critical for data storage) |
| 103 | +- S3 storage decisions (critical for performance) |
0 commit comments