// Proves the pen-feel pressure default and its one-time migration.
//
// Before this build pressureGamma was a dead slider (never applied to strokes),
// so a persisted 1.0 is the legacy inert default. load() upgrades that ONCE to
// the natural curve so the pen feels right out of the box — but the migration
// must not fight a user who later deliberately picks 1.0.

import 'dart:convert';

import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'package:badnote/editor/input/pen_config.dart';
import 'package:badnote/editor/input/pressure_curve.dart';

void main() {
  TestWidgetsFlutterBinding.ensureInitialized();

  test('fresh install defaults to the natural pressure curve', () async {
    SharedPreferences.setMockInitialValues({});
    final c = await PenConfigController.load();
    expect(c.value.pressureGamma, kNaturalPressureGamma);
    expect(kNaturalPressureGamma, lessThan(1.0)); // sanity: it IS a soft curve
  });

  test('legacy stored gamma 1.0 migrates to the natural curve once', () async {
    SharedPreferences.setMockInitialValues({
      PenConfigController.prefsKey:
          jsonEncode(const PenConfig(pressureGamma: 1.0).toJson()),
    });
    final c = await PenConfigController.load();
    expect(c.value.pressureGamma, kNaturalPressureGamma,
        reason: 'the dead-default 1.0 should be upgraded');
  });

  test('after migrating, a deliberate gamma 1.0 sticks (no re-migration)',
      () async {
    // First load migrates and sets the marker.
    SharedPreferences.setMockInitialValues({
      PenConfigController.prefsKey:
          jsonEncode(const PenConfig(pressureGamma: 1.0).toJson()),
    });
    final first = await PenConfigController.load();
    await first.setPressureGamma(1.0); // user deliberately chooses linear

    // Reload: the marker is set, so 1.0 must be respected, not re-migrated.
    final second = await PenConfigController.load();
    expect(second.value.pressureGamma, 1.0,
        reason: 'migration is one-time; user choice must persist');
  });

  test('a non-default stored gamma is never touched', () async {
    SharedPreferences.setMockInitialValues({
      PenConfigController.prefsKey:
          jsonEncode(const PenConfig(pressureGamma: 0.5).toJson()),
    });
    final c = await PenConfigController.load();
    expect(c.value.pressureGamma, 0.5);
  });
}
