api/tests/plexus.test.ts
45 lines · 1661 bytes
1// Unit tests for Plexus de-Googled rating aggregation
2import { describe, it, expect } from 'vitest'
3import { summarizeRatings, type PlexusRating } from '@/lib/plexus'
4
5const r = (type: string, num: number): PlexusRating => ({
6 rating_type: type,
7 score: { numerator: num, denominator: 4 },
8})
9
10describe('summarizeRatings', () => {
11 it('maps perfect native scores to gold', () => {
12 const out = summarizeRatings([r('native', 4), r('native', 4)])
13 expect(out.native).toMatchObject({
14 available: true,
15 status: 'gold',
16 score: 4,
17 ratings: 2,
18 })
19 expect(out.microg).toEqual({ available: false })
20 })
21
22 it('grades silver, bronze and broken by average score', () => {
23 expect(summarizeRatings([r('native', 3)]).native).toMatchObject({ status: 'silver' })
24 expect(summarizeRatings([r('native', 2)]).native).toMatchObject({ status: 'bronze' })
25 expect(summarizeRatings([r('native', 1)]).native).toMatchObject({ status: 'broken' })
26 })
27
28 it('separates native from micro_g ratings', () => {
29 const out = summarizeRatings([r('native', 4), r('micro_g', 2)])
30 expect(out.native).toMatchObject({ status: 'gold', ratings: 1 })
31 expect(out.microg).toMatchObject({ status: 'bronze', ratings: 1 })
32 })
33
34 it('marks a type unavailable when it has no ratings', () => {
35 expect(summarizeRatings([]).native).toEqual({ available: false })
36 })
37
38 it('ignores ratings with a missing or zero score', () => {
39 const bad = [
40 { rating_type: 'native' },
41 { rating_type: 'native', score: { denominator: 0 } },
42 ]
43 expect(summarizeRatings(bad as PlexusRating[]).native).toEqual({ available: false })
44 })
45})