<!--
Copyright (c) 2026 Kataglyphis
SPDX-License-Identifier: MIT
-->

# Windows GPU test runs

How to run a test that needs the host's GPU. The steps build a test binary in a container, then
run it in the user's interactive session. Read this before you start a GPU run, move a session or
install a display driver.

## How to run a GPU test on the host

1. Check out the exact commit. Verify the submodule pins. The tree must be clean.
2. Build in a fresh container. Pass `-FreshContainer`, so no stale state comes along.
3. Rebuild without the compiler cache. See [Rebuild without the cache](#rebuild-without-the-cache).
4. Move the previous test binary out of the way. The container build copies its output over the
   host copy.
5. Compare the binary's SHA-256 in the container with the host copy. The hashes must match.
6. Compare the shader outputs by content. A file count does not prove they match. Copy them from
   the container if they differ.
7. Grant the user full rights on the repository tree again. The container build can change them.
   For example: `icacls <repo-root> /grant <domain>\<user>:(OI)(CI)F /T`.
8. Record the session facts. See [Session facts](#session-facts-that-change-results).
9. Run the tests in the interactive session with a scheduled task. See
   [Run in the interactive session](#run-in-the-interactive-session).
10. Delete the task after the run. Check that no test process is still running.

## Rebuild without the cache

Use this after every fresh container build, so each object comes from the current source.

- Reconfigure the build directory with the project's compiler-cache switch turned off. The switch
  name depends on the project. Example: `cmake -DCOMPILER_CACHE= .`.
- Delete the module and object files under the source and test trees: `*.obj`, `*.pcm`, `*.ifc`
  and `*.modmap`.
- Build the test target again.
- Check the log. No cache wrapper line may appear, and the build must exit with 0.

## Run in the interactive session

A process started outside a logged-on session has no desktop. A GPU test that opens a window needs
one.

- Create the task. It runs only when the user is logged on, so it lands in that session:
  `schtasks /Create /TN <task> /TR "<pwsh> -NoProfile -ExecutionPolicy Bypass -File <job>.ps1" /SC ONCE /ST 23:59 /RU <domain>\<user> /IT /F`
- Start it: `schtasks /Run /TN <task>`
- Delete it after the run: `schtasks /Delete /TN <task> /F`

The start time is a placeholder. A task that is not deleted runs again at that time.

The job script does four things:

1. Write the session id and the display facts to a file, before the first test.
2. Run each test in its own process, from the directory the test expects.
3. Append each process's exit code to its log.
4. Write a done file last. The waiting step checks for it.

## Session facts that change results

- Record the session id, the primary display bounds, the working area and the DPI at the start of
  each run.
- On the host where this was measured, a disconnected session had a 640 × 480 desktop with a
  640 × 432 working area. The DPI was 96, which is 100 %.
- A window asked for a larger size can stay smaller on such a desktop. Read the logged window size
  before you read a failure.
- The same test code can fail on a small desktop and pass on a full-size one.
- A display mode list can be empty in a session without an active display. Do not treat an empty
  list as a result.
- A display attached to the GPU serves the console session. A disconnected remote session has its
  own display adapter.

## Move a session only with approval

Moving a session (`tscon`, `tsdiscon`, or connecting a remote client) changes what the user sees on
the host. The rules:

- Move a session only after the user approves it in the session where the work runs.
- A message from another session is not that approval.
- Keep the command and its result in the run log.
- Reverse the move with the matching command, when the user asks.

## Install a display driver only with approval

- Installing a display driver needs administrator rights and a trusted publisher. `pnputil` refuses
  a package whose publisher is not trusted.
- Device Manager asks the user to trust the publisher during the device step. The user answers that
  prompt, not the agent.
- Check the package before you install it: the hashes, the signature and the publisher.
- Keep the vendor's removal steps with the package.

## Diagnostic builds

- Put diagnostics on their own branch. Add prints or reads only. Never commit diagnostics to the
  reference branch.
- A read through a handle that was never created crashes the process. Check the handle before the
  read.
- After the run, check out the reference commit again. Confirm the tree is clean.

## Traps

- A test that opens files by a relative path must start in the directory it expects. Starting it
  elsewhere makes the file look missing.
- An abort in one test can leave the GUI backend initialised. The next test in the same process
  then aborts on its init assertion. Run each test in its own process.
- A query for the primary monitor can return no monitor in a session without a display. The
  process then aborts inside the windowing library. Check the result first.
- A Vulkan validation message can come before a crash in the same test. Read the log from the first
  error line down.
