Regenerate font subsets for the current locales; port font, locale and storage path tests

The Chinese profile picture string used a character (U+50CF) that no shipped font contained. New glyph coverage and locale key tests replace the old C ones; storage path scenarios run as Ziran processes.
This commit is contained in:
waozi committed 2026-09-28 19:19:47 -03:00
1 parent 31fe8c6a8a
commit 77dd9a032b
23 files changed
+392 -1121

No files matched your search

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
-23
View File
@@ -1,23 +0,0 @@
import Base
import ./main.bend as Policy
law current_dir_is_inbe_everywhere:
for +platform: Policy.Platform
{Policy.current_dir(platform) == Policy.Inbe{} : Policy.DirName}
law current_db_is_inbe:
{Policy.current_db() == Policy.InbeDb{} : Policy.DbName}
law legacy_dirs_differ_from_current:
for +platform: Policy.Platform
{Policy.dirs_distinct(Policy.current_dir(platform), Policy.legacy_dir(platform)) == Policy.Different{} : Policy.Same}
law legacy_db_is_breathing:
{Policy.legacy_db() == Policy.BreathingDb{} : Policy.DbName}
law export_entry_is_importable:
{Policy.importable(Policy.export_entry()) == Policy.Yes{} : Policy.Importable}
law historical_entries_stay_importable:
for +entry: Policy.EntryName
{Policy.importable(entry) == Policy.Yes{} : Policy.Importable}
-41
View File
@@ -1,41 +0,0 @@
import Base
import ./main.bend as Policy
import ./LAWS.bend as Laws
def Laws.current_dir_is_inbe_everywhere(platform):
match platform:
case Policy.Android{}:
{==}
case Policy.Web{}:
{==}
case Policy.Windows{}:
{==}
case Policy.Posix{}:
{==}
def Laws.current_db_is_inbe():
{==}
def Laws.legacy_dirs_differ_from_current(platform):
match platform:
case Policy.Android{}:
{==}
case Policy.Web{}:
{==}
case Policy.Windows{}:
{==}
case Policy.Posix{}:
{==}
def Laws.legacy_db_is_breathing():
{==}
def Laws.export_entry_is_importable():
{==}
def Laws.historical_entries_stay_importable(entry):
match entry:
case Policy.InbeDataDb{}:
{==}
case Policy.BreathingDataDb{}:
{==}
-92
View File
@@ -1,92 +0,0 @@
import Base
type Platform is Data:
Android{}
Web{}
Windows{}
Posix{}
type DirName is Data:
Inbe{}
Breathing{}
BreathSession{}
type DbName is Data:
InbeDb{}
BreathingDb{}
type Same is Data:
Different{}
Same{}
type EntryName is Data:
InbeDataDb{}
BreathingDataDb{}
type Importable is Data:
Yes{}
def current_dir(platform: Platform) -> DirName:
match platform:
case Android{}:
Inbe{}
case Web{}:
Inbe{}
case Windows{}:
Inbe{}
case Posix{}:
Inbe{}
def current_db() -> DbName:
InbeDb{}
def legacy_dir(platform: Platform) -> DirName:
match platform:
case Android{}:
Breathing{}
case Web{}:
Breathing{}
case Windows{}:
BreathSession{}
case Posix{}:
Breathing{}
def dirs_distinct(current: DirName, legacy: DirName) -> Same:
match current:
case Inbe{}:
match legacy:
case Inbe{}:
Same{}
case Breathing{}:
Different{}
case BreathSession{}:
Different{}
case Breathing{}:
match legacy:
case Inbe{}:
Different{}
case Breathing{}:
Same{}
case BreathSession{}:
Different{}
case BreathSession{}:
match legacy:
case Inbe{}:
Different{}
case Breathing{}:
Different{}
case BreathSession{}:
Same{}
def legacy_db() -> DbName:
BreathingDb{}
def export_entry() -> EntryName:
InbeDataDb{}
def importable(entry: EntryName) -> Importable:
match entry:
case InbeDataDb{}:
Yes{}
case BreathingDataDb{}:
Yes{}
-86
View File
@@ -1,86 +0,0 @@
import Base
import ./main.bend as Policy
# The contract owns the schedule; the implementation must prove it agrees.
def schedule(attempt: Policy.Attempt) -> Policy.Decision:
match attempt:
case Policy.Initial{}:
Policy.Decision{1, 5, 0}
case Policy.First{}:
Policy.Decision{2, 15, 0}
case Policy.Second{}:
Policy.Decision{3, 30, 0}
case Policy.Third{}:
Policy.Decision{4, 60, 0}
case Policy.Capped{}:
Policy.Decision{4, 60, 0}
def saved_delay(attempt: Policy.Attempt) -> U32:
match attempt:
case Policy.Initial{}:
5
case Policy.First{}:
5
case Policy.Second{}:
15
case Policy.Third{}:
30
case Policy.Capped{}:
60
def saved_attempt(attempt: Policy.Attempt) -> U32:
match attempt:
case Policy.Initial{}:
0
case Policy.First{}:
1
case Policy.Second{}:
2
case Policy.Third{}:
3
case Policy.Capped{}:
4
law success_resets:
for attempt: Policy.Attempt
{Policy.decide(Policy.Success{}, attempt) == Policy.Decision{0, 0, 0} : Policy.Decision}
law challenge_retries:
for +attempt: Policy.Attempt
{Policy.decide(Policy.ChallengeFailed{}, attempt) == schedule(attempt) : Policy.Decision}
law request_retries:
for +attempt: Policy.Attempt
{Policy.decide(Policy.RequestFailed{}, attempt) == schedule(attempt) : Policy.Decision}
law invalid_url_stops:
for +attempt: Policy.Attempt
{Policy.decide(Policy.InvalidUrl{}, attempt) == Policy.Decision{Policy.number(attempt), 0, 1} : Policy.Decision}
law no_account_stops:
for +attempt: Policy.Attempt
{Policy.decide(Policy.NoAccount{}, attempt) == Policy.Decision{Policy.number(attempt), 0, 1} : Policy.Decision}
law payload_failure_stops:
for +attempt: Policy.Attempt
{Policy.decide(Policy.PayloadFailed{}, attempt) == Policy.Decision{Policy.number(attempt), 0, 1} : Policy.Decision}
law signing_failure_stops:
for +attempt: Policy.Attempt
{Policy.decide(Policy.SignFailed{}, attempt) == Policy.Decision{Policy.number(attempt), 0, 1} : Policy.Decision}
law authentication_failure_stops:
for +attempt: Policy.Attempt
{Policy.decide(Policy.AuthFailed{}, attempt) == Policy.Decision{Policy.number(attempt), 0, 1} : Policy.Decision}
law unknown_result_stops:
for +attempt: Policy.Attempt
{Policy.decide(Policy.Unknown{}, attempt) == Policy.Decision{Policy.number(attempt), 0, 1} : Policy.Decision}
law saved_delay_matches:
for +attempt: Policy.Attempt
{Policy.delay(attempt) == saved_delay(attempt) : U32}
law attempt_number_matches:
for +attempt: Policy.Attempt
{Policy.number(attempt) == saved_attempt(attempt) : U32}
-76
View File
@@ -1,76 +0,0 @@
import Base
import ./main.bend as Policy
import ./LAWS.bend as Laws
def Laws.success_resets(attempt):
{==}
def Laws.challenge_retries(attempt):
match attempt:
case Policy.Initial{}:
{==}
case Policy.First{}:
{==}
case Policy.Second{}:
{==}
case Policy.Third{}:
{==}
case Policy.Capped{}:
{==}
def Laws.request_retries(attempt):
match attempt:
case Policy.Initial{}:
{==}
case Policy.First{}:
{==}
case Policy.Second{}:
{==}
case Policy.Third{}:
{==}
case Policy.Capped{}:
{==}
def Laws.invalid_url_stops(attempt):
{==}
def Laws.no_account_stops(attempt):
{==}
def Laws.payload_failure_stops(attempt):
{==}
def Laws.signing_failure_stops(attempt):
{==}
def Laws.authentication_failure_stops(attempt):
{==}
def Laws.unknown_result_stops(attempt):
{==}
def Laws.saved_delay_matches(attempt):
match attempt:
case Policy.Initial{}:
{==}
case Policy.First{}:
{==}
case Policy.Second{}:
{==}
case Policy.Third{}:
{==}
case Policy.Capped{}:
{==}
def Laws.attempt_number_matches(attempt):
match attempt:
case Policy.Initial{}:
{==}
case Policy.First{}:
{==}
case Policy.Second{}:
{==}
case Policy.Third{}:
{==}
case Policy.Capped{}:
{==}
-89
View File
@@ -1,89 +0,0 @@
import Base
type Result is Data:
Success{}
InvalidUrl{}
NoAccount{}
PayloadFailed{}
ChallengeFailed{}
SignFailed{}
RequestFailed{}
AuthFailed{}
Unknown{}
type Attempt is Data:
Initial{}
First{}
Second{}
Third{}
Capped{}
type Decision is Data:
Decision{attempt: U32, delay: U32, needs_action: U32}
def number(attempt: Attempt) -> U32:
match attempt:
case Initial{}:
0
case First{}:
1
case Second{}:
2
case Third{}:
3
case Capped{}:
4
def advance(attempt: Attempt) -> Attempt:
match attempt:
case Initial{}:
First{}
case First{}:
Second{}
case Second{}:
Third{}
case Third{}:
Capped{}
case Capped{}:
Capped{}
def delay(attempt: Attempt) -> U32:
match attempt:
case Initial{}:
5
case First{}:
5
case Second{}:
15
case Third{}:
30
case Capped{}:
60
def retry(attempt: Attempt) -> Decision:
+next = advance(attempt)
Decision{number(next), delay(next), 0}
def stop(attempt: Attempt) -> Decision:
Decision{number(attempt), 0, 1}
def decide(result: Result, attempt: Attempt) -> Decision:
match result:
case Success{}:
Decision{0, 0, 0}
case InvalidUrl{}:
stop(attempt)
case NoAccount{}:
stop(attempt)
case PayloadFailed{}:
stop(attempt)
case ChallengeFailed{}:
retry(attempt)
case SignFailed{}:
stop(attempt)
case RequestFailed{}:
retry(attempt)
case AuthFailed{}:
stop(attempt)
case Unknown{}:
stop(attempt)
-176
View File
@@ -1,176 +0,0 @@
// Host-only proof checking and finite policy evaluation. No Bend runtime ships.
import fs from 'node:fs';
import path from 'node:path';
import { createHash } from 'node:crypto';
import { fileURLToPath, pathToFileURL } from 'node:url';
const toolDir = path.dirname(fileURLToPath(import.meta.url));
const bendDir = path.resolve(toolDir, '../build/packages/bend/bend2');
const pin = JSON.parse(fs.readFileSync(path.join(toolDir, 'bend-pin.json'), 'utf8'));
function requireLaw(condition, message) {
if (!condition) {
throw new Error(`proof.required: ${message}`);
}
}
async function checker() {
const [major, minor] = process.versions.node.split('.').map(Number);
requireLaw(major > 22 || (major === 22 && minor >= 18), 'Node.js 22.18 or newer is required');
for (const [name, expected] of Object.entries(pin.files)) {
const file = path.join(bendDir, name);
requireLaw(fs.existsSync(file), 'run sh scripts/packages.sh to fetch the pinned bend package');
const actual = createHash('sha256').update(fs.readFileSync(file)).digest('hex');
requireLaw(actual === expected, `${name} differs from pinned Bend ${pin.version} (${pin.commit})`);
}
return import(pathToFileURL(path.join(bendDir, 'bend.ts')).href);
}
// Restrict the proof package to Base and local .bend imports. Validate before
// loading: Bend's general-purpose package loader can otherwise access a hub.
function localFiles(entry) {
const root = path.dirname(entry);
const files = new Set();
function visit(file) {
const real = fs.realpathSync(file);
const relative = path.relative(root, real);
requireLaw(relative !== '..' && !relative.startsWith(`..${path.sep}`)
&& !path.isAbsolute(relative), `import leaves proof package: ${file}`);
if (files.has(real)) {
return;
}
files.add(real);
for (const line of fs.readFileSync(real, 'utf8').split('\n')) {
if (!/^\s*import(?:\s|$)/.test(line)) {
continue;
}
if (/^\s*import\s+Base\s*(?:#.*)?$/.test(line)) {
continue;
}
const match = line.match(/^\s*import\s+(\.\/\S+\.bend)\s+as\s+[A-Za-z_][A-Za-z0-9_]*\s*(?:#.*)?$/);
requireLaw(match, `only Base and ./local.bend imports are allowed: ${line.trim()}`);
visit(path.resolve(path.dirname(real), match[1]));
}
}
visit(entry);
requireLaw(files.has(path.join(root, 'LAWS.bend')), 'PROOF.bend must import its sibling LAWS.bend');
return [...files].sort();
}
export async function checkLaws(proofFile) {
const entry = fs.realpathSync(proofFile);
requireLaw(path.basename(entry) === 'PROOF.bend', 'entry point must be PROOF.bend');
const files = localFiles(entry);
const Bend = await checker();
const book = Bend.book_nil();
const seen = new Map();
try {
await Bend.book_load(book, entry, '', seen);
} catch (error) {
throw new Error(error?.$ === 'Err' ? Bend.err_show(error) : String(error));
}
const lawsFile = path.join(path.dirname(entry), 'LAWS.bend');
requireLaw(seen.has(lawsFile), 'LAWS.bend was not loaded by the checker');
for (const [name, declaration] of Object.entries(book.tlds)) {
requireLaw(!declaration.u, `@unsafe is forbidden: ${name}`);
requireLaw(!declaration.i || declaration.b === true, `foreign implementation is forbidden: ${name}`);
}
for (const [name, template] of Object.entries(book.tmps)) {
requireLaw(!template.u, `@unsafe template is forbidden: ${name}`);
}
try {
Bend.book_valid(book);
} catch (error) {
throw new Error(error?.$ === 'Err' ? Bend.err_show(error) : String(error));
}
requireLaw(book.hols === 0 && book.open === 0,
`${book.hols} proof holes and ${book.open} unproved declarations`);
const laws = [...fs.readFileSync(lawsFile, 'utf8').matchAll(/^law\s+([A-Za-z_][A-Za-z0-9_]*):/gm)]
.map(match => match[1]);
requireLaw(laws.length > 0, 'LAWS.bend must declare laws');
function value(term) {
const number = Bend.u32_from_term(term);
if (number !== null) {
return number;
}
requireLaw(term.$ === 'Ctr', 'finite policy must evaluate to a closed data value');
return { constructor: term.k, fields: term.x.map(value) };
}
// Enumerate the *checked function's* finite domains, then normalize that
// same implementation in the kernel. This avoids a second model and avoids
// relying on Bend's optimizing C/JS emitters for this finite-policy path.
function table(name) {
const declaration = book.tlds[name];
requireLaw(declaration?.$ === 'Def' && declaration.v !== null && !declaration.i,
`unknown or foreign policy: ${name}`);
const rows = [];
const domains = [];
function enumerate(type, term, args) {
const head = Bend.term_wnf(book, type);
if (head.$ !== 'All') {
requireLaw(rows.length < 4096, 'finite table exceeds 4096 rows');
rows.push({ arguments: args, value: value(Bend.term_snf(book, term)) });
return;
}
requireLaw(head.q.$ !== 'None', 'erased policy parameters cannot index a table');
const domain = Bend.term_wnf(book, head.A);
const adt = domain.$ === 'ADT' ? book.tlds[domain.k] : null;
requireLaw(adt?.$ === 'ADT' && adt.n === 0 && domain.r.length === 0
&& adt.c.length > 0 && adt.c.every(ctor => ctor.n === 0),
'table parameters must be nonempty enumerations without fields');
const constructors = adt.c.map(ctor => ctor.k);
const depth = args.length;
requireLaw(depth < 8, 'finite table exceeds 8 parameters');
requireLaw(!domains[depth] || JSON.stringify(domains[depth]) === JSON.stringify(constructors),
'dependent argument domains cannot form a rectangular table');
domains[depth] = constructors;
for (const constructor of constructors) {
const argument = Bend.Ctr(constructor, []);
enumerate(head.B(argument), Bend.App(term, argument), [...args, constructor]);
}
}
enumerate(declaration.T, Bend.Ref(name), []);
return { domains, rows };
}
return { laws, files, table, version: pin.version, commit: pin.commit };
}
// Bounded checker runner (law plan phase 2): proof checking happens in a
// separate process with a wall-clock timeout and a memory cap, so a stalled
// or runaway check fails with a diagnostic instead of hanging a gate.
import { spawnSync } from 'node:child_process';
export async function checkLawsProcess(proofFile, opts = {}) {
const timeoutMs = opts.timeoutMs ?? 120000;
const maxOldSpaceMb = opts.maxOldSpaceMb ?? 1024;
const args = [`--max-old-space-size=${maxOldSpaceMb}`,
new URL('bend-laws.mjs', import.meta.url).pathname, '--json', proofFile];
const result = spawnSync(process.execPath, args, { encoding: 'utf8', timeout: timeoutMs });
if (result.error) {
const reason = result.error.code === 'ETIMEDOUT'
? `checker exceeded ${timeoutMs}ms wall-clock limit`
: `checker could not run: ${result.error.message}`;
throw new Error(`proof.bounded: ${reason}`);
}
if (result.status !== 0) {
throw new Error(`proof.bounded: checker rejected the package:\n${result.stderr.trim()}`);
}
return JSON.parse(result.stdout);
}
if (process.argv[1] && pathToFileURL(path.resolve(process.argv[1])).href === import.meta.url) {
try {
requireLaw(process.argv.length === 3 || (process.argv.length === 4 && process.argv[2] === '--json'),
'usage: node scripts/bend-laws.mjs [--json] path/to/PROOF.bend');
const proof = await checkLaws(process.argv[process.argv.length - 1]);
if (process.argv.includes('--json')) {
console.log(JSON.stringify({ laws: proof.laws, files: proof.files, version: proof.version, commit: proof.commit }));
} else {
console.log(`Bend ${proof.version}: ${proof.laws.length} laws proved; no holes or open declarations`);
}
} catch (error) {
console.error(error.message);
process.exitCode = 1;
}
}
-8
View File
@@ -1,8 +0,0 @@
{
"version": "2.0.16",
"commit": "15ae0c86f3193b8f645b4bedbc438655b648d0da",
"files": {
"bend.ts": "47e1b06e2fa4466bad702f86d8cbce03f40e833a8ba8eaa1e04297fa46dc94a9",
"base.bend": "e149828ca05581f61d1b06cf2a7d1a744e394d29a4c8e1942e5062ecbf30f5b8"
}
}
-179
View File
@@ -1,179 +0,0 @@
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { checkLaws } from './bend-laws.mjs';
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const platforms = ['Android', 'Web', 'Windows', 'Posix'];
const requiredLaws = ['current_dir_is_inbe_everywhere', 'current_db_is_inbe',
'legacy_dirs_differ_from_current', 'legacy_db_is_breathing',
'export_entry_is_importable', 'historical_entries_stay_importable'];
const c = name => `main.${name}`;
const dirNames = { Inbe: 'inbe', Breathing: 'breathing',
BreathSession: 'BreathSession' };
const dbNames = { InbeDb: 'inbe.db', BreathingDb: 'breathing.db' };
const entryNames = {
InbeDataDb: 'inbe-data/inbe.db',
BreathingDataDb: 'breathing-data/breathing.db',
};
export async function generateStorageLayout(packageDir, output,
ziranOutput = output.replace(/\.h$/, '.zi')) {
const proof = await checkLaws(path.join(packageDir, 'PROOF.bend'));
assert.deepEqual(proof.laws.toSorted(), requiredLaws.toSorted(),
'storage.layout.required_laws');
const dirs = proof.table('main.current_dir');
assert.deepEqual(dirs.domains, [platforms.map(c)],
'storage.layout.current_dir_domain');
assert.equal(dirs.rows.length, platforms.length,
'storage.layout.current_dir_rows');
const currentDirs = dirs.rows.map((row, index) => {
assert.equal(row.arguments[0], c(platforms[index]),
'storage.layout.current_dir_order');
const constructor = row.value.constructor.slice('main.'.length);
assert.ok(dirNames[constructor] !== undefined,
'storage.layout.current_dir_name');
return dirNames[constructor];
});
const olderDirs = proof.table('main.legacy_dir');
assert.deepEqual(olderDirs.domains, [platforms.map(c)],
'storage.layout.legacy_dir_domain');
const legacyDirs = olderDirs.rows.map((row, index) => {
assert.equal(row.arguments[0], c(platforms[index]),
'storage.layout.legacy_dir_order');
const constructor = row.value.constructor.slice('main.'.length);
assert.ok(dirNames[constructor] !== undefined,
'storage.layout.legacy_dir_name');
return dirNames[constructor];
});
const scalar = (name, names) => {
const result = proof.table(`main.${name}`);
assert.deepEqual(result.domains, [], `storage.layout.${name}_domain`);
assert.equal(result.rows.length, 1, `storage.layout.${name}_rows`);
const constructor = result.rows[0].value.constructor.slice('main.'.length);
assert.ok(names[constructor] !== undefined,
`storage.layout.${name}_name`);
return names[constructor];
};
const currentDb = scalar('current_db', dbNames);
const legacyDb = scalar('legacy_db', dbNames);
const exportEntry = scalar('export_entry', entryNames);
const importable = proof.table('main.importable');
assert.deepEqual(importable.domains, [Object.keys(entryNames).map(c)],
'storage.layout.importable_domain');
assert.equal(importable.rows.length, Object.keys(entryNames).length,
'storage.layout.importable_rows');
for (const row of importable.rows) {
assert.equal(row.value.constructor, c('Yes'),
'storage.layout.historical_entry_not_importable');
}
assert.equal(exportEntry, entryNames.InbeDataDb,
'storage.layout.export_entry');
assert.ok(currentDirs.every(name => name === dirNames.Inbe),
'storage.layout.current_dir');
assert.ok(legacyDirs.every((name, index) => name !== currentDirs[index]),
'storage.layout.legacy_dir_distinct');
const platformDefines = platforms.map((platform, index) =>
`#define STORAGE_EXPECT_DIR_${platform.toUpperCase()} "${currentDirs[index]}"`).join('\n');
const legacyDefines = platforms.map((platform, index) =>
`#define STORAGE_DIR_LEGACY_${platform.toUpperCase()} "${legacyDirs[index]}"`).join('\n');
const content = `/* Generated by scripts/generate-storage-layout.mjs from proved Bend ${proof.version} policy. */
#ifndef STORAGE_LAYOUT_H
#define STORAGE_LAYOUT_H
#define STORAGE_DIR_NAME "${dirNames.Inbe}"
#define STORAGE_DB_NAME "${currentDb}"
#define STORAGE_DB_NAME_LEGACY "${legacyDb}"
#define STORAGE_DIR_ARCHIVE_SUFFIX ".before-breathing-migration-"
#define STORAGE_EXPORT_ENTRY_DB "${exportEntry}"
#define STORAGE_EXPORT_ENTRY_META "inbe-data/metadata.json"
#define STORAGE_EXPORT_META_FORMAT "inbe-data-sqlite"
#define STORAGE_EXPORT_PREFIX "${dirNames.Inbe}"
#define STORAGE_IMPORT_ENTRY_COUNT ${Object.keys(entryNames).length}
static const char *const storage_import_entries[STORAGE_IMPORT_ENTRY_COUNT] = {
"${exportEntry}",
"${entryNames.BreathingDataDb}"
};
#define STORAGE_WEB_HOME "/home/${dirNames.Inbe}"
#define STORAGE_WEB_HOME_LEGACY "/home/${legacyDirs[1]}"
#define STORAGE_IMPORT_DB_TMP "import-${dirNames.Inbe}.db"
#define STORAGE_IMPORT_DB_INSPECT "import-inspect-${dirNames.Inbe}.db"
#define STORAGE_EXPORT_DB_TMP "export-${dirNames.Inbe}.db"
#define STORAGE_EXPORT_FILENAME_FMT "${dirNames.Inbe}-%lld.zip"
#define STORAGE_EXPORT_FILENAME_FALLBACK "${dirNames.Inbe}.zip"
#define STORAGE_EXPORT_SESSIONS_CSV "${dirNames.Inbe}-sessions.csv"
#define STORAGE_WEB_EXPORT_TMP "/tmp/${dirNames.Inbe}-web-export.zip"
${platformDefines}
${legacyDefines}
#if defined(__EMSCRIPTEN__) || defined(PLATFORM_WEB)
#define STORAGE_EXPECT_DIR_HERE STORAGE_EXPECT_DIR_WEB
#elif defined(ANDROID_BUILD)
#define STORAGE_EXPECT_DIR_HERE STORAGE_EXPECT_DIR_ANDROID
#elif defined(_WIN32)
#define STORAGE_EXPECT_DIR_HERE STORAGE_EXPECT_DIR_WINDOWS
#else
#define STORAGE_EXPECT_DIR_HERE STORAGE_EXPECT_DIR_POSIX
#endif
#endif
`;
const ziran = `// Generated from the proved Bend storage layout. Do not edit by hand.
CurrentDirectoryName :: "${dirNames.Inbe}"
PreviousDirectoryPosix :: "${legacyDirs[3]}"
PreviousDirectoryWindows :: "${legacyDirs[2]}"
PreviousDirectoryAndroid :: "${legacyDirs[0]}"
PreviousDirectoryWeb :: "${legacyDirs[1]}"
ArchiveDirectorySuffix :: ".before-breathing-migration-"
CurrentDatabaseName :: "${currentDb}"
PreviousDatabaseName :: "${legacyDb}"
CurrentDatabaseEntry :: "${exportEntry}"
PreviousDatabaseEntry :: "${entryNames.BreathingDataDb}"
ExportMetadataEntry :: "inbe-data/metadata.json"
ExportMetadataFormat :: "inbe-data-sqlite"
ImportDatabaseTemporaryName :: "import-${dirNames.Inbe}.db"
InspectDatabaseTemporaryName :: "import-inspect-${dirNames.Inbe}.db"
ExportDatabaseTemporaryName :: "export-${dirNames.Inbe}.db"
ExportFilenameFormat :: "${dirNames.Inbe}-%lld.zip"
ExportFilenameFallback :: "${dirNames.Inbe}.zip"
ExportFilenamePrefix :: "${dirNames.Inbe}-"
ExportFilenameSuffix :: ".zip"
ExportSessionsCsv :: "${dirNames.Inbe}-sessions.csv"
CurrentWebHome :: "/home/${dirNames.Inbe}"
PreviousWebHome :: "/home/${legacyDirs[1]}"
WebExportTemporaryPath :: "/tmp/${dirNames.Inbe}-web-export.zip"
`;
function writeIfChanged(destination, text) {
fs.mkdirSync(path.dirname(destination), { recursive: true });
if (fs.existsSync(destination) && fs.readFileSync(destination, 'utf8') === text)
return;
const temporary = `${destination}.${process.pid}.tmp`;
try {
fs.writeFileSync(temporary, text);
fs.renameSync(temporary, destination);
} finally {
fs.rmSync(temporary, { force: true });
}
}
writeIfChanged(output, content);
writeIfChanged(ziranOutput, ziran);
return proof;
}
if (process.argv[1] && pathToFileURL(path.resolve(process.argv[1])).href === import.meta.url) {
try {
assert.ok(process.argv.length <= 4,
'usage: node scripts/generate-storage-layout.mjs [output.h] [output.zi]');
const output = process.argv[2] ?? path.join(root, 'build/proofs/storage_layout.h');
const ziranOutput = process.argv[3] ?? path.join(root, 'src/storage/storage_layout.zi');
const proof = await generateStorageLayout(path.join(root, 'laws/storage_layout'),
output, ziranOutput);
console.log(`storage.layout: ${proof.laws.length} Bend laws proved; current layout generated`);
} catch (error) {
console.error(error.message);
process.exitCode = 1;
}
}
-139
View File
@@ -1,139 +0,0 @@
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { checkLaws } from './bend-laws.mjs';
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const results = ['Success', 'InvalidUrl', 'NoAccount', 'PayloadFailed', 'ChallengeFailed',
'SignFailed', 'RequestFailed', 'AuthFailed', 'Unknown'];
const codes = ['SYNC_OK', 'SYNC_INVALID_URL', 'SYNC_NO_ACCOUNT', 'SYNC_PAYLOAD_FAILED',
'SYNC_CHALLENGE_FAILED', 'SYNC_SIGN_FAILED', 'SYNC_REQUEST_FAILED', 'SYNC_AUTH_FAILED'];
const attempts = ['Initial', 'First', 'Second', 'Third', 'Capped'];
const requiredLaws = ['success_resets', 'challenge_retries', 'request_retries',
'invalid_url_stops', 'no_account_stops', 'payload_failure_stops',
'signing_failure_stops', 'authentication_failure_stops', 'unknown_result_stops',
'saved_delay_matches', 'attempt_number_matches'];
export async function generateSyncRetry(packageDir, output, resultModule) {
const proof = await checkLaws(path.join(packageDir, 'PROOF.bend'));
assert.deepEqual(proof.laws.toSorted(), requiredLaws.toSorted(), 'sync.retry.required_laws');
const decisions = proof.table('main.decide');
const delays = proof.table('main.delay');
const domain = names => names.map(name => `main.${name}`);
assert.deepEqual(decisions.domains, [domain(results), domain(attempts)], 'sync.retry.domains');
assert.deepEqual(delays.domains, [domain(attempts)], 'sync.retry.delay_domain');
const enumBody = fs.readFileSync(resultModule, 'utf8').match(/SyncResult\s*::\s*enum\s*\{([^}]+)\}/)?.[1];
assert.ok(enumBody, 'sync.retry.result_enum_missing');
const enumEntries = enumBody.split('\n').map(entry => entry.replace(/\/\/.*/, '').trim()).filter(Boolean);
assert.deepEqual(enumEntries.map(entry => entry.match(/^(SYNC_[A-Z_]+)/)?.[1]),
codes, 'sync.retry.result_enum_changed');
let wireCode = -1;
for (let index = 0; index < enumEntries.length; index++) {
const entry = enumEntries[index].match(/^SYNC_[A-Z_]+\s*::\s*(-?\d+)\s*;?$/);
assert.ok(entry, 'sync.retry.result_enum_changed');
wireCode = Number(entry[1]);
assert.equal(wireCode, index, 'sync.retry.result_enum_changed');
}
const rows = decisions.rows.map(row => {
assert.equal(row.value.constructor, 'main.Decision');
assert.equal(row.value.fields.length, 3);
const [attempt, delay, action] = row.value.fields;
assert.ok(Number.isInteger(attempt) && attempt >= 0 && attempt <= 4);
assert.ok(Number.isInteger(delay) && delay >= 0 && delay <= 60);
assert.ok(action === 0 || action === 1);
return { attempt, delay, action };
});
assert.equal(rows.length, results.length * attempts.length,
'sync.retry.decision_count');
const seconds = delays.rows.map(row => {
assert.ok(Number.isInteger(row.value) && row.value > 0 && row.value <= 60);
return row.value;
});
assert.equal(seconds.length, attempts.length, 'sync.retry.delay_count');
const decisionCode = [];
for (let result = 0; result < results.length; result++) {
const indent = result === results.length - 1 ? '' : ' ';
if (result !== results.length - 1) {
decisionCode.push(` if result == ${result} {`);
}
decisionCode.push(`${indent} // ${results[result]}`);
for (let attempt = 0; attempt < attempts.length; attempt++) {
const row = rows[result * attempts.length + attempt];
if (attempt !== attempts.length - 1) {
decisionCode.push(`${indent} if attempt == ${attempt} {`);
decisionCode.push(`${indent} return RetryDecision.{${row.attempt}, ${row.delay}, ${row.action}}`);
decisionCode.push(`${indent} }`);
} else {
decisionCode.push(`${indent} return RetryDecision.{${row.attempt}, ${row.delay}, ${row.action}}`);
}
}
if (result !== results.length - 1) {
decisionCode.push(' }');
}
}
const delayCode = seconds.flatMap((delay, index) =>
index === seconds.length - 1
? [` return ${delay}.0`]
: [` if attempt == ${index} {`, ` return ${delay}.0`, ' }']);
const text = `#import, file "../storage/sync_result.zi";
// Generated by scripts/generate-sync-retry.mjs from proved Bend ${proof.version} policy.
// SyncResult wire codes are verified against src/storage/sync_result.zi.
RetryDecision :: struct {
attempt: s32
delay: s32
needs_action: s32
}
retry_attempt_index :: (attempt: s32) -> s32 {
if attempt < 0 { return 0 }
if attempt > 4 { return 4 }
return attempt
}
#program_export
sync_retry_decision :: (result: s32, attempt: s32) -> RetryDecision {
if result < 0 || result > 7 { result = 8 }
attempt = retry_attempt_index(attempt)
${decisionCode.join('\n')}
}
#program_export
app_sync_failure_needs_action :: (result: s32) -> s32 {
decision: RetryDecision = sync_retry_decision(result, 0)
return decision.needs_action
}
#program_export
app_sync_retry_delay :: (attempt: s32) -> float64 {
attempt = retry_attempt_index(attempt)
${delayCode.join('\n')}
}
`;
fs.mkdirSync(path.dirname(output), { recursive: true });
if (!fs.existsSync(output) || fs.readFileSync(output, 'utf8') !== text) {
const temporary = `${output}.${process.pid}.tmp`;
try {
fs.writeFileSync(temporary, text);
fs.renameSync(temporary, output);
} finally {
fs.rmSync(temporary, { force: true });
}
}
return proof;
}
if (process.argv[1] && pathToFileURL(path.resolve(process.argv[1])).href === import.meta.url) {
try {
assert.ok(process.argv.length <= 3, 'usage: node scripts/generate-sync-retry.mjs [output.zi]');
const output = path.resolve(process.argv[2] ?? path.join(root, 'src/app/sync_retry.zi'));
const proof = await generateSyncRetry(path.join(root, 'laws/sync_retry'), output,
path.join(root, 'src/storage/sync_result.zi'));
console.log(`sync.retry: ${proof.laws.length} Bend laws proved; 45 decisions generated`);
} catch (error) {
console.error(error.message);
process.exitCode = 1;
}
}
+6
View File
@@ -9,5 +9,11 @@ Answer :: () -> s32 {
if FontAssetForLocale("zh_Hant") != FONT_TC { return 5 }
if FontAssetForLocale("en") != FONT_LATIN { return 6 }
if FontAssetForLocale("") != FONT_LATIN { return 7 }
latin: [8]string = .["cs", "de", "es", "fr", "id", "it", "pt", "ru"]
index: s32 = 0
while index < 8 {
if FontAssetForLocale(latin[index]) != FONT_LATIN { return 10 + index }
index += 1
}
return 42
}
+6
View File
@@ -70,3 +70,9 @@ done
cmp "$work/source/font_assets.zib" "$work/saved/font_assets.zib"
echo "font assets Ziran test passed"
# Every font the locale mapping can pick is in the repository.
for font in $(sed -n 's/^FONT_[A-Z]* :: "\(.*\)"$/\1/p' "$root/src/app/font_assets.zi"); do
[ -f "$root/$font" ] || { echo "missing font file $font" >&2; exit 1; }
done
echo "font asset files present"
+87
View File
@@ -0,0 +1,87 @@
#!/usr/bin/env python3
"""The shipped subset fonts contain the glyphs the UI renders and users type.
Fonts used to be checked only as files on disk, which is how a subset
regeneration that dropped ae oe ue ss / e' n~ c~ shipped silently. Invariants:
1. The Latin subset covers every non-ASCII codepoint of the Latin/Cyrillic
locale files, the language index, and assets/fonts/input_common.txt (the
typed-input charset). Those locales render with the Latin font.
2. Every non-ASCII codepoint of the CJK locale files and the language index
is in at least one shipped font, so the picker and the cross-font
fallback can render it.
"""
from pathlib import Path
import sys
from fontTools.ttLib import TTFont
root = Path(__file__).resolve().parent.parent
fonts = {
"latin": root / "assets/fonts/subset/NotoSans-App-Regular.ttf",
"sc": root / "assets/fonts/subset/NotoSansSC-App-Regular.otf",
"jp": root / "assets/fonts/subset/NotoSansJP-App-Regular.otf",
"kr": root / "assets/fonts/subset/NotoSansKR-App-Regular.otf",
"tc": root / "assets/fonts/subset/NotoSansTC-App-Regular.otf",
}
LATIN_LOCALES = ["en", "es", "cs", "de", "fr", "id", "it", "pt", "ru"]
CJK_LOCALES = ["ja", "ko", "zh"]
def is_cjk(cp):
"""Scripts that render with the CJK subset fonts, never the Latin one."""
return (0x2E80 <= cp <= 0x303E or 0x3040 <= cp <= 0x30FF or
0x3130 <= cp <= 0x318F or 0x3400 <= cp <= 0x4DBF or
0x4E00 <= cp <= 0x9FFF or 0xAC00 <= cp <= 0xD7AF or
0xF900 <= cp <= 0xFAFF or 0xFF00 <= cp <= 0xFFEF)
def codepoints(*paths):
"""Non-ASCII codepoints of the files (ASCII is always covered)."""
found = set()
for path in paths:
for char in path.read_text(encoding="utf-8"):
cp = ord(char)
if cp > 0x7F and cp != 0xFEFF:
found.add(cp)
return found
def cmap_of(path):
if not path.exists():
return None
return TTFont(path).getBestCmap()
failures = 0
cmaps = {name: cmap_of(path) for name, path in fonts.items()}
if cmaps["latin"] is None:
print(f"FAIL: cannot parse cmap of {fonts['latin']}", file=sys.stderr)
sys.exit(1)
latin_needed = codepoints(
root / "assets/fonts/input_common.txt", root / "locales/index.txt",
*[root / f"locales/{name}.txt" for name in LATIN_LOCALES])
for cp in sorted(latin_needed):
if not is_cjk(cp) and cp not in cmaps["latin"]:
print(f"FAIL: U+{cp:04X} has no glyph in the Latin font", file=sys.stderr)
failures += 1
print(f"Latin font: {len(latin_needed)} required codepoints, {failures} missing")
cjk_needed = codepoints(
root / "locales/index.txt", *[root / f"locales/{name}.txt" for name in CJK_LOCALES])
opened = [cmap for cmap in cmaps.values() if cmap is not None]
if not opened:
print("FAIL: cannot parse any shipped font cmap", file=sys.stderr)
sys.exit(1)
before = failures
for cp in sorted(cjk_needed):
if not any(cp in cmap for cmap in opened):
print(f"FAIL: U+{cp:04X} has no glyph in ANY shipped font", file=sys.stderr)
failures += 1
print(f"CJK/picker: {len(cjk_needed)} required codepoints checked against "
f"{len(opened)} fonts, {failures - before} missing")
if failures:
print(f"font_glyph_coverage tests FAILED ({failures} missing glyphs)", file=sys.stderr)
sys.exit(1)
print("font_glyph_coverage tests passed")
+134
View File
@@ -0,0 +1,134 @@
#!/usr/bin/env python3
"""Locale files and the code that uses them agree on the set of keys.
Every locale file has exactly the English keys, each with a value and a `---`
separator; the four practice labels exist; every LocaleText / FormatLocaleText
call with a literal key names an English key; and every English key is used
somewhere (or is one of the keys chosen at run time, listed below).
"""
from pathlib import Path
import re
import sys
root = Path(__file__).resolve().parent.parent
failures = []
PRACTICE_LABELS = ["exercise_wim_hof", "exercise_patterns",
"exercise_meditation", "exercise_sun_salutation"]
# Keys picked by name at run time, so no literal use appears in the sources.
DYNAMIC_KEYS = """
exercise_wim_hof exercise_patterns exercise_meditation exercise_sun_salutation
meditation_music_download_button meditation_music_redownload_button
sync_alias_title sync_alias_change_title sync_alias_message
sync_alias_change_message sync_alias_register_button sync_alias_save_button
skip_button close_button sync_review_using_remote sync_review_keeping_local
habit_stats_no_rounds_month habit_stats_no_rounds_week habit_stats_day_singular
habit_stats_day_plural session_count_singular session_count_plural
practice_home_minutes practice_home_repetitions practice_home_wim_hof_metadata
habit_session_total_count habit_day_total_count_suffix deleted_sessions
sync_pull_ok sync_pull_conflict sync_push_ok sync_push_failed sync_sign_failed
sync_invalid_account sync_server_unreachable sync_server_error sync_connected
sync_auth_failed sync_failed sync_server_unavailable sync_status_connected
sync_status_disconnected tray_show_inner_breeze tray_hide_inner_breeze
tray_start_practice tray_quit_inner_breeze tray_break_settings
tray_mark_complete tray_wim_hof_paused tray_wim_hof_breath tray_wim_hof_hold
tray_wim_hof_breathe_in tray_wim_hof_next_round tray_wim_hof_starting_seconds
tray_meditation_paused tray_meditation_left tray_sun_salutation
tray_sun_salutation_paused tray_patterns_left tray_patterns_left_paused
tray_patterns_elapsed tray_patterns_elapsed_paused tray_next_break
theme_sky theme_ocean theme_forest theme_sunset theme_lavender theme_cherry
theme_dawn theme_sage theme_sepia theme_mono theme_mint theme_cobalt
tutorial_step_intro tutorial_step_method tutorial_step_breathe
tutorial_step_exhale_hold tutorial_step_inhale_hold
""".split() + ["sun_salutation_step_%d" % i for i in range(1, 13)]
def load_keys(path):
"""The keys of a locale file, recording format problems as failures."""
keys = []
pending = None
needs_separator = False
for line in path.read_text(encoding="utf-8").split("\n"):
line = line.rstrip("\r")
if line == "---":
needs_separator = False
header = re.match(r"^\[([^\]]+)\]", line)
if header:
key = header.group(1)
if needs_separator:
failures.append(f"{path.name} missing --- before [{key}]")
needs_separator = True
if pending is not None:
failures.append(f"{path.name} key [{pending}] has no value")
if key not in keys:
keys.append(key)
pending = key
elif pending is not None and line.strip() not in ("", "---"):
pending = None
if pending is not None:
failures.append(f"{path.name} key [{pending}] has no value")
return keys
def check_practice_labels(keys, name):
for label in PRACTICE_LABELS:
if label not in keys:
failures.append(f"{name} missing practice label [{label}]")
english = load_keys(root / "locales/en.txt")
english_set = set(english)
check_practice_labels(english, "en.txt")
sources = []
for directory in (root / "src", root / "build/packages/kryon/src"):
if directory.is_dir():
sources += sorted(p for p in directory.rglob("*") if p.suffix in (".zi", ".c", ".h"))
call = re.compile(r'\b(?:LocaleText|GetLocaleText|FormatLocaleText)\(\s*"([^"]*)"')
used = set(DYNAMIC_KEYS)
for source in sources:
text = source.read_text(encoding="utf-8", errors="replace")
for number, line in enumerate(text.split("\n"), 1):
for match in call.finditer(line):
if match.group(1) not in english_set:
failures.append(f"{source.relative_to(root)}:{number} locale call missing English key [{match.group(1)}]")
# Key literals also appear in data tables, not only in direct calls.
for literal in re.findall(r'"([^"\n]{1,127})"', text):
if literal in english_set:
used.add(literal)
# English strings nothing uses yet. Listing them keeps new dead strings from
# slipping in; delete a key from the locale files and from this list together.
allowed_unused = set()
allowlist = root / "tests/locale_unused_keys.txt"
if allowlist.exists():
allowed_unused = {line.strip() for line in allowlist.read_text().split("\n") if line.strip()}
for key in english:
if key not in used and key not in allowed_unused:
failures.append(f"en.txt unused key [{key}]")
for key in sorted(allowed_unused):
if key not in english_set:
failures.append(f"tests/locale_unused_keys.txt lists [{key}], which is not in en.txt")
elif key in used:
failures.append(f"tests/locale_unused_keys.txt lists [{key}], which is now used")
for path in sorted((root / "locales").glob("*.txt")):
if path.name in ("en.txt", "index.txt"):
continue
keys = load_keys(path)
check_practice_labels(keys, path.name)
have = set(keys)
for key in english:
if key not in have:
failures.append(f"{path.name} missing [{key}]")
for key in keys:
if key not in english_set:
failures.append(f"{path.name} extra key [{key}] not in en.txt")
if failures:
print("\n".join(failures), file=sys.stderr)
print(f"{len(failures)} locale key test failure(s)", file=sys.stderr)
sys.exit(1)
print("locale key tests passed")
+24
View File
@@ -0,0 +1,24 @@
export_health_connect_button
guide_title
habit_default_name_format
habit_mixed_practice_label
habit_schedule_custom
habit_tracking_hint
health_connect_started
meditation_music_archive_open_failed
meditation_music_file_path_too_long
ok_button
profile_friend_remove_message
sync_connected_as
sync_everything_up_to_date
sync_history_days_label
sync_list_items_label
sync_lists_label
sync_private_key_backup_cancelled
sync_public_id_label
sync_review_keep_local_button
sync_review_local_only
sync_review_needed
sync_review_remote_only
sync_review_use_remote_button
update_available_toast
-97
View File
@@ -1,97 +0,0 @@
import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { test } from 'node:test';
import { generateStorageLayout } from '../scripts/generate-storage-layout.mjs';
const root = path.resolve(import.meta.dirname, '..');
async function fixture(run) {
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'storage-layout-proof-'));
try {
const policy = path.join(directory, 'laws/storage_layout');
fs.cpSync(path.join(root, 'laws/storage_layout'), policy, { recursive: true });
const output = path.join(directory, 'storage_layout.h');
await run({ policy, output });
} finally {
fs.rmSync(directory, { recursive: true, force: true });
}
}
test('the checked policy generates the current layout deterministically', async () => {
await fixture(async ({ policy, output }) => {
const proof = await generateStorageLayout(policy, output);
assert.equal(proof.laws.length, 6);
const first = fs.readFileSync(output, 'utf8');
const ziranOutput = output.replace(/\.h$/, '.zi');
const ziran = fs.readFileSync(ziranOutput, 'utf8');
assert.match(first, /#define STORAGE_DIR_NAME "inbe"/);
assert.match(first, /#define STORAGE_DB_NAME "inbe\.db"/);
assert.match(first, /#define STORAGE_DB_NAME_LEGACY "breathing\.db"/);
assert.match(first, /#define STORAGE_DIR_LEGACY_WINDOWS "BreathSession"/);
assert.match(first, /#define STORAGE_EXPORT_ENTRY_DB "inbe-data\/inbe\.db"/);
assert.match(first, /#define STORAGE_IMPORT_ENTRY_COUNT 2/);
assert.match(first, /"breathing-data\/breathing\.db"/);
assert.match(first, /#define STORAGE_WEB_HOME "\/home\/inbe"/);
assert.match(ziran, /CurrentDatabaseEntry :: "inbe-data\/inbe\.db"/);
assert.match(ziran, /PreviousDirectoryWindows :: "BreathSession"/);
assert.match(ziran, /ArchiveDirectorySuffix :: "\.before-breathing-migration-"/);
assert.match(ziran, /PreviousDatabaseEntry :: "breathing-data\/breathing\.db"/);
assert.match(ziran, /ExportDatabaseTemporaryName :: "export-inbe\.db"/);
const mtime = fs.statSync(output).mtimeMs;
const ziranMtime = fs.statSync(ziranOutput).mtimeMs;
await generateStorageLayout(policy, output);
assert.equal(fs.statSync(output).mtimeMs, mtime);
assert.equal(fs.statSync(ziranOutput).mtimeMs, ziranMtime);
fs.writeFileSync(output, 'stale or hand-edited output');
fs.writeFileSync(ziranOutput, 'stale or hand-edited module');
await generateStorageLayout(policy, output);
assert.equal(fs.readFileSync(output, 'utf8'), first);
assert.equal(fs.readFileSync(ziranOutput, 'utf8'), ziran);
});
});
test('policies that change a proved layout guarantee fail', async () => {
const mutations = [
[text => text.replace(' case Windows{}:\n Inbe{}',
' case Windows{}:\n Breathing{}'),
'every platform selects the current directory'],
[text => text.replace('def current_db() -> DbName:\n InbeDb{}',
'def current_db() -> DbName:\n BreathingDb{}'),
'the database filename remains current'],
[text => text.replace(' case Windows{}:\n BreathSession{}',
' case Windows{}:\n Inbe{}'),
'the older directory remains distinct'],
[text => text.replace('def legacy_db() -> DbName:\n BreathingDb{}',
'def legacy_db() -> DbName:\n InbeDb{}'),
'the older database name remains stable'],
[text => text.replace('type Importable is Data:\n Yes{}',
'type Importable is Data:\n Yes{}\n No{}')
.replace(' case InbeDataDb{}:\n Yes{}',
' case InbeDataDb{}:\n No{}'),
'the export entry remains importable'],
[text => text.replace('type Importable is Data:\n Yes{}',
'type Importable is Data:\n Yes{}\n No{}')
.replace(' case BreathingDataDb{}:\n Yes{}',
' case BreathingDataDb{}:\n No{}'),
'the historical archive entry remains importable'],
];
for (const [mutate, reason] of mutations) {
await fixture(async ({ policy, output }) => {
await generateStorageLayout(policy, output);
const old = fs.readFileSync(output, 'utf8');
const ziranOutput = output.replace(/\.h$/, '.zi');
const oldZiran = fs.readFileSync(ziranOutput, 'utf8');
const source = path.join(policy, 'main.bend');
const before = fs.readFileSync(source, 'utf8');
const after = mutate(before);
assert.notEqual(after, before, `mutation target missing: ${reason}`);
fs.writeFileSync(source, after);
await assert.rejects(generateStorageLayout(policy, output),
/LAWS\.|storage\.layout/, reason);
assert.equal(fs.readFileSync(output, 'utf8'), old);
assert.equal(fs.readFileSync(ziranOutput, 'utf8'), oldZiran);
});
}
});
+43
View File
@@ -0,0 +1,43 @@
#import "byte_text_linux"
#import, file "storage_test_support.zi";
#import, file "../src/app/text_buffers.zi";
#import, file "../src/storage/data.zi";
#import, file "../src/storage/state.zi";
#import, file "../src/storage/db.zi";
#import, file "../src/storage/storage_core.zi";
#import, file "../src/storage/storage_layout.zi";
#import, file "../src/habits/habit_types.zi";
#import, file "../src/habits/habit_memory.zi";
#import, file "../src/habits/habit_model.zi";
#import, file "../src/storage/storage_sessions.zi";
#import, file "../src/storage/storage_habits.zi";
#import, file "../src/storage/session_mood.zi";
c :: #system_library "c";
write_out :: (descriptor: s32, data: *void, count: usize) -> s64 #foreign c "write";
// The storage vocabulary the whole app relies on.
constants :: () -> s32 {
check_true("export entry", CurrentDatabaseEntry == "inbe-data/inbe.db")
check_true("export metadata entry", ExportMetadataEntry == "inbe-data/metadata.json")
check_true("export metadata format", ExportMetadataFormat == "inbe-data-sqlite")
check_true("database name", CurrentDatabaseName == "inbe.db")
check_true("web home", CurrentWebHome == "/home/inbe")
check_true("posix directory", CurrentDirectoryName == "inbe")
check_true("older database name", PreviousDatabaseName == "breathing.db")
check_true("older database entry", PreviousDatabaseEntry == "breathing-data/breathing.db")
check_true("export filename prefix", ExportFilenamePrefix == "inbe-")
check_true("export filename suffix", ExportFilenameSuffix == ".zip")
return failures
}
// The data root, written to standard output. data_root() caches its answer,
// so the runner starts one process per environment.
#program_export
Check :: () -> s32 {
if constants() != 0 { return 1 }
root := data_root()
text: string = root
unused write_out(1, cast(*void)pointer_of(text), cast(usize)text.count)
return 0
}
+92
View File
@@ -0,0 +1,92 @@
#!/bin/sh
set -eu
# Where the app keeps its data: the override, XDG, HOME, moving an older
# directory into place, and archiving a conflicting one. data_root() caches
# its result, so each scenario is a fresh process with its own environment.
root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
ziran=${1:-"$root/build/ziran-toolchain/bin/ziran"}
work=$root/build/storage-paths-scenarios-zi-test
include=$root/build/packages/ziran/include
rm -rf "$work"
mkdir -p "$work/generated"
"$ziran" build --target=c --define PLATFORM_DESKTOP --root "$root/tests" \
--module-path "$root/src" \
--module-path "$root/build/packages/kryon/src/ui" \
--module-path "$root/build/packages/kryon/src/kss" \
--module-path "$root/build/packages/kryon/src/backend" \
--module-path "$root/build/packages/game2d/src" \
--module-path "$root/build/packages/ziran/std" \
--module-path "$root/build/packages/daochi-client" \
--entry storage_paths_behavior:Check \
-o "$work/generated" "$root/tests/storage_paths_behavior.zi" \
"$root/tests/sync_test_host.zi"
cat > "$work/generated/main.c" <<'C'
#include <stdio.h>
#include "storage_paths_behavior.h"
int main(void)
{
int result = Check();
if (result != 0) fprintf(stderr, "storage paths check failed with code %d\n", result);
return result == 0 ? 0 : 1;
}
C
"${CC:-cc}" -std=c11 -O0 -ffunction-sections -fdata-sections \
-I"$include" -I"$work/generated" -I"$root/vendor-builds/sqlite" \
"$work/generated"/*.c "$root/vendor-builds/sqlite/sqlite3.c" \
-Wl,--gc-sections -ldl -lpthread -lz -lm -o "$work/test"
fixture=$(mktemp -d /tmp/inbe-storage-paths-XXXXXX)
trap 'rm -rf "$fixture"' EXIT HUP INT TERM
failures=0
expect_root() {
label=$1
wanted=$2
shift 2
got=$(env -u DISPLAY -u WAYLAND_DISPLAY "$@" "$work/test")
if [ "$got" != "$wanted" ]; then
echo "FAIL $label: got $got, want $wanted" >&2
failures=$((failures + 1))
fi
if [ ! -d "$wanted" ]; then
echo "FAIL $label: root was not created" >&2
failures=$((failures + 1))
fi
}
expect_root "override root" "$fixture/override-root" \
APP_DATA_ROOT="$fixture/override-root"
expect_root "xdg root" "$fixture/inbe" \
-u APP_DATA_ROOT XDG_DATA_HOME="$fixture"
expect_root "home root" "$fixture/.local/share/inbe" \
-u APP_DATA_ROOT -u XDG_DATA_HOME HOME="$fixture"
# An older directory is moved into place, contents and all.
base=$fixture/legacy-home
mkdir -p "$base/.local/share/breathing"
echo data > "$base/.local/share/breathing/sentinel.txt"
expect_root "old directory is selected as current" "$base/.local/share/inbe" \
-u APP_DATA_ROOT -u XDG_DATA_HOME HOME="$base"
[ -f "$base/.local/share/inbe/sentinel.txt" ] || { echo "FAIL old content moved" >&2; failures=$((failures + 1)); }
[ ! -e "$base/.local/share/breathing" ] || { echo "FAIL old directory moved away" >&2; failures=$((failures + 1)); }
# A conflicting current directory is archived before the older one moves in.
base=$fixture/conflict-home
mkdir -p "$base/.local/share/breathing" "$base/.local/share/inbe"
echo legacy > "$base/.local/share/breathing/legacy.txt"
echo current > "$base/.local/share/inbe/current.txt"
expect_root "conflicting directory resolves to current" "$base/.local/share/inbe" \
-u APP_DATA_ROOT -u XDG_DATA_HOME HOME="$base"
[ -f "$base/.local/share/inbe/legacy.txt" ] || { echo "FAIL old data moved into the current root" >&2; failures=$((failures + 1)); }
[ -f "$base/.local/share/inbe.before-breathing-migration-1/current.txt" ] || { echo "FAIL prior current data archived" >&2; failures=$((failures + 1)); }
[ ! -e "$base/.local/share/breathing" ] || { echo "FAIL old directory moved away (conflict)" >&2; failures=$((failures + 1)); }
if [ "$failures" -ne 0 ]; then
echo "$failures storage path check(s) failed" >&2
exit 1
fi
echo "storage path scenarios passed"
-115
View File
@@ -1,115 +0,0 @@
import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { spawnSync } from 'node:child_process';
import { test } from 'node:test';
import { generateSyncRetry } from '../scripts/generate-sync-retry.mjs';
const root = path.resolve(import.meta.dirname, '..');
const resultModule = path.join(root, 'src/storage/sync_result.zi');
async function fixture(run) {
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'sync-retry-proof-'));
try {
fs.cpSync(path.join(root, 'laws/sync_retry'), path.join(directory, 'laws/sync_retry'), { recursive: true });
const policy = path.join(directory, 'laws/sync_retry');
const output = path.join(directory, 'src/app/sync_retry.zi');
await run({ directory, policy, output });
} finally {
fs.rmSync(directory, { recursive: true, force: true });
}
}
test('proved table generation is deterministic and repairs stale generated content', async () => {
await fixture(async ({ policy, output }) => {
const proof = await generateSyncRetry(policy, output, resultModule);
assert.equal(proof.laws.length, 11);
const first = fs.readFileSync(output, 'utf8');
const mtime = fs.statSync(output).mtimeMs;
await generateSyncRetry(policy, output, resultModule);
assert.equal(fs.statSync(output).mtimeMs, mtime);
fs.writeFileSync(output, 'stale or hand-edited output');
await generateSyncRetry(policy, output, resultModule);
assert.equal(fs.readFileSync(output, 'utf8'), first);
});
});
test('policy changes that break each class of behavior fail their proofs', async () => {
const mutations = [
[' 60', ' 61'],
[' Decision{0, 0, 0}', ' Decision{1, 0, 0}'],
[' case AuthFailed{}:\n stop(attempt)', ' case AuthFailed{}:\n retry(attempt)'],
[' case Capped{}:\n Capped{}', ' case Capped{}:\n Initial{}'],
];
for (const [before, after] of mutations) {
await fixture(async ({ policy, output }) => {
await generateSyncRetry(policy, output, resultModule);
const old = fs.readFileSync(output, 'utf8');
const source = path.join(policy, 'main.bend');
const text = fs.readFileSync(source, 'utf8');
assert.ok(text.includes(before));
fs.writeFileSync(source, text.replace(before, after));
await assert.rejects(generateSyncRetry(policy, output, resultModule), /LAWS\./);
assert.equal(fs.readFileSync(output, 'utf8'), old);
});
}
});
test('changed runtime enum cannot silently acquire an unproved mapping', async () => {
await fixture(async ({ directory, policy, output }) => {
const changedModule = path.join(directory, 'sync_result.zi');
fs.writeFileSync(changedModule, fs.readFileSync(resultModule, 'utf8').replace('SYNC_AUTH_FAILED', 'SYNC_NEW_RESULT'));
await assert.rejects(generateSyncRetry(policy, output, changedModule), /result_enum_changed/);
});
});
test('generation rejects renumbered result codes', async () => {
await fixture(async ({ directory, policy, output }) => {
const changedModule = path.join(directory, 'sync_result.zi');
fs.writeFileSync(changedModule, fs.readFileSync(resultModule, 'utf8').replace('SYNC_OK :: 0', 'SYNC_OK :: 1'));
await assert.rejects(generateSyncRetry(policy, output, changedModule),
/result_enum_changed/);
assert.equal(fs.existsSync(output), false);
});
});
test('actual Make proof prerequisite rejects broken policy despite existing Ziran source', async () => {
await fixture(async ({ directory, policy, output }) => {
fs.mkdirSync(path.join(directory, 'scripts'));
fs.mkdirSync(path.join(directory, 'vendor'));
fs.mkdirSync(path.join(directory, 'src/core'), { recursive: true });
fs.mkdirSync(path.join(directory, 'src/storage'), { recursive: true });
for (const file of ['scripts/bend-laws.mjs', 'scripts/bend-pin.json',
'scripts/generate-sync-retry.mjs', 'scripts/generate-storage-layout.mjs',
'src/storage/sync_result.zi', 'src/core/version.h', 'Makefile']) {
fs.copyFileSync(path.join(root, file), path.join(directory, file));
}
fs.cpSync(path.join(root, 'laws/storage_layout'), path.join(directory, 'laws/storage_layout'),
{ recursive: true });
fs.mkdirSync(path.join(directory, 'build/packages'), { recursive: true });
// The package links below are already in place; the manifest and a
// newer packages.mk keep Make from fetching them again.
for (const file of ['ziran.toml', 'ziran.lock', 'scripts/packages.sh', 'mk/sync.mk']) {
fs.mkdirSync(path.dirname(path.join(directory, file)), { recursive: true });
fs.copyFileSync(path.join(root, file), path.join(directory, file));
}
fs.writeFileSync(path.join(directory, 'build/packages.mk'), 'PACKAGES_READY := 1\n');
fs.symlinkSync(path.join(root, 'build/packages/kryon'), path.join(directory, 'build/packages/kryon'), 'dir');
fs.symlinkSync(path.join(root, 'build/packages/bend'), path.join(directory, 'build/packages/bend'), 'dir');
const make = () => spawnSync('make', ['src/app/sync_retry.zi'], {
cwd: directory, encoding: 'utf8', timeout: 30000,
});
const good = make();
assert.ifError(good.error);
assert.equal(good.status, 0, good.stdout + good.stderr);
const old = fs.readFileSync(output, 'utf8');
const source = path.join(policy, 'main.bend');
fs.writeFileSync(source, fs.readFileSync(source, 'utf8').replace(' 60', ' 61'));
const bad = make();
assert.ifError(bad.error);
assert.notEqual(bad.status, 0);
assert.match(bad.stdout + bad.stderr, /LAWS\./);
assert.equal(fs.readFileSync(output, 'utf8'), old);
});
});