1- import { describe , expect , it } from 'vitest' ;
1+ import { LinuxDistro , ResourceOs } from '@codifycli/schemas' ;
2+ import { describe , expect , it , vi } from 'vitest' ;
3+
4+ import { OsUtils } from '../utils/os-utils.js' ;
25import { Project } from './project.js' ;
36import { ResourceConfig } from './resource-config.js' ;
4- import { InMemoryFile } from '../parser/entities' ;
7+
8+ function makeResource ( type : string , os ?: ResourceOs [ ] , distro ?: LinuxDistro [ ] ) : ResourceConfig {
9+ return new ResourceConfig ( { type, ...( os ? { os } : { } ) , ...( distro ? { distro } : { } ) } ) ;
10+ }
11+
12+ function makeProject ( ...configs : ResourceConfig [ ] ) : Project {
13+ return new Project ( null , configs , [ ] ) ;
14+ }
515
616describe ( 'Project Unit Tests' , ( ) => {
717 it ( 'Can add unique names for duplicate resources' , async ( ) => {
@@ -26,4 +36,219 @@ describe('Project Unit Tests', () => {
2636 // expect(project.resourceConfigs[3].id).to.eq('other')
2737 } )
2838
29- } )
39+ describe ( 'removeResourcesUsingOsFilter' , ( ) => {
40+ it ( 'keeps resources with no os filter' , ( ) => {
41+ vi . spyOn ( OsUtils , 'getOs' ) . mockReturnValue ( ResourceOs . MACOS ) ;
42+
43+ const project = makeProject (
44+ makeResource ( 'tool-a' ) ,
45+ makeResource ( 'tool-b' ) ,
46+ ) ;
47+
48+ project . removeResourcesUsingOsFilter ( ) ;
49+
50+ expect ( project . resourceConfigs ) . toHaveLength ( 2 ) ;
51+ } ) ;
52+
53+ it ( 'keeps resources that match the current os' , ( ) => {
54+ vi . spyOn ( OsUtils , 'getOs' ) . mockReturnValue ( ResourceOs . MACOS ) ;
55+
56+ const project = makeProject (
57+ makeResource ( 'mac-tool' , [ ResourceOs . MACOS ] ) ,
58+ makeResource ( 'linux-tool' , [ ResourceOs . LINUX ] ) ,
59+ ) ;
60+
61+ project . removeResourcesUsingOsFilter ( ) ;
62+
63+ expect ( project . resourceConfigs ) . toHaveLength ( 1 ) ;
64+ expect ( project . resourceConfigs [ 0 ] . type ) . toBe ( 'mac-tool' ) ;
65+ } ) ;
66+
67+ it ( 'keeps resources that list multiple os including the current one' , ( ) => {
68+ vi . spyOn ( OsUtils , 'getOs' ) . mockReturnValue ( ResourceOs . LINUX ) ;
69+
70+ const project = makeProject (
71+ makeResource ( 'cross-platform' , [ ResourceOs . MACOS , ResourceOs . LINUX ] ) ,
72+ ) ;
73+
74+ project . removeResourcesUsingOsFilter ( ) ;
75+
76+ expect ( project . resourceConfigs ) . toHaveLength ( 1 ) ;
77+ } ) ;
78+
79+ it ( 'removes resources whose os does not match the current os' , ( ) => {
80+ vi . spyOn ( OsUtils , 'getOs' ) . mockReturnValue ( ResourceOs . WINDOWS ) ;
81+
82+ const project = makeProject (
83+ makeResource ( 'mac-only' , [ ResourceOs . MACOS ] ) ,
84+ makeResource ( 'linux-only' , [ ResourceOs . LINUX ] ) ,
85+ ) ;
86+
87+ project . removeResourcesUsingOsFilter ( ) ;
88+
89+ expect ( project . resourceConfigs ) . toHaveLength ( 0 ) ;
90+ } ) ;
91+ } ) ;
92+
93+ describe ( 'removeResourcesUsingDistroFilter' , ( ) => {
94+ it ( 'does nothing on macOS (not Linux)' , async ( ) => {
95+ vi . spyOn ( OsUtils , 'isLinux' ) . mockReturnValue ( false ) ;
96+
97+ const project = makeProject (
98+ makeResource ( 'tool' , undefined , [ LinuxDistro . UBUNTU ] ) ,
99+ ) ;
100+
101+ await project . removeResourcesUsingDistroFilter ( ) ;
102+
103+ expect ( project . resourceConfigs ) . toHaveLength ( 1 ) ;
104+ } ) ;
105+
106+ it ( 'does nothing when distro cannot be determined' , async ( ) => {
107+ vi . spyOn ( OsUtils , 'isLinux' ) . mockReturnValue ( true ) ;
108+ vi . spyOn ( OsUtils , 'getLinuxDistro' ) . mockResolvedValue ( undefined ) ;
109+
110+ const project = makeProject (
111+ makeResource ( 'tool' , undefined , [ LinuxDistro . UBUNTU ] ) ,
112+ ) ;
113+
114+ await project . removeResourcesUsingDistroFilter ( ) ;
115+
116+ expect ( project . resourceConfigs ) . toHaveLength ( 1 ) ;
117+ } ) ;
118+
119+ it ( 'keeps resources with no distro filter' , async ( ) => {
120+ vi . spyOn ( OsUtils , 'isLinux' ) . mockReturnValue ( true ) ;
121+ vi . spyOn ( OsUtils , 'getLinuxDistro' ) . mockResolvedValue ( LinuxDistro . UBUNTU ) ;
122+
123+ const project = makeProject (
124+ makeResource ( 'tool-a' ) ,
125+ makeResource ( 'tool-b' ) ,
126+ ) ;
127+
128+ await project . removeResourcesUsingDistroFilter ( ) ;
129+
130+ expect ( project . resourceConfigs ) . toHaveLength ( 2 ) ;
131+ } ) ;
132+
133+ it ( 'keeps resources that match the current distro exactly' , async ( ) => {
134+ vi . spyOn ( OsUtils , 'isLinux' ) . mockReturnValue ( true ) ;
135+ vi . spyOn ( OsUtils , 'getLinuxDistro' ) . mockResolvedValue ( LinuxDistro . UBUNTU ) ;
136+
137+ const project = makeProject (
138+ makeResource ( 'ubuntu-tool' , undefined , [ LinuxDistro . UBUNTU ] ) ,
139+ makeResource ( 'arch-tool' , undefined , [ LinuxDistro . ARCH ] ) ,
140+ ) ;
141+
142+ await project . removeResourcesUsingDistroFilter ( ) ;
143+
144+ expect ( project . resourceConfigs ) . toHaveLength ( 1 ) ;
145+ expect ( project . resourceConfigs [ 0 ] . type ) . toBe ( 'ubuntu-tool' ) ;
146+ } ) ;
147+
148+ it ( 'keeps resources when current distro matches debian-based group' , async ( ) => {
149+ vi . spyOn ( OsUtils , 'isLinux' ) . mockReturnValue ( true ) ;
150+ vi . spyOn ( OsUtils , 'getLinuxDistro' ) . mockResolvedValue ( LinuxDistro . UBUNTU ) ;
151+
152+ const project = makeProject (
153+ makeResource ( 'debian-tool' , undefined , [ LinuxDistro . DEBIAN_BASED ] ) ,
154+ makeResource ( 'rpm-tool' , undefined , [ LinuxDistro . RPM_BASED ] ) ,
155+ ) ;
156+
157+ await project . removeResourcesUsingDistroFilter ( ) ;
158+
159+ expect ( project . resourceConfigs ) . toHaveLength ( 1 ) ;
160+ expect ( project . resourceConfigs [ 0 ] . type ) . toBe ( 'debian-tool' ) ;
161+ } ) ;
162+
163+ it ( 'keeps resources when current distro matches rpm-based group' , async ( ) => {
164+ vi . spyOn ( OsUtils , 'isLinux' ) . mockReturnValue ( true ) ;
165+ vi . spyOn ( OsUtils , 'getLinuxDistro' ) . mockResolvedValue ( LinuxDistro . FEDORA ) ;
166+
167+ const project = makeProject (
168+ makeResource ( 'debian-tool' , undefined , [ LinuxDistro . DEBIAN_BASED ] ) ,
169+ makeResource ( 'rpm-tool' , undefined , [ LinuxDistro . RPM_BASED ] ) ,
170+ ) ;
171+
172+ await project . removeResourcesUsingDistroFilter ( ) ;
173+
174+ expect ( project . resourceConfigs ) . toHaveLength ( 1 ) ;
175+ expect ( project . resourceConfigs [ 0 ] . type ) . toBe ( 'rpm-tool' ) ;
176+ } ) ;
177+
178+ it ( 'debian-based group covers all expected distros' , async ( ) => {
179+ const debianDistros = [
180+ LinuxDistro . DEBIAN ,
181+ LinuxDistro . UBUNTU ,
182+ LinuxDistro . MINT ,
183+ LinuxDistro . POP_OS ,
184+ LinuxDistro . ELEMENTARY_OS ,
185+ LinuxDistro . KALI ,
186+ ] ;
187+
188+ vi . spyOn ( OsUtils , 'isLinux' ) . mockReturnValue ( true ) ;
189+
190+ for ( const distro of debianDistros ) {
191+ vi . spyOn ( OsUtils , 'getLinuxDistro' ) . mockResolvedValue ( distro ) ;
192+
193+ const project = makeProject (
194+ makeResource ( 'tool' , undefined , [ LinuxDistro . DEBIAN_BASED ] ) ,
195+ ) ;
196+
197+ await project . removeResourcesUsingDistroFilter ( ) ;
198+
199+ expect ( project . resourceConfigs ) . toHaveLength ( 1 ) ;
200+ }
201+ } ) ;
202+
203+ it ( 'rpm-based group covers all expected distros' , async ( ) => {
204+ const rpmDistros = [
205+ LinuxDistro . FEDORA ,
206+ LinuxDistro . CENTOS ,
207+ LinuxDistro . RHEL ,
208+ LinuxDistro . AMAZON_LINUX ,
209+ LinuxDistro . OPENSUSE ,
210+ LinuxDistro . SUSE ,
211+ ] ;
212+
213+ vi . spyOn ( OsUtils , 'isLinux' ) . mockReturnValue ( true ) ;
214+
215+ for ( const distro of rpmDistros ) {
216+ vi . spyOn ( OsUtils , 'getLinuxDistro' ) . mockResolvedValue ( distro ) ;
217+
218+ const project = makeProject (
219+ makeResource ( 'tool' , undefined , [ LinuxDistro . RPM_BASED ] ) ,
220+ ) ;
221+
222+ await project . removeResourcesUsingDistroFilter ( ) ;
223+
224+ expect ( project . resourceConfigs ) . toHaveLength ( 1 ) ;
225+ }
226+ } ) ;
227+
228+ it ( 'removes resources when no distro in filter matches the current distro' , async ( ) => {
229+ vi . spyOn ( OsUtils , 'isLinux' ) . mockReturnValue ( true ) ;
230+ vi . spyOn ( OsUtils , 'getLinuxDistro' ) . mockResolvedValue ( LinuxDistro . ARCH ) ;
231+
232+ const project = makeProject (
233+ makeResource ( 'tool' , undefined , [ LinuxDistro . UBUNTU , LinuxDistro . DEBIAN ] ) ,
234+ ) ;
235+
236+ await project . removeResourcesUsingDistroFilter ( ) ;
237+
238+ expect ( project . resourceConfigs ) . toHaveLength ( 0 ) ;
239+ } ) ;
240+
241+ it ( 'keeps resources that list multiple distros including the current one' , async ( ) => {
242+ vi . spyOn ( OsUtils , 'isLinux' ) . mockReturnValue ( true ) ;
243+ vi . spyOn ( OsUtils , 'getLinuxDistro' ) . mockResolvedValue ( LinuxDistro . ARCH ) ;
244+
245+ const project = makeProject (
246+ makeResource ( 'tool' , undefined , [ LinuxDistro . UBUNTU , LinuxDistro . ARCH ] ) ,
247+ ) ;
248+
249+ await project . removeResourcesUsingDistroFilter ( ) ;
250+
251+ expect ( project . resourceConfigs ) . toHaveLength ( 1 ) ;
252+ } ) ;
253+ } ) ;
254+ } ) ;
0 commit comments