Files
breeze/tests/media_store_behavior.zi
waozi 017d3565d3 Build on Ziran 0.5.0 and Kryon's namespaced widgets, with the Tao picture page
Pin only pushed commits, with no local overrides: Ziran v0.5.0
(f9f92b46), Kryon b45cc524 (one lock entry, ref master, shared with the
Css and Plot master edges), Apps 352901e, Entity b30dd4a and practices
893e5b6f, whose sharpened banners and regenerated Lumi guide clips the
narration check now matches.

The Library opens each Tao Te Ching chapter on its own picture page: the
chapter art and its title, centered together; the text pages follow.
The reader keeps the picture page across reflow, and a chapter start or
a Contents pick opens it. The 81 approved chapter pictures are included.

Breeze now uses Kryon's namespaced widgets (Text.Draw/Text.Props,
Button, Image, Modal.ActionModal, Layout, TabBar, LoadingScreen,
LoadingIndicator, Shape.Circle) instead of the removed Kryon/Widgets
facades. Hosted apps import std/text explicitly, since a short "text"
import now names Kryon's widget. AGENTS.md and the Text/Button API checks
describe the namespaced forms; two unused test doubles are removed.

Build fixes for the new toolchain: the bundle runtime carries Ziran's
instruction engine (no BOOTSTRAP=1) and takes the pinned host zi2c for
cross builds; the Apps controller's lock context has the manifest's
module roots; the app codec generator lists Kryon's raw modules only;
the package scripts read ZX revision 28 bundle assets. The Apps
controller digest matches the Apps 352901e build.

Also included: the stopped lanes' work in this tree (Radio download,
Photos install, Lists, Lumi, Habits shop rewards, astral exercise
selection, appearance and download recovery) and the raylib-free desktop
lane's host modules (app_trace, host_input, host_window, host_clock),
inactive unless DESKTOP_HOST is defined.

Not included: the crash-recovery lane's picture-window and storage
crash-safety changes, which that lane commits itself.

Local tests: zi-check (510 modules), native and Android source checks,
hosted app checks, build-laws, clean-text-api-check, button-api-check,
library-reader-test, library-ui-test, storage-crash-zi-test and
native-image-decode-test pass. The bundled Arcade is still ZX revision 26
and waits for an Arcade build on these pins.
2026-11-16 14:20:12 -03:00

380 lines
20 KiB
Ziran

