# 6. Finish and test the app

Source: https://daybrite.dev/docs/tutorials/complete-app

Add navigation buttons and automated tests to the app from
[lesson 5](https://daybrite.dev/docs/tutorials/navigation). Retain `Cargo.toml`, `Day.toml`, `build.rs`, and `src/main.rs`.
Replace the UI and catalog with the versions below.

## 1. Complete the catalog

Replace `resource/locales/en/app.ftl`. The unused alert messages are removed:

**All platforms: Write `resource/locales/en/app.ftl`**

```ftl
app_title = Session counter
counter_value = Count: { $count }
add_one = Add one
reset_count = Reset
session_hint = Add one after each session. Reset when you start a new day.
nav_counter = Counter
nav_summary = Summary
summary_value = Sessions completed: { $count }
summary_hint = This count lasts until you quit the app.
show_summary = Show summary
back_to_counter = Back to counter
```

## 2. Complete the UI

Replace `src/lib.rs`:

**All platforms: Write `src/lib.rs`**

```rust
use day::prelude::*;

day::resources!();
day::day_start!(options: res::window_options(), root);

pub fn root() -> impl Piece {
    let count = Signal::new(0i64);
    let section = Signal::new("counter".to_string());

    nav(section)
        .style(NavStyle::Sidebar)
        .title(res::str::app_title())
        .item("counter", res::str::nav_counter(), move || {
            counter_page(count)
        })
        .item("summary", res::str::nav_summary(), move || {
            summary_page(count)
        })
        .grow(Dimension::Both)
}

fn counter_page(count: Signal<i64>) -> impl Piece {
    column((
        label(res::str::counter_value(count)).id(id!(counter_label)),
        row((
            button(res::str::add_one())
                .action(move || count.update(|n| *n = n.saturating_add(1)))
                .id(id!(increment_button)),
            button(res::str::reset_count())
                .action(move || count.set(0))
                .id(id!(reset_button)),
        ))
        .spacing(Space::M),
        label(res::str::session_hint()),
        nav_link(res::str::show_summary(), "summary").id(id!(summary_button)),
    ))
    .spacing(Space::M)
    .padding(Space::L)
    .grow(Dimension::Both)
}

fn summary_page(count: Signal<i64>) -> impl Piece {
    column((
        label(res::str::summary_value(count)).id(id!(summary_label)),
        label(res::str::summary_hint()),
        nav_link(res::str::back_to_counter(), "counter").id(id!(counter_button)),
    ))
    .spacing(Space::M)
    .padding(Space::L)
    .grow(Dimension::Both)
}
```

[`nav_link`](https://docs.rs/day-pieces/latest/day_pieces/fn.nav_link.html) creates a button that
selects a route key. These buttons and the sidebar update the same navigation state.
This app switches peer sections; it does not push pages onto a back stack.
All UI text uses generated Fluent accessors.

## 3. Run the finished app

**macOS: Run in `first-app`**

```sh
day launch -t macos-appkit
```

**Windows: Run in `first-app`**

```powershell
day launch -t windows-winui
```

**Linux: Run in `first-app`**

```sh
day launch -t linux-gtk
```

Add two sessions and press **Show summary**; expect **Sessions completed: 2**.
Press **Back to counter**, reset, and reopen the summary; expect zero.
The in-memory count resets when the app exits.

**macOS:** Quit with Command-Q before continuing.

**Windows:** Close the app window before continuing.

**Linux:** Close the app window before continuing.

## 4. Automate the walkthrough

Create the script directory:

**macOS: Run in `first-app`**

```sh
mkdir -p dayscript
```

**Windows: Run in `first-app`**

```powershell
New-Item -ItemType Directory -Force -Path dayscript
```

**Linux: Run in `first-app`**

```sh
mkdir -p dayscript
```

Create `dayscript/session-counter.yaml`:

**All platforms: Write `dayscript/session-counter.yaml`**

```yaml
name: session-counter
on_failure: stop
flow:
  - wait_for: { id: increment_button }
  - assert_text: { id: counter_label, key: counter_value, args: { count: 0 } }
  - tap: { id: increment_button }
  - tap: { id: increment_button }
  - assert_text: { id: counter_label, key: counter_value, args: { count: 2 } }
  - tap: { id: summary_button }
  - assert_route: { route: summary }
  - assert_text: { id: summary_label, key: summary_value, args: { count: 2 } }
  - tap: { id: counter_button }
  - assert_route: { route: counter }
  - assert_text: { id: counter_label, key: counter_value, args: { count: 2 } }
  - tap: { id: reset_button }
  - assert_text: { id: counter_label, key: counter_value, args: { count: 0 } }
  - tap: { id: summary_button }
  - assert_text: { id: summary_label, key: summary_value, args: { count: 0 } }
```

Dayscript is Day's YAML UI automation format. IDs locate controls; message keys and arguments
specify expected localized text. `assert_route` checks navigation; `on_failure: stop` stops at
the first failed assertion.

Run from the app directory:

**macOS: Run in `first-app`**

```sh
day launch -t macos-appkit --locale en --script dayscript/session-counter.yaml
```

**Windows: Run in `first-app`**

```powershell
day launch -t windows-winui --locale en --script dayscript/session-counter.yaml
```

**Linux: Run in `first-app`**

```sh
day launch -t linux-gtk --locale en --script dayscript/session-counter.yaml
```

Expect 15 passing steps and exit status zero. Assertion failures return status 5.
Scripted taps test Day's event dispatch; test physical mouse and keyboard input separately.

## Troubleshooting

| Symptom                   | Check and recovery                                                                                                                                                                                                 |
| ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Script cannot find an ID  | Match YAML IDs to `id!(...)`, save both files, and rerun without `--skip-build`.                                                                                                                                   |
| Initial count is not zero | Launch a new app instance.                                                                                                                                                                                         |
| Navigation fails          | Match [`nav_link`](https://docs.rs/day-pieces/latest/day_pieces/fn.nav_link.html) keys to [`.item`](https://docs.rs/day-pieces/latest/day_pieces/trait.NavBuilder.html#tymethod.item) keys, not translated labels. |
| Text assertion fails      | Check arguments against the count; use matching final catalog and Rust files.                                                                                                                                      |

## Next steps

See [other platforms](https://daybrite.dev/docs/tutorials/other-platforms),
[localization](https://daybrite.dev/docs/how-to/localization), [state](https://daybrite.dev/docs/explanation/state), and
[navigation](https://daybrite.dev/docs/how-to/navigation).
