• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

super3 / feed / 16857609330

10 Aug 2025 05:11AM UTC coverage: 95.372% (+1.1%) from 94.312%
16857609330

push

github

developeratexample
Fix code coverage by adding config.js tests

- Added comprehensive tests for lib/config.js module
- Achieved 100% coverage for config.js
- All coverage thresholds now met:
  - Functions: 98.33% (threshold: 80%)
  - Statements: 97.50% (threshold: 80%)
  - Branches: 92.65% (threshold: 78%)
  - Lines: 97.35% (threshold: 80%)
- All 114 tests passing across 6 test suites

224 of 244 branches covered (91.8%)

Branch coverage included in aggregate %.

353 of 361 relevant lines covered (97.78%)

9.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.36
/api/cron/fetch-posts.js
1
const { getStorage } = require('../../lib/storage');
1✔
2
const { methodNotAllowed, serverError } = require('../../lib/utils/error-handler');
1✔
3
const { fetchRedditPosts } = require('../../lib/reddit-client');
1✔
4
const config = require('../../lib/config');
1✔
5

6
module.exports = async (req, res) => {
1✔
7
  if (req.method !== 'POST' && req.method !== 'GET') {
16!
8
    return methodNotAllowed(res, ['GET', 'POST']);
×
9
  }
10

11
  try {
16✔
12
    console.log('Cron job triggered at:', new Date().toISOString());
16✔
13
    console.log('Authorization:', req.headers.authorization ? 'Present' : 'Not present');
16✔
14
    
15
    // Optional security check for cron jobs
16
    if (config.security.hasCronSecret) {
16✔
17
      const authHeader = req.headers.authorization;
2✔
18
      const providedSecret = authHeader?.replace('Bearer ', '');
2✔
19
      
20
      if (providedSecret !== config.security.cronSecret) {
2✔
21
        return res.status(401).json({ error: 'Unauthorized' });
1✔
22
      }
23
    }
24
    
25
    const storage = getStorage();
15✔
26
    console.log('Storage type:', storage.type);
15✔
27
    console.log('Redis type:', storage.redisType);
15✔
28
    
29
    await storage.init();
15✔
30
    
31
    // Get keywords from storage or use default
32
    const keywordsKey = config.keys.keywords;
14✔
33
    let keywords = await storage.get(keywordsKey);
14✔
34
    console.log('Retrieved keywords from storage:', keywords);
14✔
35
    
36
    if (!keywords || keywords.length === 0) {
14✔
37
      console.log(`No keywords found, setting default: ${config.reddit.defaultKeyword}`);
1✔
38
      keywords = [config.reddit.defaultKeyword]; // Default keyword
1✔
39
      await storage.set(keywordsKey, keywords);
1✔
40
    }
41

42
    const results = {};
14✔
43
    let totalNewPosts = 0;
14✔
44
    
45
    // Fetch posts for each keyword
46
    for (const keywordItem of keywords) {
14✔
47
      // Handle both string and object formats
48
      const keyword = typeof keywordItem === 'string' ? keywordItem : keywordItem.keyword;
15✔
49
      
50
      try {
15✔
51
        const result = await fetchRedditPosts(keyword, storage);
15✔
52
        results[keyword] = {
10✔
53
          success: true,
54
          newPosts: result.newPosts,
55
          totalFound: result.totalFound,
56
          key: result.key
57
        };
58
        totalNewPosts += result.newPosts;
10✔
59
      } catch (error) {
60
        console.error(`Error fetching posts for ${keyword}:`, error);
5✔
61
        results[keyword] = {
5✔
62
          success: false,
63
          error: error.message
64
        };
65
      }
66
    }
67

68
    console.log(`Cron job completed. Total new posts: ${totalNewPosts}`);
14✔
69

70
    res.status(200).json({
14✔
71
      success: true,
72
      timestamp: new Date().toISOString(),
73
      keywords: keywords,
74
      totalNewPosts: totalNewPosts,
75
      results: results
76
    });
77
  } catch (error) {
78
    serverError(res, error, { context: 'Cron job failed' });
1✔
79
  }
80
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc