@@ -32,7 +32,7 @@ import { createFixture } from 'fs-fixture';
3232import { isDirectorySync } from 'path-type' ;
3333import { glob } from 'tinyglobby' ;
3434import { z } from 'zod' ;
35- import { CLAUDE_CONFIG_DIR_ENV , CLAUDE_PROJECTS_DIR_NAME , CONTEXT_LOW_THRESHOLD_ENV , CONTEXT_MEDIUM_THRESHOLD_ENV , DEFAULT_CLAUDE_CODE_PATH , DEFAULT_CLAUDE_CONFIG_PATH , DEFAULT_CONTEXT_USAGE_THRESHOLDS , USAGE_DATA_GLOB_PATTERN , USER_HOME_DIR } from './_consts.ts' ;
35+ import { CLAUDE_CONFIG_DIR_ENV , CLAUDE_PROJECTS_DIR_NAME , DEFAULT_CLAUDE_CODE_PATH , DEFAULT_CLAUDE_CONFIG_PATH , USAGE_DATA_GLOB_PATTERN , USER_HOME_DIR } from './_consts.ts' ;
3636import {
3737 identifySessionBlocks ,
3838} from './_session-blocks.ts' ;
@@ -133,48 +133,6 @@ export function getClaudePaths(): string[] {
133133 return paths ;
134134}
135135
136- /**
137- * Get context usage percentage thresholds for color coding
138- * Can be configured via environment variables or uses defaults
139- * Validates and clamps values to 0-100 range and enforces LOW < MEDIUM ordering
140- * @returns Context usage thresholds with LOW and MEDIUM percentages
141- */
142- export function getContextUsageThresholds ( ) : { readonly LOW : number ; readonly MEDIUM : number } {
143- // Parse and validate environment variables
144- const lowThresholdStr = process . env [ CONTEXT_LOW_THRESHOLD_ENV ] ;
145- const mediumThresholdStr = process . env [ CONTEXT_MEDIUM_THRESHOLD_ENV ] ;
146-
147- // Parse with validation - use defaults for invalid values
148- let lowThreshold : number = DEFAULT_CONTEXT_USAGE_THRESHOLDS . LOW ;
149- let mediumThreshold : number = DEFAULT_CONTEXT_USAGE_THRESHOLDS . MEDIUM ;
150-
151- if ( lowThresholdStr != null ) {
152- const parsed = Number . parseInt ( lowThresholdStr , 10 ) ;
153- if ( ! Number . isNaN ( parsed ) ) {
154- lowThreshold = Math . max ( 0 , Math . min ( 100 , parsed ) ) ; // Clamp to 0-100
155- }
156- }
157-
158- if ( mediumThresholdStr != null ) {
159- const parsed = Number . parseInt ( mediumThresholdStr , 10 ) ;
160- if ( ! Number . isNaN ( parsed ) ) {
161- mediumThreshold = Math . max ( 0 , Math . min ( 100 , parsed ) ) ; // Clamp to 0-100
162- }
163- }
164-
165- // Enforce ordering: LOW must be less than MEDIUM
166- // If ordering is violated, reset both to defaults
167- if ( lowThreshold >= mediumThreshold ) {
168- lowThreshold = DEFAULT_CONTEXT_USAGE_THRESHOLDS . LOW ;
169- mediumThreshold = DEFAULT_CONTEXT_USAGE_THRESHOLDS . MEDIUM ;
170- }
171-
172- return {
173- LOW : lowThreshold ,
174- MEDIUM : mediumThreshold ,
175- } as const ;
176- }
177-
178136/**
179137 * Extract project name from Claude JSONL file path
180138 * @param jsonlPath - Absolute path to JSONL file
@@ -4775,108 +4733,4 @@ if (import.meta.vitest != null) {
47754733 expect ( res ?. percentage ) . toBe ( 100 ) ; // Should be clamped to 100
47764734 } ) ;
47774735 } ) ;
4778-
4779- describe ( 'getContextUsageThresholds' , ( ) => {
4780- afterEach ( ( ) => {
4781- vi . unstubAllEnvs ( ) ;
4782- } ) ;
4783-
4784- it ( 'returns default values when no environment variables are set' , ( ) => {
4785- const thresholds = getContextUsageThresholds ( ) ;
4786- expect ( thresholds . LOW ) . toBe ( 50 ) ;
4787- expect ( thresholds . MEDIUM ) . toBe ( 80 ) ;
4788- } ) ;
4789-
4790- it ( 'parses valid environment variables correctly' , ( ) => {
4791- vi . stubEnv ( 'CCUSAGE_CONTEXT_LOW_THRESHOLD' , '30' ) ;
4792- vi . stubEnv ( 'CCUSAGE_CONTEXT_MEDIUM_THRESHOLD' , '70' ) ;
4793-
4794- const thresholds = getContextUsageThresholds ( ) ;
4795- expect ( thresholds . LOW ) . toBe ( 30 ) ;
4796- expect ( thresholds . MEDIUM ) . toBe ( 70 ) ;
4797- } ) ;
4798-
4799- it ( 'clamps values to 0-100 range' , ( ) => {
4800- vi . stubEnv ( 'CCUSAGE_CONTEXT_LOW_THRESHOLD' , '-10' ) ;
4801- vi . stubEnv ( 'CCUSAGE_CONTEXT_MEDIUM_THRESHOLD' , '150' ) ;
4802-
4803- const thresholds = getContextUsageThresholds ( ) ;
4804- expect ( thresholds . LOW ) . toBe ( 0 ) ;
4805- expect ( thresholds . MEDIUM ) . toBe ( 100 ) ;
4806- } ) ;
4807-
4808- it ( 'handles non-numeric values by falling back to defaults' , ( ) => {
4809- vi . stubEnv ( 'CCUSAGE_CONTEXT_LOW_THRESHOLD' , 'invalid' ) ;
4810- vi . stubEnv ( 'CCUSAGE_CONTEXT_MEDIUM_THRESHOLD' , 'not-a-number' ) ;
4811-
4812- const thresholds = getContextUsageThresholds ( ) ;
4813- expect ( thresholds . LOW ) . toBe ( 50 ) ;
4814- expect ( thresholds . MEDIUM ) . toBe ( 80 ) ;
4815- } ) ;
4816-
4817- it ( 'handles empty string values by falling back to defaults' , ( ) => {
4818- vi . stubEnv ( 'CCUSAGE_CONTEXT_LOW_THRESHOLD' , '' ) ;
4819- vi . stubEnv ( 'CCUSAGE_CONTEXT_MEDIUM_THRESHOLD' , '' ) ;
4820-
4821- const thresholds = getContextUsageThresholds ( ) ;
4822- expect ( thresholds . LOW ) . toBe ( 50 ) ;
4823- expect ( thresholds . MEDIUM ) . toBe ( 80 ) ;
4824- } ) ;
4825-
4826- it ( 'handles partial invalid values correctly' , ( ) => {
4827- vi . stubEnv ( 'CCUSAGE_CONTEXT_LOW_THRESHOLD' , '25' ) ;
4828- vi . stubEnv ( 'CCUSAGE_CONTEXT_MEDIUM_THRESHOLD' , 'invalid' ) ;
4829-
4830- const thresholds = getContextUsageThresholds ( ) ;
4831- expect ( thresholds . LOW ) . toBe ( 25 ) ;
4832- expect ( thresholds . MEDIUM ) . toBe ( 80 ) ; // Falls back to default
4833- } ) ;
4834-
4835- it ( 'enforces ordering constraint: resets to defaults when LOW >= MEDIUM' , ( ) => {
4836- vi . stubEnv ( 'CCUSAGE_CONTEXT_LOW_THRESHOLD' , '85' ) ;
4837- vi . stubEnv ( 'CCUSAGE_CONTEXT_MEDIUM_THRESHOLD' , '60' ) ;
4838-
4839- const thresholds = getContextUsageThresholds ( ) ;
4840- // Should reset both to defaults due to ordering violation
4841- expect ( thresholds . LOW ) . toBe ( 50 ) ;
4842- expect ( thresholds . MEDIUM ) . toBe ( 80 ) ;
4843- } ) ;
4844-
4845- it ( 'enforces ordering constraint: resets to defaults when LOW equals MEDIUM' , ( ) => {
4846- vi . stubEnv ( 'CCUSAGE_CONTEXT_LOW_THRESHOLD' , '70' ) ;
4847- vi . stubEnv ( 'CCUSAGE_CONTEXT_MEDIUM_THRESHOLD' , '70' ) ;
4848-
4849- const thresholds = getContextUsageThresholds ( ) ;
4850- // Should reset both to defaults due to ordering violation
4851- expect ( thresholds . LOW ) . toBe ( 50 ) ;
4852- expect ( thresholds . MEDIUM ) . toBe ( 80 ) ;
4853- } ) ;
4854-
4855- it ( 'handles edge case: LOW=0, MEDIUM=100' , ( ) => {
4856- vi . stubEnv ( 'CCUSAGE_CONTEXT_LOW_THRESHOLD' , '0' ) ;
4857- vi . stubEnv ( 'CCUSAGE_CONTEXT_MEDIUM_THRESHOLD' , '100' ) ;
4858-
4859- const thresholds = getContextUsageThresholds ( ) ;
4860- expect ( thresholds . LOW ) . toBe ( 0 ) ;
4861- expect ( thresholds . MEDIUM ) . toBe ( 100 ) ;
4862- } ) ;
4863-
4864- it ( 'handles decimal values by truncating to integer' , ( ) => {
4865- vi . stubEnv ( 'CCUSAGE_CONTEXT_LOW_THRESHOLD' , '25.7' ) ;
4866- vi . stubEnv ( 'CCUSAGE_CONTEXT_MEDIUM_THRESHOLD' , '75.9' ) ;
4867-
4868- const thresholds = getContextUsageThresholds ( ) ;
4869- expect ( thresholds . LOW ) . toBe ( 25 ) ; // parseInt truncates
4870- expect ( thresholds . MEDIUM ) . toBe ( 75 ) ; // parseInt truncates
4871- } ) ;
4872-
4873- it ( 'handles values with extra whitespace' , ( ) => {
4874- vi . stubEnv ( 'CCUSAGE_CONTEXT_LOW_THRESHOLD' , ' 30 ' ) ;
4875- vi . stubEnv ( 'CCUSAGE_CONTEXT_MEDIUM_THRESHOLD' , ' 70 ' ) ;
4876-
4877- const thresholds = getContextUsageThresholds ( ) ;
4878- expect ( thresholds . LOW ) . toBe ( 30 ) ;
4879- expect ( thresholds . MEDIUM ) . toBe ( 70 ) ;
4880- } ) ;
4881- } ) ;
48824736}
0 commit comments