utils/github_device_auth/test/device_auth_test.dart
Krzysztof kuhy Rudnicki 200432a2b4 dart: split ten oversized test files by group
firebase_auth_rest_test.dart  525 -> 153 + _google 158 + _tokens 206 + _uid 141
  sync_revisions_test.dart      451 -> 223 + _peers 228 + _backends 158
                                       + _order 165 + _persistence 215
  sync_settings_screen_test     457 -> 207 + _backup 143 + _google 145
  mirror_store_test.dart        452 -> 209 + _reads 172 + _fallback 196 + _revisions 244
  firebase_client_test.dart     374 -> 198 + _writes 227
  firebase_config_test.dart     360 -> 138 + _client 148 + _client_google 164
  device_auth_test.dart         307 -> 128 + _poll 201
  github_client_test.dart       291 -> 143 + _writes 201
  sync_test.dart                269 -> 160 + _skips 175

Two traps this hit, both invisible to a line count:

  - _FakePersistence is declared *after* main(), so a naive cut at the last
    brace dropped it; sync_revisions_persistence_test.dart failed to load
    until it was carried across. Audited every other split file for trailing
    top-level declarations — this was the only one.
  - Copying the preamble into each sibling left helpers the sibling never
    uses. `dart analyze` flagged 12; all removed rather than left as dead
    code, and the analyzer is now clean.

crdt_sync_dart 280 passed, sync_settings_ui 29 passed, github_device_auth
analyze clean — all matching a clean HEAD worktree.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CFSGeWxHPY5rHUGrPqhcmG
2026-08-22 14:54:35 +02:00

129 lines
3.8 KiB
Dart

import 'dart:convert';
import 'package:flutter_test/flutter_test.dart';
import 'package:github_device_auth/github_device_auth.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
/// Never actually waits: the poll loop's pacing is injected so a test that
/// exercises `slow_down` does not take 15 real seconds.
Future<void> noDelay(Duration _) async {}
GitHubDeviceAuth authWith(
MockClient client, {
String scope = 'repo',
}) => GitHubDeviceAuth(
clientId: 'cid',
scope: scope,
httpClient: client,
delay: noDelay,
);
void main() {
group('DeviceCodeResponse.fromJson', () {
test('reads every field', () {
final r = DeviceCodeResponse.fromJson(const {
'device_code': 'dc',
'user_code': 'UC-1',
'verification_uri': 'https://github.com/login/device',
'interval': 7,
'expires_in': 100,
});
expect(r.deviceCode, 'dc');
expect(r.userCode, 'UC-1');
expect(r.verificationUri, 'https://github.com/login/device');
expect(r.interval, 7);
expect(r.expiresIn, 100);
});
test('defaults interval and expiry when GitHub omits them', () {
final r = DeviceCodeResponse.fromJson(const {
'device_code': 'dc',
'user_code': 'UC',
'verification_uri': 'u',
});
expect(r.interval, 5);
expect(r.expiresIn, 900);
});
});
test('DeviceAuthException stringifies code and message', () {
expect(
DeviceAuthException('access_denied', 'nope').toString(),
'DeviceAuthException(access_denied): nope',
);
});
group('requestDeviceCode', () {
test('posts the client id and scope, and parses the response', () async {
late http.Request seen;
final auth = authWith(
MockClient((req) async {
seen = req;
return http.Response(
jsonEncode({
'device_code': 'dc',
'user_code': 'UC',
'verification_uri': 'u',
}),
200,
);
}),
scope: 'repo,gist',
);
final device = await auth.requestDeviceCode();
expect(seen.url.toString(), 'https://github.com/login/device/code');
expect(seen.bodyFields['client_id'], 'cid');
expect(seen.bodyFields['scope'], 'repo,gist');
expect(device.userCode, 'UC');
});
test('defaults to GitHub s own endpoints', () {
final auth = GitHubDeviceAuth(clientId: 'cid')..close();
expect(auth.deviceCodeUrl, githubDeviceCodeUrl);
expect(auth.tokenUrl, githubTokenUrl);
});
test('honours an overridden endpoint', () async {
// diet-guard's desktop web build routes both endpoints through a local
// proxy, because GitHub's device-flow endpoints send no CORS headers
// and a page cannot call them at all.
late Uri seen;
final auth = GitHubDeviceAuth(
clientId: 'cid',
deviceCodeUrl: 'http://127.0.0.1:9/gh/auth/device/start',
httpClient: MockClient((req) async {
seen = req.url;
return http.Response(
jsonEncode({
'device_code': 'dc',
'user_code': 'UC',
'verification_uri': 'u',
}),
200,
);
}),
delay: noDelay,
);
await auth.requestDeviceCode();
expect(seen.toString(), 'http://127.0.0.1:9/gh/auth/device/start');
});
test('throws with the status code on a non-200', () async {
final auth = authWith(MockClient((_) async => http.Response('bad', 503)));
await expectLater(
auth.requestDeviceCode(),
throwsA(
isA<DeviceAuthException>()
.having((e) => e.code, 'code', 'http_503')
.having((e) => e.message, 'message', 'bad'),
),
);
});
});
}