|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:pub_dev/service/download_counts/package_trends.dart'; |
| 6 | +import 'package:test/test.dart'; |
| 7 | + |
| 8 | +void main() { |
| 9 | + group('calculateLinearRegressionSlope', () { |
| 10 | + test('correctly calculates slope for chronological data', () { |
| 11 | + expect(calculateLinearRegressionSlope([10.0, 20.0, 30.0]), 10.0); |
| 12 | + expect(calculateLinearRegressionSlope([30.0, 20.0, 10.0]), -10.0); |
| 13 | + expect(calculateLinearRegressionSlope([10.0, 10.0, 10.0]), 0); |
| 14 | + }); |
| 15 | + |
| 16 | + test('return 0.0 if denominator is very small', () { |
| 17 | + expect(calculateLinearRegressionSlope([]), 0.0); |
| 18 | + expect(calculateLinearRegressionSlope([10.0]), 0.0); |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + group('computeRelativeGrowthRate', () { |
| 23 | + test('returns 0.0 for stable downloads meeting threshold', () { |
| 24 | + final downloads = List<int>.generate(analysisWindowDays, (i) => 2000); |
| 25 | + expect(computeRelativeGrowthRate(downloads), 0.0); |
| 26 | + }); |
| 27 | + |
| 28 | + test('calculates positive relative growth rate for positive trend', () { |
| 29 | + // Input list (newest first): [1645, 1635, ..., 1355] (30 values) |
| 30 | + // Average = 1500 for the first 30 values. Slope: 10. |
| 31 | + final downloads = |
| 32 | + List<int>.generate(analysisWindowDays * 2, (i) => 1645 - (i * 10)); |
| 33 | + final expectedRate = 10.0 / 1500.0; |
| 34 | + expect(computeRelativeGrowthRate(downloads), expectedRate); |
| 35 | + }); |
| 36 | + |
| 37 | + test('calculates negative relative growth rate for negative trend', () { |
| 38 | + // Input list (newest first): [1355, 1365, ..., 1645] |
| 39 | + // Average = 1500. Slope: -10. |
| 40 | + final downloads = |
| 41 | + List<int>.generate(analysisWindowDays, (i) => 1355 + (i * 10)); |
| 42 | + final expectedRate = -10.0 / 1500.0; |
| 43 | + expect(computeRelativeGrowthRate(downloads), expectedRate); |
| 44 | + }); |
| 45 | + |
| 46 | + test( |
| 47 | + 'calculates positive relative growth for data barely meeting threshold', |
| 48 | + () { |
| 49 | + // Input list (newest first): [1016, 1015, ..., 987] |
| 50 | + // Average: 1001.5. Slope: 1. |
| 51 | + final downloads = |
| 52 | + List<int>.generate(analysisWindowDays, (i) => 1016 - i * 1); |
| 53 | + final expectedRate = 1.0 / 1001.5; |
| 54 | + expect(computeRelativeGrowthRate(downloads), closeTo(expectedRate, 1e-9)); |
| 55 | + }); |
| 56 | + |
| 57 | + test('should handle fluctuating data with a slight positive overall trend', |
| 58 | + () { |
| 59 | + // Newest first. Average 1135. |
| 60 | + final downloads = <int>[ |
| 61 | + 1300, |
| 62 | + 1250, |
| 63 | + 1280, |
| 64 | + 1230, |
| 65 | + 1260, |
| 66 | + 1210, |
| 67 | + 1240, |
| 68 | + 1190, |
| 69 | + 1220, |
| 70 | + 1170, |
| 71 | + 1200, |
| 72 | + 1150, |
| 73 | + 1180, |
| 74 | + 1130, |
| 75 | + 1160, |
| 76 | + 1110, |
| 77 | + 1140, |
| 78 | + 1090, |
| 79 | + 1120, |
| 80 | + 1070, |
| 81 | + 1100, |
| 82 | + 1050, |
| 83 | + 1080, |
| 84 | + 1030, |
| 85 | + 1060, |
| 86 | + 1010, |
| 87 | + 1040, |
| 88 | + 990, |
| 89 | + 1020, |
| 90 | + 970 |
| 91 | + ]; |
| 92 | + final expectedRate = 683250.0 / 67425.0 / 1135.0; |
| 93 | + expect(computeRelativeGrowthRate(downloads), expectedRate); |
| 94 | + }); |
| 95 | + }); |
| 96 | +} |
0 commit comments