import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { afterEach, describe, expect, it } from 'vitest'; import { getMeowaReleaseCredentialPath, MEOWA_RELEASE_CREDENTIAL_FILE_NAME, readBundledMeowaApiKey, readEmbeddedMeowaApiKey, } from '@electron/services/meowa-game-assets-release-credential'; const tempDirs: string[] = []; afterEach(async () => { await Promise.all(tempDirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true }))); }); describe('Meowa release credential resource', () => { it('provides the approved Main-only embedded credential', () => { const apiKey = readEmbeddedMeowaApiKey(); expect(apiKey).toMatch(/^ma_live_/); expect(apiKey?.length).toBeLessThanOrEqual(512); }); it('reads a versioned packaged credential from the resources tree', async () => { const resourcesPath = await mkdtemp(join(tmpdir(), 'niancode-meowa-release-')); tempDirs.push(resourcesPath); const file = getMeowaReleaseCredentialPath(resourcesPath); await mkdir(join(resourcesPath, 'resources'), { recursive: true }); await writeFile(file, JSON.stringify({ schemaVersion: 1, apiKey: 'release-secret' }), 'utf8'); await expect(readBundledMeowaApiKey(resourcesPath)).resolves.toBe('release-secret'); expect(file.endsWith(MEOWA_RELEASE_CREDENTIAL_FILE_NAME)).toBe(true); }); it('rejects malformed, unversioned, and oversized packaged credentials', async () => { const resourcesPath = await mkdtemp(join(tmpdir(), 'niancode-meowa-release-')); tempDirs.push(resourcesPath); const file = getMeowaReleaseCredentialPath(resourcesPath); await mkdir(join(resourcesPath, 'resources'), { recursive: true }); await writeFile(file, JSON.stringify({ apiKey: 'missing-version' }), 'utf8'); await expect(readBundledMeowaApiKey(resourcesPath)).resolves.toBeNull(); await writeFile(file, '{not-json', 'utf8'); await expect(readBundledMeowaApiKey(resourcesPath)).resolves.toBeNull(); await writeFile(file, JSON.stringify({ schemaVersion: 1, apiKey: 'x'.repeat(513) }), 'utf8'); await expect(readBundledMeowaApiKey(resourcesPath)).resolves.toBeNull(); }); });