#!/bin/sh
# Pristren's Git diff review exercise. Creates a disposable Git repo with no remote.
set -eu

command -v git >/dev/null || { echo "Install Git and run this file again." >&2; exit 1; }
command -v node >/dev/null || { echo "Install Node.js 22 or later and run this file again." >&2; exit 1; }

example_dir=$(mktemp -d "${TMPDIR:-/tmp}/pristren-diff-review.XXXXXX")
cd "$example_dir"
unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_COMMON_DIR
git init -q --template=""
git config core.hooksPath /dev/null
cat > discount.mjs <<'JS'
export function discount(cents) {
  return cents >= 5000 ? Math.round(cents * 0.1) : 0;
}
JS
cat > discount.test.mjs <<'JS'
import assert from 'node:assert/strict';
import test from 'node:test';
import { discount } from './discount.mjs';

test('no discount at 4,999 cents', () => {
  assert.equal(discount(4999), 0);
});

test('10% discount at exactly 5,000 cents', () => {
  assert.equal(discount(5000), 500);
});
JS

node --test discount.test.mjs
git add discount.mjs discount.test.mjs
git -c user.name="Pristren Example" -c user.email="example@pristren.com" -c commit.gpgsign=false commit -qm "Add discount with boundary tests"

cat > discount.mjs <<'JS'
export function discount(cents) {
  return cents > 5000 ? Math.round(cents * 0.1) : 0;
}
JS

printf '\nExample repository: %s\n' "$example_dir"
printf 'To inspect the changed code and reproduce the failure, run:\n'
# Paths created by mktemp may contain spaces, so display the change-directory
# command as separate instructions and keep the commands themselves portable.
printf '  Change into the example repository above.\n'
printf '  git diff HEAD -- discount.mjs\n'
printf '  node --test discount.test.mjs\n'
printf 'Expected: the 4,999-cent case passes and the 5,000-cent case fails (0 instead of 500).\n'
