59 lines
3.0 KiB
TypeScript
59 lines
3.0 KiB
TypeScript
/**
|
||
* The save bridge's contract between the Kotlin side (android/java/MainActivity.kt) and the game
|
||
* (apps/rosea/src/App.svelte, packages/ui KeepSafe): keep-her-safe-apps.md A2–A5. A mismatch here
|
||
* fails silently on the device — the save screen never opens, or the garden never hears back —
|
||
* so the names are read from both sources and compared.
|
||
*/
|
||
import { readFileSync } from 'node:fs';
|
||
import { dirname, join } from 'node:path';
|
||
import { fileURLToPath } from 'node:url';
|
||
import { describe, expect, it } from 'vitest';
|
||
|
||
const HERE = dirname(fileURLToPath(import.meta.url));
|
||
const kotlin = readFileSync(join(HERE, '..', 'android', 'java', 'MainActivity.kt'), 'utf8');
|
||
const app = readFileSync(join(HERE, '..', '..', 'rosea', 'src', 'App.svelte'), 'utf8');
|
||
const keepSafe = readFileSync(join(HERE, '..', '..', '..', 'packages', 'ui', 'src', 'rose', 'KeepSafe.svelte'), 'utf8');
|
||
const conf = JSON.parse(readFileSync(join(HERE, '..', 'src-tauri', 'tauri.conf.json'), 'utf8')) as { identifier: string };
|
||
|
||
const nameRule = new RegExp(/Regex\("(.+?)"\)/.exec(kotlin)?.[1]?.replaceAll('\\\\', '\\') ?? 'missing');
|
||
|
||
describe('the Android save bridge', () => {
|
||
it('lives in the package of the app it is copied into', () => {
|
||
expect(kotlin).toMatch(new RegExp(`^package ${conf.identifier.replaceAll('.', '\\.')}$`, 'm'));
|
||
expect(kotlin).toMatch(/class MainActivity : TauriActivity\(\)/);
|
||
});
|
||
|
||
it('is found by the game under the name it is registered with', () => {
|
||
const registered = /addJavascriptInterface\(RoseFile\(\), "(\w+)"\)/.exec(kotlin)?.[1];
|
||
expect(registered).toBe('roseaFile');
|
||
expect(app).toContain(`roseaFile?: { save(name: string, text: string): void }`);
|
||
expect(app).toContain('.roseaFile');
|
||
});
|
||
|
||
it('answers on the event the game listens for, with true or false', () => {
|
||
const event = /new CustomEvent\('([\w-]+)', \{ detail: \$written \}\)/.exec(kotlin)?.[1];
|
||
expect(event).toBe('rosea-file-saved');
|
||
expect(app.match(/'rosea-file-saved'/g)?.length).toBeGreaterThanOrEqual(2);
|
||
expect(app).toContain('.detail === true');
|
||
});
|
||
|
||
it("accepts the file name the garden gives (rose-<day>.json) and nothing else", () => {
|
||
expect(keepSafe).toContain('`rose-${today}.json`');
|
||
expect(nameRule.test('rose-2026-11-17.json')).toBe(true);
|
||
for (const wrong of ['rose.json', '../rose-2026-11-17.json', 'rose-2026-11-17.json.exe', 'rose-2026-11-17.apk', 'evil-2026-11-17.json']) {
|
||
expect(nameRule.test(wrong)).toBe(false);
|
||
}
|
||
});
|
||
|
||
it('keeps the bridge through a minified release build', () => {
|
||
expect(kotlin).toMatch(/@Keep\s+inner class RoseFile/);
|
||
expect(kotlin).toMatch(/@JavascriptInterface\s+fun save\(name: String, text: String\)/);
|
||
});
|
||
|
||
it('asks Android for nothing: the save screen needs no permission', () => {
|
||
// Asking at run time or naming one; the APK check (aapt2) refuses any declared in the manifest.
|
||
expect(kotlin).not.toMatch(/requestPermissions\(|Manifest\.permission|checkSelfPermission/);
|
||
expect(kotlin).toContain('Intent.ACTION_CREATE_DOCUMENT');
|
||
});
|
||
});
|