feat(core): Add support for listing experiments (#12495)

This commit is contained in:
Shreya Keshive
2025-11-03 13:51:22 -08:00
committed by GitHub
parent 1c044ba8af
commit be1dc13bb1
7 changed files with 273 additions and 5 deletions

View File

@@ -5,7 +5,14 @@
*/
import { vi, describe, it, expect, beforeEach } from 'vitest';
import { isNightly, isPreview, isStable, _clearCache } from './channel.js';
import {
ReleaseChannel,
getReleaseChannel,
isNightly,
isPreview,
isStable,
_clearCache,
} from './channel.js';
import * as packageJson from './package.js';
vi.mock('./package.js', () => ({
@@ -132,6 +139,54 @@ describe('channel', () => {
});
});
describe('getReleaseChannel', () => {
it('should return STABLE for a stable version', async () => {
vi.spyOn(packageJson, 'getPackageJson').mockResolvedValue({
name: 'test',
version: '1.0.0',
});
await expect(getReleaseChannel('/test/dir')).resolves.toBe(
ReleaseChannel.STABLE,
);
});
it('should return NIGHTLY for a nightly version', async () => {
vi.spyOn(packageJson, 'getPackageJson').mockResolvedValue({
name: 'test',
version: '1.0.0-nightly.1',
});
await expect(getReleaseChannel('/test/dir')).resolves.toBe(
ReleaseChannel.NIGHTLY,
);
});
it('should return PREVIEW for a preview version', async () => {
vi.spyOn(packageJson, 'getPackageJson').mockResolvedValue({
name: 'test',
version: '1.0.0-preview.1',
});
await expect(getReleaseChannel('/test/dir')).resolves.toBe(
ReleaseChannel.PREVIEW,
);
});
it('should return NIGHTLY if package.json is not found', async () => {
vi.spyOn(packageJson, 'getPackageJson').mockResolvedValue(undefined);
await expect(getReleaseChannel('/test/dir')).resolves.toBe(
ReleaseChannel.NIGHTLY,
);
});
it('should return NIGHTLY if version is not defined', async () => {
vi.spyOn(packageJson, 'getPackageJson').mockResolvedValue({
name: 'test',
});
await expect(getReleaseChannel('/test/dir')).resolves.toBe(
ReleaseChannel.NIGHTLY,
);
});
});
describe('memoization', () => {
it('should only call getPackageJson once for the same cwd', async () => {
const spy = vi
@@ -141,6 +196,9 @@ describe('channel', () => {
await expect(isStable('/test/dir')).resolves.toBe(true);
await expect(isNightly('/test/dir')).resolves.toBe(false);
await expect(isPreview('/test/dir')).resolves.toBe(false);
await expect(getReleaseChannel('/test/dir')).resolves.toBe(
ReleaseChannel.STABLE,
);
expect(spy).toHaveBeenCalledTimes(1);
});

View File

@@ -22,7 +22,7 @@ export function _clearCache() {
cache.clear();
}
async function _getReleaseChannel(cwd: string): Promise<ReleaseChannel> {
export async function getReleaseChannel(cwd: string): Promise<ReleaseChannel> {
if (cache.has(cwd)) {
return cache.get(cwd)!;
}
@@ -43,13 +43,13 @@ async function _getReleaseChannel(cwd: string): Promise<ReleaseChannel> {
}
export async function isNightly(cwd: string): Promise<boolean> {
return (await _getReleaseChannel(cwd)) === ReleaseChannel.NIGHTLY;
return (await getReleaseChannel(cwd)) === ReleaseChannel.NIGHTLY;
}
export async function isPreview(cwd: string): Promise<boolean> {
return (await _getReleaseChannel(cwd)) === ReleaseChannel.PREVIEW;
return (await getReleaseChannel(cwd)) === ReleaseChannel.PREVIEW;
}
export async function isStable(cwd: string): Promise<boolean> {
return (await _getReleaseChannel(cwd)) === ReleaseChannel.STABLE;
return (await getReleaseChannel(cwd)) === ReleaseChannel.STABLE;
}