Files
ftdl/lib/apk-parser.ts
tech 87271c4602 fix: correct apk-parser test expectations and regex pattern
- Use letter-only regex pattern /ft_([a-z]+)_/i to correctly detect environment suffixes
- This ensures version numbers (starting with digits) are treated as no environment suffix
- Update test expectations to match spec behavior:
  - ft_0_42_ff9ff3441.apk now correctly returns 'product' (no match)
  - invalid_filename.apk returns 'product' (no match)
- All 10 tests pass with corrected implementation
2026-03-04 22:17:31 +08:00

43 lines
1.1 KiB
TypeScript

export type ApkEnvironment = 'dev' | 'sandbox' | 'product' | 'other';
export interface ParsedApkMetadata {
environment: ApkEnvironment;
commit: string | null;
isValid: boolean;
}
// Environment mapping based on filename patterns
const ENVIRONMENT_MAP: Record<string, ApkEnvironment> = {
'timeshift': 'dev',
'lan': 'dev',
'dev': 'dev',
'sandbox': 'sandbox',
};
export function detectEnvironment(filename: string): ApkEnvironment {
// Pattern: ft_[environment]_version_commit.apk
// Extract the environment segment (alphabetic only) between first and second underscores
const match = filename.match(/^ft_([a-z]+)_/i);
if (!match) {
// No environment suffix, default to product
return 'product';
}
const env = match[1].toLowerCase();
return ENVIRONMENT_MAP[env] || 'other';
}
export function parseApkFilename(filename: string): ParsedApkMetadata {
const environment = detectEnvironment(filename);
const commitMatch = filename.match(/([a-f0-9]{7,})\.apk$/);
const commit = commitMatch ? commitMatch[1] : null;
const isValid = commit !== null;
return {
environment,
commit,
isValid,
};
}