@@ -41,6 +41,90 @@ describe("getCacheDir", () => {
4141 delete process . env . GITHUB_CODE_SEARCH_CACHE_DIR ;
4242 expect ( getCacheDir ( ) ) . toContain ( "github-code-search" ) ;
4343 } ) ;
44+
45+ it ( "uses LOCALAPPDATA on win32 when the env var is set" , ( ) => {
46+ delete process . env . GITHUB_CODE_SEARCH_CACHE_DIR ;
47+ const originalPlatform = Object . getOwnPropertyDescriptor ( process , "platform" ) ;
48+ const originalLocalAppData = process . env . LOCALAPPDATA ;
49+ try {
50+ Object . defineProperty ( process , "platform" , { value : "win32" , configurable : true } ) ;
51+ process . env . LOCALAPPDATA = "C:\\Users\\user\\AppData\\Local" ;
52+ const dir = getCacheDir ( ) ;
53+ // path.join uses the host OS separator in tests (macOS: /), so we just
54+ // assert both components are present rather than hard-coding a separator.
55+ expect ( dir ) . toContain ( "AppData" ) ;
56+ expect ( dir ) . toContain ( "Local" ) ;
57+ expect ( dir ) . toContain ( "github-code-search" ) ;
58+ } finally {
59+ if ( originalPlatform ) Object . defineProperty ( process , "platform" , originalPlatform ) ;
60+ if ( originalLocalAppData !== undefined ) process . env . LOCALAPPDATA = originalLocalAppData ;
61+ else delete process . env . LOCALAPPDATA ;
62+ }
63+ } ) ;
64+
65+ it ( "falls back to ~/AppData/Local on win32 when LOCALAPPDATA is not set" , ( ) => {
66+ delete process . env . GITHUB_CODE_SEARCH_CACHE_DIR ;
67+ const originalPlatform = Object . getOwnPropertyDescriptor ( process , "platform" ) ;
68+ const originalLocalAppData = process . env . LOCALAPPDATA ;
69+ try {
70+ Object . defineProperty ( process , "platform" , { value : "win32" , configurable : true } ) ;
71+ delete process . env . LOCALAPPDATA ;
72+ const dir = getCacheDir ( ) ;
73+ expect ( dir ) . toContain ( "AppData" ) ;
74+ expect ( dir ) . toContain ( "Local" ) ;
75+ expect ( dir ) . toContain ( "github-code-search" ) ;
76+ } finally {
77+ if ( originalPlatform ) Object . defineProperty ( process , "platform" , originalPlatform ) ;
78+ if ( originalLocalAppData !== undefined ) process . env . LOCALAPPDATA = originalLocalAppData ;
79+ }
80+ } ) ;
81+
82+ it ( "uses XDG_CACHE_HOME on linux when set" , ( ) => {
83+ delete process . env . GITHUB_CODE_SEARCH_CACHE_DIR ;
84+ const originalPlatform = Object . getOwnPropertyDescriptor ( process , "platform" ) ;
85+ const originalXdg = process . env . XDG_CACHE_HOME ;
86+ try {
87+ Object . defineProperty ( process , "platform" , { value : "linux" , configurable : true } ) ;
88+ process . env . XDG_CACHE_HOME = "/custom/xdg/cache" ;
89+ const dir = getCacheDir ( ) ;
90+ expect ( dir ) . toContain ( "custom" ) ;
91+ expect ( dir ) . toContain ( "xdg" ) ;
92+ expect ( dir ) . toContain ( "github-code-search" ) ;
93+ } finally {
94+ if ( originalPlatform ) Object . defineProperty ( process , "platform" , originalPlatform ) ;
95+ if ( originalXdg !== undefined ) process . env . XDG_CACHE_HOME = originalXdg ;
96+ else delete process . env . XDG_CACHE_HOME ;
97+ }
98+ } ) ;
99+
100+ it ( "falls back to ~/.cache on linux when XDG_CACHE_HOME is not set" , ( ) => {
101+ delete process . env . GITHUB_CODE_SEARCH_CACHE_DIR ;
102+ const originalPlatform = Object . getOwnPropertyDescriptor ( process , "platform" ) ;
103+ const originalXdg = process . env . XDG_CACHE_HOME ;
104+ try {
105+ Object . defineProperty ( process , "platform" , { value : "linux" , configurable : true } ) ;
106+ delete process . env . XDG_CACHE_HOME ;
107+ const dir = getCacheDir ( ) ;
108+ expect ( dir ) . toContain ( ".cache" ) ;
109+ expect ( dir ) . toContain ( "github-code-search" ) ;
110+ } finally {
111+ if ( originalPlatform ) Object . defineProperty ( process , "platform" , originalPlatform ) ;
112+ if ( originalXdg !== undefined ) process . env . XDG_CACHE_HOME = originalXdg ;
113+ }
114+ } ) ;
115+
116+ it ( "falls back to ~/.github-code-search/cache on unknown platforms" , ( ) => {
117+ delete process . env . GITHUB_CODE_SEARCH_CACHE_DIR ;
118+ const originalPlatform = Object . getOwnPropertyDescriptor ( process , "platform" ) ;
119+ try {
120+ Object . defineProperty ( process , "platform" , { value : "freebsd" , configurable : true } ) ;
121+ const dir = getCacheDir ( ) ;
122+ expect ( dir ) . toContain ( ".github-code-search" ) ;
123+ expect ( dir ) . toContain ( "cache" ) ;
124+ } finally {
125+ if ( originalPlatform ) Object . defineProperty ( process , "platform" , originalPlatform ) ;
126+ }
127+ } ) ;
44128} ) ;
45129
46130// ─── getCacheKey ─────────────────────────────────────────────────────────────
0 commit comments