Files
breeze/tests/habits_shop_apps_behavior.zi
waozi ef0de86e02
Cloudflare Pages / Build and verify the site (push) Successful in 3m29s
Integrate Breeze desktop, Android, and app updates
Use the verified dependency lock across native hosts and bundled apps. Preserve saved styles, local app reactivation, and offline packages while integrating the latest practice, Habits, Lumi, and figure preview updates.
2026-11-20 02:24:34 -03:00

314 lines
13 KiB
Ziran

// The habit shop is Habits' own page: the bundled package draws it from the
// frame the host gives it and reads each reward through the host, within
// the calls the host allows Habits. The host never composes the page.
#import "session"
#import "geometry"
#import "drawing_props"
#import "kryon/tree"
#import "tree_draw"
#import "paint_queue"
#import "bundle_host"
#import "byte_text_linux"
#import "hosted_values"
#import "widget_host"
#import, file "../src/apps/module_host.zi";
#import, file "../src/habits/reward_icons.zi";
#import, file "../src/habits/habit_icons.zi";
#import, file "../src/habits/habit_coins.zi";
#import, file "../src/apps/habits_types.zi";
#import, file "../src/apps/habits_ui_types.zi";
#import, file "../src/apps/app_codec.zi";
// Text, shapes and the clipboard as a host that draws nothing.
#import, file "android_text_input_host.zi";
c_shop_test :: #system_library "c";
ModuleTestText :: (name: string) -> string #foreign c_shop_test;
using HostValueKind;
shop_test_frame: HabitsShopFrame;
shop_test_codec: WidgetHabitsShopFrameCodec;
shop_test_reward_codec: WidgetHabitsShopRewardCodec;
shop_test_activity_codec: WidgetHabitsShopActivityCodec;
shop_test_reads: s32;
shop_test_rewards_seen: u32;
shop_test_activity_seen: u32;
shop_test_written: bool;
ShopTestAssets :: (path: string) -> bool {
return path.count == 0 || AppsAsset(path).found
}
ShopTestCall :: (context: *void, module: *u8, function: *u8,
arguments: *HostValue, count: s32, result: *HostValue) -> s32 {
if TextFromCString(module) != "shop_view" && TextFromCString(module) != "shop_stats_view" {
return WidgetHostCall(context, module, function, arguments, count, result)
}
name := TextFromCString(function)
if name == "ReadHabitsShopHost" && count == 0 {
shop_test_reads += 1
result.* = WidgetEncodeHabitsShopFrame(shop_test_frame, *shop_test_codec)
return 1
}
if name == "WriteHabitsShopHost" && count == 1 && WidgetValidHabitsShopFrame(arguments[0]) {
shop_test_written = true
shop_test_frame.action = WidgetDecodeHabitsShopFrame(arguments[0], *shop_test_codec).action
return 1
}
if name == "ReadHabitRewardHost" && count == 1 {
index := cast(s32)arguments[0].integer
if index < 0 || index >= shop_test_frame.reward_count { return 0 }
shop_test_rewards_seen |= cast(u32)1 << cast(u32)index
result.* = WidgetEncodeHabitsShopReward(HabitsShopReward.{.id = "reward-1",
.name = "Reward", .price = 5,
.statistics = HabitsShopStatistics.{.purchase_count = 2,
.spent_coins = 10, .month_count = 2, .last_date = 20260315},
.icon = ifx index == 0 then 48 else REWARD_ICON_COFFEE}, *shop_test_reward_codec)
return 1
}
if name == "ReadHabitActivityHost" && count == 1 {
index := cast(s32)arguments[0].integer
if index < 0 || index >= shop_test_frame.activity_count { return 0 }
shop_test_activity_seen |= cast(u32)1 << cast(u32)index
line := HabitsShopActivity.{.name = ifx index == 0 then "Meditate" else "Joint",
.icon = 20, .halves = ifx index == 0 then 2 else -10, .day = "Today"}
result.* = WidgetEncodeHabitsShopActivity(line, *shop_test_activity_codec)
return 1
}
if name == "HabitsLocaleHost" && count == 1 {
text := "Shop"
key := TextFromBytes(BytesFromPointer(arguments[0].data, cast(u64)arguments[0].length))
if key == "habit_shop_stats_title" {
text = "Purchase statistics"
}
if key == "habit_shop_stats_empty" {
text = "Buy a reward to start your purchase history."
}
if key == "habit_shop_stats_purchases" {
text = "Purchases"
}
if key == "habit_shop_stats_spent" {
text = "Coins spent"
}
if key == "habit_shop_stats_week" {
text = "Last 7 days"
}
if key == "habit_shop_stats_month" {
text = "Last 30 days"
}
if key == "habit_shop_stats_last" {
text = "Last bought: %s"
}
result.* = HostValue.{.kind = cast(s32)HostString, .type_name = HostTextData("string"),
.data = HostTextData(text), .length = cast(usize)text.count}
return 1
}
return 0
}
// The theme's highlight the host sends: coins, and coins spent, take it.
ShopTestHighlight :: () -> Color {
return Color.{110, 90, 200, 255}
}
SameRgb :: (a: Color, b: Color) -> bool {
return a.r == b.r && a.g == b.g && a.b == b.b
}
// Draws the shop with two rewards and the grid of symbols open, then the
// dialog Buy opens; returns 0 when the package drew both through the host.
CheckHabitShop :: () -> s32 {
// Use the candidate Habits package rather than an older copy in the root fixture.
if !AppsOpenBytes(ModuleTestText("lists"), ModuleTestText("habits"),
ModuleTestText("practice")) { return 8 }
session := SessionOpen()
bounds := Rectangle.{0.0, 0.0, 400.0, 1800.0}
BeginFrame(session, cast(u64)1, bounds)
shop_test_frame = HabitsShopFrame.{.session = session, .scale = 1.0,
.bounds = bounds, .view_width = 400, .font = 16, .small_font = 13,
.halves = 20, .reward_count = 2, .activity_count = 2, .price = 5, .price_text = "5",
.statistics = HabitsShopStatistics.{.purchase_count = 12, .spent_coins = 150,
.week_count = 4, .month_count = 9, .last_date = 20260315},
.icon = 38, .icon_open = true, .highlight_color = ShopTestHighlight()}
WidgetHostBegin(session, ShopTestAssets)
AppsUiBegin(ShopTestCall, null)
drawn := HabitsRun(HabitsMessage.{.action = HABITS_SHOP_VIEW})
AppsUiEnd()
WidgetHostEnd()
painted := PendingPaintCount(session)
generated: u32 = 0
legacy: s32 = 0
statistics_colors: u32 = 0
spending_seen := false
date_seen := false
coin_highlighted := false
coin_gold := false
for index: cast(s32)0..painted - 1 {
command := PendingPaintAt(session, index)
// Coins are icons rasterized into small rectangles in the theme's
// highlight, never the gold they once had in every theme.
if command.kind == 2 && SameRgb(command.color, ShopTestHighlight()) { coin_highlighted = true }
if SameRgb(command.color, Color.{226, 168, 48, 255}) { coin_gold = true }
if command.value == "150" {
spending_seen = true
}
if command.value == "Last bought: 2026-03-15" {
date_seen = true
}
if command.color.r == 63 && command.color.g == 137 && command.color.b == 216 {
statistics_colors |= cast(u32)1
}
if SameRgb(command.color, ShopTestHighlight()) {
statistics_colors |= cast(u32)2
}
if command.color.r == 48 && command.color.g == 159 && command.color.b == 128 {
statistics_colors |= cast(u32)4
}
if command.color.r == 155 && command.color.g == 104 && command.color.b == 207 {
statistics_colors |= cast(u32)8
}
// SVG icon rasterization queues small filled rectangles, not shape primitives.
if command.kind == 2 && command.bounds.width <= 16.0 && command.bounds.height <= 16.0 { legacy += 1 }
icons: [5]s32 = .[76, REWARD_ICON_COFFEE, REWARD_ICON_BUBBLE_TEA,
REWARD_ICON_CHOCOLATE, REWARD_ICON_CANDLE]
for choice: cast(s32)0..4 {
if command.kind == 5 && command.asset_path == RewardIconAsset(icons[choice]) {
generated |= cast(u32)1 << cast(u32)choice
}
}
}
unused EndFrame(session)
unused SessionClose(session)
if generated != cast(u32)31 || legacy == 0 {
print("Reward paint coverage: % bitmap mask, % legacy shapes\n", generated, legacy)
return 7
}
if !drawn.succeeded { return 1 }
if shop_test_reads != 1 || !shop_test_written { return 2 }
// Layout measures wrapped rows before drawing them; every row must be
// available in both passes, independently of the number of host reads.
if shop_test_rewards_seen != cast(u32)3 || shop_test_activity_seen != cast(u32)3 { return 3 }
if painted == 0 { return 4 }
if shop_test_frame.action != HABITS_SHOP_NONE { return 5 }
if spending_seen || date_seen {
print("Purchase statistics leaked into rewards\n")
return 10
}
if !coin_highlighted || coin_gold {
print("Coins: theme highlight %, fixed gold %\n", coin_highlighted, coin_gold)
return 14
}
if RewardIconAsset(HABIT_ICON_SODA).count == 0 ||
RewardIconAsset(REWARD_ICON_ENERGY_DRINK).count != 0 ||
RewardIconShape(REWARD_ICON_ENERGY_DRINK).count == 0 ||
RewardIconShape(REWARD_ICON_ENERGY_DRINK) == RewardIconShape(HABIT_ICON_SODA) ||
HabitRewardIconClamp(REWARD_ICON_ENERGY_DRINK) != REWARD_ICON_ENERGY_DRINK {
return 12
}
statistics_session := SessionOpen()
BeginFrame(statistics_session, cast(u64)4, bounds)
shop_test_frame.session = statistics_session
shop_test_frame.icon_open = false
WidgetHostBegin(statistics_session, ShopTestAssets)
AppsUiBegin(ShopTestCall, null)
stats_drawn := HabitsRun(HabitsMessage.{.action = HABITS_SHOP_STATS_VIEW})
AppsUiEnd()
WidgetHostEnd()
statistics_colors = 0
spending_seen = false
date_seen = false
for index: cast(s32)0..PendingPaintCount(statistics_session) - 1 {
command := PendingPaintAt(statistics_session, index)
if command.value == "150" {
spending_seen = true
}
if command.value == "Last bought: 2026-03-15" {
date_seen = true
}
if command.color.r == 63 && command.color.g == 137 && command.color.b == 216 {
statistics_colors |= cast(u32)1
}
if SameRgb(command.color, ShopTestHighlight()) {
statistics_colors |= cast(u32)2
}
if command.color.r == 48 && command.color.g == 159 && command.color.b == 128 {
statistics_colors |= cast(u32)4
}
if command.color.r == 155 && command.color.g == 104 && command.color.b == 207 {
statistics_colors |= cast(u32)8
}
}
unused EndFrame(statistics_session)
unused SessionClose(statistics_session)
if !stats_drawn.succeeded { return 13 }
if statistics_colors != cast(u32)15 || !spending_seen || !date_seen {
print("Purchase statistics: color mask %, spending %, date %\n", statistics_colors, spending_seen, date_seen)
return 10
}
empty := SessionOpen()
BeginFrame(empty, cast(u64)3, bounds)
shop_test_frame.session = empty
shop_test_frame.statistics = HabitsShopStatistics.{}
shop_test_frame.reward_count = 0
shop_test_frame.activity_count = 0
shop_test_frame.icon_open = false
WidgetHostBegin(empty, ShopTestAssets)
AppsUiBegin(ShopTestCall, null)
empty_drawn := HabitsRun(HabitsMessage.{.action = HABITS_SHOP_STATS_VIEW})
AppsUiEnd()
WidgetHostEnd()
empty_seen := false
for index: cast(s32)0..PendingPaintCount(empty) - 1 {
if PendingPaintAt(empty, index).value == "Buy a reward to start your purchase history." {
empty_seen = true
}
}
unused EndFrame(empty)
unused SessionClose(empty)
if !empty_drawn.succeeded || !empty_seen { return 11 }
// Buy's dialog draws the reward it asks about.
dialog := SessionOpen()
BeginFrame(dialog, cast(u64)2, bounds)
shop_test_frame.session = dialog
shop_test_frame.buying = HabitsShopReward.{.id = "reward-1", .name = "Joint",
.price = 5, .icon = REWARD_ICON_COFFEE}
shop_test_written = false
WidgetHostBegin(dialog, ShopTestAssets)
AppsUiBegin(ShopTestCall, null)
asked := HabitsRun(HabitsMessage.{.action = HABITS_SHOP_BUY_DIALOG})
AppsUiEnd()
WidgetHostEnd()
dialog_painted := PendingPaintCount(dialog)
unused EndFrame(dialog)
unused SessionClose(dialog)
if !asked.succeeded || !shop_test_written || dialog_painted == 0 { return 6 }
if !AppsOpenPackage(ModuleTestText("inbe")) { return 9 }
print("Habits reward picker: shop separation, drink icon mapping and purchase history passed\n")
return 0
}
// Generated rewards expose their packaged WebP size to the headless painter.
#program_export
ImageWidth :: (path: string) -> s32 {
if RewardTestImage(path) { return 128 }
return 0
}
#program_export
ImageHeight :: (path: string) -> s32 {
if RewardTestImage(path) { return 128 }
return 0
}
RewardTestImage :: (path: string) -> bool {
icons: [5]s32 = .[76, REWARD_ICON_COFFEE, REWARD_ICON_BUBBLE_TEA,
REWARD_ICON_CHOCOLATE, REWARD_ICON_CANDLE]
for icon: icons {
if path == RewardIconAsset(icon) {
asset := AppsAsset(path)
return asset.found && asset.data.count >= 12 && asset.data[0:4] == "RIFF" && asset.data[8:12] == "WEBP"
}
}
return false
}