Files
Rosea/apps/rosea-tauri/test/android-bridge.test.ts

70 lines
3.8 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 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("paints behind the system bars with the garden's own paper, light and dark", () => {
const tokens = readFileSync(join(HERE, '..', '..', '..', 'packages', 'ui', 'src', 'tokens.css'), 'utf8');
const papers = [...tokens.matchAll(/--paper:\s*(#[0-9a-f]{6})/gi)].map((m) => m[1]!.toLowerCase());
expect(papers).toHaveLength(2);
expect(/PAPER = "(#[0-9a-f]{6})"/i.exec(kotlin)?.[1]?.toLowerCase()).toBe(papers[0]);
expect(/PAPER_DARK = "(#[0-9a-f]{6})"/i.exec(kotlin)?.[1]?.toLowerCase()).toBe(papers[1]);
// A source check only; the proof is the tablet's screenshot (review). Exact call and padding:
expect(kotlin).toMatch(/ViewCompat\.setOnApplyWindowInsetsListener\(findViewById\(android\.R\.id\.content\)\)/);
expect(kotlin).toMatch(/view\.setPadding\(inset\.left, inset\.top, inset\.right, inset\.bottom\)/);
});
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');
});
});