// The account's media library on a real database: a photo is kept once by
// its hash as a record of the account's entity data, with its capture day
// and a JPEG preview, Trash keeps it, queries list it by day or all newest
// first, a released version's library record lists until its photo moves,
// and only this account's photos draw.
#import "byte_text_linux"
#import "vec"
#import "text"
#import "file_linux"
#import "json_scan"
#import "text_buffer"
#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_settings.zi";
#import, file "../src/storage/media_records.zi";
#import, file "../src/storage/entity_records.zi";
#import, file "../src/apps/entity_photos.zi";
#import, file "../src/apps/package_files.zi";
#import, file "../src/apps/diary_records.zi";
#import, file "../src/apps/media_store.zi";
#import, file "../src/apps/media_metadata.zi";
#import, file "../src/apps/media_compression_store.zi";
Byte :: (out: *Vec(u8), value: s64) {
unused VecPush(out.*, cast(u8)(value & 255))
}
Word :: (out: *Vec(u8), value: s64) {
Byte(out, value >> 8)
Byte(out, value)
}
Long :: (out: *Vec(u8), value: s64) {
Word(out, value >> 16)
Word(out, value)
}
Chars :: (out: *Vec(u8), value: string) {
for byte: value {
unused VecPush(out.*, byte)
}
}
// A big-endian Exif block with the original date and the orientation.
Tiff :: (out: *Vec(u8), date: string, orientation: s64) {
Chars(out, "MM")
Word(out, 42)
Long(out, 8)
exif := 8 + 2 + 2 * 12 + 4
Word(out, 2)
Word(out, 274)
Word(out, 3)
Long(out, 1)
Word(out, orientation)
Word(out, 0)
Word(out, 34665)
Word(out, 4)
Long(out, 1)
Long(out, exif)
Long(out, 0)
data := exif + 2 + 12 + 4
Word(out, 1)
Word(out, 36867)
Word(out, 2)
Long(out, cast(s64)date.count + 1)
Long(out, data)
Long(out, 0)
Chars(out, date)
Byte(out, 0)
}
// A JPEG-shaped file: its Exif segment, then `filler` bytes of a fake scan
// (the test's decoder reads every file as the same gradient).
Photo :: (date: string, orientation: s64, filler: s64, seed: s64) -> string {
tiff: Vec(u8)
Tiff(*tiff, date, orientation)
out: Vec(u8)
Byte(*out, 255)
Byte(*out, 216)
Byte(*out, 255)
Byte(*out, 225)
Word(*out, 2 + 6 + tiff.count)
Chars(*out, "Exif")
Byte(*out, 0)
Byte(*out, 0)
for byte: VecSlice(tiff, 0, tiff.count) {
unused VecPush(out, byte)
}
index: s64 = 0
while index < filler {
Byte(*out, (index * 7 + seed) % 251)
index += 1
}
Byte(*out, 255)
Byte(*out, 217)
return BuilderFinish(out)
}
Hash :: (data: string) -> string {
output: [65]u8
descriptor := cast(*TextDescriptor)(*data)
storage_sha256_hex(descriptor.data, data.count, *output[0])
return PackageText(TextFromCString(*output[0]), "")
}
RecordField :: (hash: string, field: string, output: []u8) -> string {
key := EntityPhotoKey(hash)
record := MediaRecordRead(key)
count := ReadStringText(record, FieldAt(record, 0, field), output)
PackageFree(key)
PackageFree(record)
if count <= 0 {
return ""
}
return TextFromBytes(output[:count])
}
WriteSource :: (name: string, data: string) -> string {
buffer: TextBuffer
append(*buffer, data_root())
append(*buffer, "/")
append(*buffer, name)
path := PackageText(TextFromCString(*buffer.bytes[0]), "")
unused RemoveFile(path)
file := OpenExclusivePrivate(path)
written := file.valid && WriteTextAt(file, data, 0) == data.count
check_true("write a photo to pick", CloseFile(file) && written)
return path
}
#program_export
Check :: () -> s32 {
guard := support_begin("/mnt/storage/Projects/waozixyz/apps/breeze/build/media-store-zi-test/")
if guard != 0 { return guard }
root := make_root("media-library")
check_true("init library device", storage_init(root) != 0)
storage_set_setting_text("sync_public_id", "media-test-account")
morning := Photo("2026:10:17 08:15:00", 6, 60000, 1)
morning_hash := Hash(morning)
source := WriteSource("IMG_2041.jpg", morning)
added := MediaImportFile(source)
check_true("a picked photo is added", added.succeeded && !added.existed)
check_true("it is kept by its hash", TextFromBytes(added.hash[:64]) == morning_hash)
field: [256]u8
check_true("its capture day is the camera's", RecordField(morning_hash, "day", field[:]) == "2026-10-17")
check_true("its capture time is the camera's",
RecordField(morning_hash, "taken", field[:]) == "2026-10-17T08:15:00")
check_true("its name is the file's", RecordField(morning_hash, "name", field[:]) == "IMG_2041.jpg")
check_true("its type is read from its bytes", RecordField(morning_hash, "type", field[:]) == "image/jpeg")
item := MediaRead(morning_hash)
check_true("the record reads back", item.valid && item.entity && item.size == morning.count && !item.trashed)
check_int("one record of the account's entity data queues to sync",
read_db_count("SELECT COUNT(*) FROM sync_outbox WHERE entity_type='entity_record'"), 1)
check_int("nothing goes to the released versions' library",
read_db_count("SELECT COUNT(*) FROM settings WHERE key GLOB 'cell.media.*'"), 0)
check_int("its capture time is in seconds, read as UTC without an offset",
read_db_count(PackageText(PackageText("SELECT json_extract(value,'$.taken_at') FROM settings WHERE key='entity.photos.",
morning_hash), "'")), 1792224900)
check_int("it names no chunks before a node has them",
read_db_count("SELECT COUNT(*) FROM settings WHERE key GLOB 'entity.photos.*' AND json_type(value,'$.blobs') IS NOT NULL"), 0)
// The preview: an upright JPEG at most 512 pixels on its longer side.
preview_path := MediaPath(morning_hash, true)
preview := PackageRead(preview_path, DIARY_FILE_LIMIT)
check_true("a preview is made", preview.count > 100 && preview.count < morning.count)
check_true("the preview is a JPEG", preview[0] == cast(u8)255 && preview[1] == cast(u8)216 &&
preview[preview.count - 2] == cast(u8)255 && preview[preview.count - 1] == cast(u8)217)
// SOF0 says height then width: a 600 by 400 photo turned by orientation
// 6 is 341 by 512 upright.
sof: s64 = 2
while sof + 9 < preview.count && !(preview[sof] == cast(u8)255 && preview[sof + 1] == cast(u8)192) {
sof += 1
}
preview_height := cast(s32)preview[sof + 5] * 256 + cast(s32)preview[sof + 6]
preview_width := cast(s32)preview[sof + 7] * 256 + cast(s32)preview[sof + 8]
check_int("the preview is upright and 512 tall", preview_height, 512)
check_int("the preview is 341 wide", preview_width, 341)
photo_record := MediaEntityRecord(morning_hash)
preview_content := EntityPreviewContent(photo_record)
check_true("the photo's record names its preview's bytes",
EntityContentMatches(preview_content, preview) && TextUntilNul(preview_content.format[:]) == "jpg")
PackageFree(photo_record)
// Assets: the preview draws; asking for the original finds it here.
preview_asset := PackageText(MediaAsset(morning_hash, false), "")
check_true("the gallery draws the preview", preview_asset == PackageText("media/preview/",
PackageText(morning_hash, ".jpg")))
view := MediaAssetView(preview_asset)
check_true("the preview's bytes are served", view.found && view.mime == "image/jpeg" && view.data == preview)
original_asset := PackageText(MediaAsset(morning_hash, true), "")
check_true("the viewer draws the original", original_asset == PackageText("media/",
PackageText(morning_hash, ".jpg")))
check_true("the original's bytes are served", MediaAssetView(original_asset).data == morning)
check_true("a library photo may be drawn", MediaAssetAllowed(preview_asset) && MediaAssetAllowed(original_asset))
stranger := "media/0000000000000000000000000000000000000000000000000000000000000000.jpg"
check_true("a photo outside the library may not", !MediaAssetAllowed(stranger))
check_true("other paths may not", !MediaAssetAllowed("media/../inbe.db") &&
!MediaAssetAllowed(PackageText("media/", PackageText(morning_hash, ".exe"))))
check_true("a changed file is not served",
!MediaAssetView(PackageText("media/", PackageText(morning_hash, ".png"))).found)
// An original that is not on this device is asked for; the preview
// shows meanwhile.
original_path := MediaPath(morning_hash, false)
check_true("drop the original", RemoveFile(original_path))
MediaAssetsChanged()
check_true("not wanted before it is opened", !MediaOriginalWanted(morning_hash))
check_true("the preview stands in for a missing original",
MediaAsset(morning_hash, true) == preview_asset)
check_true("opening it asks for the original", MediaOriginalWanted(morning_hash))
// The same bytes again add nothing; the file comes back.
queued := read_db_count("SELECT COUNT(*) FROM sync_outbox WHERE entity_type='entity_record'")
again := MediaImportBytes(morning, "copy of IMG_2041.jpg")
check_true("the same photo is added once", again.succeeded && again.existed)
check_int("nothing new is queued",
read_db_count("SELECT COUNT(*) FROM sync_outbox WHERE entity_type='entity_record'"), queued)
check_true("its name stays", RecordField(morning_hash, "name", field[:]) == "IMG_2041.jpg")
check_true("the file is back", PackageRead(original_path, DIARY_FILE_LIMIT) == morning)
// Queries: newest first, by day or all, Trash apart.
evening := Photo("2026:10:17 19:40:00", 1, 60000, 2)
evening_hash := Hash(evening)
earlier := Photo("2026:10:15 12:00:00", 1, 60000, 3)
earlier_hash := Hash(earlier)
check_true("add the evening photo", MediaImportBytes(evening, "evening.jpg").succeeded)
check_true("add an earlier day's photo", MediaImportBytes(earlier, "earlier.jpg").succeeded)
check_int("all photos", MediaQuery("", false), 3)
first := MediaQueryRow(0)
second := MediaQueryRow(1)
third := MediaQueryRow(2)
check_true("the newest first", TextUntilNul(first.hash[:]) == evening_hash &&
TextUntilNul(second.hash[:]) == morning_hash && TextUntilNul(third.hash[:]) == earlier_hash)
check_true("each with its day", TextUntilNul(third.day[:]) == "2026-10-15")
check_int("one day's photos", MediaQuery("2026-10-17", false), 2)
check_int("another day's", MediaQuery("2026-10-15", false), 1)
check_int("a day without photos", MediaQuery("2026-10-16", false), 0)
check_int("a malformed day lists nothing", MediaQuery("2026-10", false), 0)
check_true("move a photo to Trash", MediaSetTrashed(evening_hash, true))
check_int("Trash leaves the gallery", MediaQuery("", false), 2)
check_int("Trash lists it", MediaQuery("", true), 1)
check_true("Trash keeps the photo", MediaRead(evening_hash).trashed &&
PackageFileSize(MediaPath(evening_hash, false)) == evening.count)
check_true("restore it", MediaSetTrashed(evening_hash, false))
check_int("it is back", MediaQuery("", false), 3)
check_true("trash it again", MediaSetTrashed(evening_hash, true))
check_true("adding it again brings it back", MediaImportBytes(evening, "evening.jpg").existed &&
!MediaRead(evening_hash).trashed)
check_true("rename a photo", MediaSetName(earlier_hash, "Lake \"walk\"\n.jpg"))
check_true("the name is one line", RecordField(earlier_hash, "name", field[:]) == "Lake \"walk\" .jpg")
// Fields another app added stay when this one changes the record.
key := EntityPhotoKey(earlier_hash)
record := MediaRecordRead(key)
tagged := MediaRecordWith(record, "caption", "\"By the lake\"")
check_true("another app adds a field", MediaRecordWrite(key, tagged))
check_true("trash it with the field", MediaSetTrashed(earlier_hash, true) && MediaSetTrashed(earlier_hash, false))
check_true("the other app's field stays", RecordField(earlier_hash, "caption", field[:]) == "By the lake" &&
RecordField(earlier_hash, "day", field[:]) == "2026-10-15")
check_int("Trash is true or false in the shared record",
read_db_count(PackageText(PackageText("SELECT json_type(value,'$.trashed')='false' FROM settings WHERE key='entity.photos.",
earlier_hash), "'")), 1)
// A released version's library record lists its photo until the photo
// moves; its entity record then keeps its name, day, Trash and the time
// it was added, and the old record stays for released versions.
older := Photo("2026:10:12 09:00:00", 1, 50000, 6)
older_hash := Hash(older)
older_key := PackageText(MEDIA_RECORD_PREFIX, older_hash)
older_out: Vec(u8)
BuilderPrint(*older_out, "{\"hash\":\"%\",\"size\":%,\"type\":\"jpg\",\"name\":\"Old walk.jpg\",", older_hash, older.count)
BuilderPrint(*older_out, "\"day\":\"2026-10-12\",\"taken\":\"2026-10-12T09:00:00\",\"added\":1792000000,")
BuilderPrint(*older_out, "\"capture\":1,\"trashed\":1,\"blob\":\"%\",\"key\":\"%\"}",
"abababababababababababababababababababababababababababababababab",
"cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd")
older_record := BuilderFinish(older_out)
check_true("a released version's record arrives", MediaRecordWrite(older_key, older_record))
older_item := MediaRead(older_hash)
check_true("its photo lists from it", older_item.valid && !older_item.entity && older_item.trashed &&
TextUntilNul(older_item.day[:]) == "2026-10-12")
check_int("Trash lists it", MediaQuery("", true), 1)
older_path := MediaPath(older_hash, false)
check_true("its bytes arrive", PackageWrite(older_path, older))
moved := MediaAddBytes(older, MediaAddition.{.source = older_path})
check_true("the photo moves", moved.succeeded && moved.existed && MediaRead(older_hash).entity)
check_true("its name and day stay", RecordField(older_hash, "name", field[:]) == "Old walk.jpg" &&
RecordField(older_hash, "day", field[:]) == "2026-10-12" &&
RecordField(older_hash, "taken", field[:]) == "2026-10-12T09:00:00")
check_true("it stays in Trash", MediaRead(older_hash).trashed)
check_int("the time it was added stays",
read_db_count(PackageText(PackageText("SELECT json_extract(value,'$.added_at') FROM settings WHERE key='entity.photos.",
older_hash), "'")), 1792000000)
check_true("the old record stays as it was", MediaRecordRead(older_key) == older_record)
check_int("Trash lists the photo once", MediaQuery("", true), 1)
check_true("restore it", MediaSetTrashed(older_hash, false))
check_true("the old record is not touched", MediaRecordRead(older_key) == older_record)
// Files that are not photos are refused.
check_true("text is no photo", !MediaImportBytes("just some text", "notes.txt").succeeded)
check_true("an empty file is no photo", !MediaImportBytes("", "empty.jpg").succeeded)
// Preparing/cancelling a comparison and rejected Apply actions leave
// the source bytes, records and references unchanged.
compression_source := MediaCompressionSource(morning_hash)
compression_record := MediaEntityRecord(morning_hash)
compression_queued := read_db_count("SELECT COUNT(*) FROM sync_outbox")
check_true("comparison reads the original", compression_source == morning)
PackageFree(compression_source)
check_true("a cancelled comparison keeps the record", MediaEntityRecord(morning_hash) == compression_record)
check_true("an empty encode is refused", !MediaCompressionCommit(
AppTextFromCString(storage_user_id_buffer(), 64), morning_hash, morning, "", 35))
check_true("invalid output is refused", !MediaCompressionCommit(
AppTextFromCString(storage_user_id_buffer(), 64), morning_hash, morning, "broken", 35))
check_true("another account cannot apply it", !MediaCompressionCommit(
"another-account", morning_hash, morning, preview, 35))
check_true("quality outside the control is refused", !MediaCompressionCommit(
AppTextFromCString(storage_user_id_buffer(), 64), morning_hash, morning, preview, 101))
check_true("failed Apply keeps original bytes and references", PackageRead(original_path, DIARY_FILE_LIMIT) == morning &&
MediaEntityRecord(morning_hash) == compression_record)
check_int("cancel and failures queue no mutations", read_db_count("SELECT COUNT(*) FROM sync_outbox"), compression_queued)
// A generated AVIF is content behind the stable original identity.
// It must not appear again as a gallery row, including in Trash.
target_hash := "abababababababababababababababababababababababababababababababab"
target_key := EntityPhotoKey(target_hash)
target_record := MediaCompressionTargetRecord(compression_record, target_hash, 100, 400, 600)
before_count := MediaQuery("", false)
check_true("a generated copy is recorded", MediaRecordWrite(target_key, target_record))
check_int("a generated copy adds no gallery row", MediaQuery("", false), before_count)
check_true("the generated copy has no inherited preview", !EntityPreviewContent(target_record).valid)
check_true("a generated copy can be trashed", MediaSetTrashed(target_hash, true))
check_int("a generated copy adds no Trash row", MediaQuery("", true), 0)
PackageFree(target_record)
PackageFree(target_key)
PackageFree(compression_record)
// Another account on this device sees none of these.
storage_close()
other := make_root("media-library-other")
check_true("init another account", storage_init(other) != 0)
check_int("another account's library is empty", MediaQuery("", false), 0)
check_true("another account cannot draw them", MediaAsset(morning_hash, false) == "" &&
!MediaAssetAllowed(preview_asset))
// Both photo pickers use this same import. Only the library copy is
// optimized; the picked source and camera date remain intact.
large := Photo("2026:10:18 11:22:33", 6, 600000, 4)
large_source := WriteSource("camera.jpg", large)
optimized := MediaImportFile(large_source)
optimized_hash := PackageText(TextUntilNul(optimized.hash[:]), "")
optimized_bytes := PackageRead(MediaPath(optimized_hash, false), DIARY_FILE_LIMIT)
check_true("a large photo is optimized", optimized.succeeded &&
optimized_bytes.count > 100 && optimized_bytes.count * 10 < large.count * 9)
check_true("its identity matches the saved copy", Hash(optimized_bytes) == optimized_hash &&
optimized_hash != Hash(large))
check_true("the picked source is untouched", PackageRead(large_source, DIARY_FILE_LIMIT) == large)
check_true("the optimized copy keeps its capture date",
RecordField(optimized_hash, "taken", field[:]) == "2026-10-18T11:22:33")
check_true("rotation is baked into the saved copy", MediaOrientationOf(optimized_bytes, "jpg") == 1)
repeated := MediaImportFile(large_source)
check_true("picking the source again keeps one optimized copy", repeated.succeeded && repeated.existed &&
TextUntilNul(repeated.hash[:]) == optimized_hash && MediaQuery("", false) == 1)
// Existing Diary imports keep their hashes and embeds when picked again.
legacy := Photo("2026:10:19 12:34:56", 1, 600000, 5)
legacy_hash := Hash(legacy)
check_true("keep an older full-sized photo", MediaAddBytes(legacy,
MediaAddition.{.name = "older.jpg"}).succeeded)
legacy_again := MediaImportBytes(legacy, "older copy.jpg")
check_true("existing original identities survive compression", legacy_again.succeeded && legacy_again.existed &&
TextUntilNul(legacy_again.hash[:]) == legacy_hash &&
PackageRead(MediaPath(legacy_hash, false), DIARY_FILE_LIMIT) == legacy)
PackageFree(optimized_bytes)
PackageFree(optimized_hash)
storage_close()
return failures
}