Back to Repositories

Testing Content Ranking Algorithm in 30-seconds-of-code

This test suite evaluates the Ranker utility’s content ranking functionality in the 30-seconds-of-code repository. It focuses on testing the rankIndexableContent method which calculates content scores based on keyword weights and limitations. The tests verify proper score calculation, maximum thresholds, and keyword count constraints.

Test Coverage Overview

The test suite provides comprehensive coverage of the Ranker.rankIndexableContent method’s core functionality.

  • Tests basic keyword scoring with multiple terms
  • Verifies maximum score threshold enforcement
  • Validates keyword count limit handling
  • Covers edge cases with predefined keyword scores

Implementation Analysis

The testing approach utilizes Jest’s describe/it structure for organized test cases. The implementation leverages precise floating-point comparisons with toBe() assertions to validate scoring accuracy. The tests are structured to isolate specific ranking scenarios while maintaining a clear separation of concerns.

  • Modular test case organization
  • Direct assertion patterns
  • Controlled test data preparation

Technical Details

  • Testing Framework: Jest/Vitest
  • Test Style: Unit tests
  • Assertion Methods: expect().toBe()
  • Test Setup: Predefined keyword score dictionary
  • Module Structure: ES6 imports

Best Practices Demonstrated

The test suite exemplifies several testing best practices, including isolated test cases, clear test descriptions, and comprehensive edge case coverage. Each test focuses on a specific aspect of the ranking functionality while maintaining readability and maintainability.

  • Descriptive test case naming
  • Isolated test scenarios
  • Comprehensive edge case handling
  • Clear test data setup

chalarangelo/30-seconds-of-code

spec/lib/contentUtils/ranker.test.js

            
import { describe, it, expect } from 'vitest';
import Ranker from '#src/lib/contentUtils/ranker.js';

describe('Ranker.rankIndexableContent', () => {
  Ranker.keywordScores = {
    foo: 2,
    bar: 4,
    baz: 1,
    sup: 98,
    abc: 1,
    bcd: 1,
    cde: 1,
    def: 1,
    efg: 1,
    ghi: 1,
    hij: 1,
    ijk: 1,
    jkl: 1,
    klm: 1,
    lmn: 1,
    mno: 1,
    nop: 1,
    opq: 1,
    pqr: 1,
    qrs: 1,
    rst: 1,
    stu: 1,
    tuv: 1,
    uvw: 1,
    vwx: 1,
    wxy: 1,
    xyz: 1,
  };

  it('should rank indexable content', () => {
    const indexableContent = 'foo bar baz';
    expect(Ranker.rankIndexableContent(indexableContent)).toBe(0.07);
  });

  it('should not exceed the keyword score limit', () => {
    const indexableContent = 'sup bar';
    expect(Ranker.rankIndexableContent(indexableContent)).toBe(1);
  });

  it('should not exceed the keyword count limit', () => {
    const indexableContent =
      'abc bcd cde def efg ghi hij ijk jkl klm lmn mno nop opq pqr qrs rst stu tuv uvw vwx wxy xyz';
    expect(Ranker.rankIndexableContent(indexableContent)).toBe(0.2);
  });
});