The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { SourceControlProviderKind } from "@t3tools/contracts"; |
| 2 | import { assert, it } from "@effect/vitest"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as TestClock from "effect/testing/TestClock"; |
| 5 | |
| 6 | import * as SourceControlRateLimit from "./SourceControlRateLimit.ts"; |
| 7 | |
| 8 | const github = { provider: SourceControlProviderKind.make("github"), host: "github.com" }; |
| 9 | |
| 10 | it.effect("isolates cooldowns for verified credentials on the same host", () => |
| 11 | Effect.gen(function* () { |
| 12 | const limits = yield* SourceControlRateLimit.SourceControlRateLimit; |
| 13 | yield* limits |
| 14 | .recordRateLimit({ ...github, lease: 0 }) |
| 15 | .pipe(Effect.provideService(SourceControlRateLimit.CredentialScope, "first")); |
| 16 | yield* limits |
| 17 | .check(github) |
| 18 | .pipe(Effect.provideService(SourceControlRateLimit.CredentialScope, "second")); |
| 19 | const error = yield* limits |
| 20 | .check(github) |
| 21 | .pipe(Effect.provideService(SourceControlRateLimit.CredentialScope, "first"), Effect.flip); |
| 22 | assert.strictEqual(error._tag, "SourceControlRateLimitPausedError"); |
| 23 | }).pipe(Effect.provide(SourceControlRateLimit.layer)), |
| 24 | ); |
| 25 | |
| 26 | it("parses Retry-After seconds and HTTP dates", () => { |
| 27 | assert.equal(SourceControlRateLimit.retryAtFromHeader("120", 1_000), 121_000); |
| 28 | assert.equal( |
| 29 | SourceControlRateLimit.retryAtFromHeader("Thu, 01 Jan 1970 00:02:01 GMT", 1_000), |
| 30 | 121_000, |
| 31 | ); |
| 32 | assert.isUndefined(SourceControlRateLimit.retryAtFromHeader("later", 1_000)); |
| 33 | }); |
| 34 | |
| 35 | it.effect("lets an interactive request through without clearing an active pause", () => |
| 36 | Effect.gen(function* () { |
| 37 | yield* TestClock.setTime(1_000); |
| 38 | const limits = yield* SourceControlRateLimit.SourceControlRateLimit; |
| 39 | const lease = yield* limits.check(github); |
| 40 | yield* limits.recordRateLimit({ ...github, lease, retryAt: 121_000 }); |
| 41 | |
| 42 | const interactiveLease = yield* limits.check(github, { allowPaused: true }); |
| 43 | yield* limits.recordSuccess({ ...github, lease: interactiveLease }); |
| 44 | |
| 45 | assert.equal(interactiveLease, 1); |
| 46 | assert.equal((yield* Effect.flip(limits.check(github))).retryAt, 121_000); |
| 47 | |
| 48 | yield* limits.recordRateLimit({ ...github, lease: interactiveLease, retryAt: 61_000 }); |
| 49 | assert.equal((yield* Effect.flip(limits.check(github))).retryAt, 121_000); |
| 50 | }).pipe(Effect.provide(SourceControlRateLimit.layer)), |
| 51 | ); |
| 52 | |
| 53 | it.effect("keeps providers and hosts isolated", () => |
| 54 | Effect.gen(function* () { |
| 55 | const limits = yield* SourceControlRateLimit.SourceControlRateLimit; |
| 56 | const lease = yield* limits.check(github); |
| 57 | yield* limits.recordRateLimit({ ...github, lease }); |
| 58 | |
| 59 | assert.equal( |
| 60 | yield* limits.check({ |
| 61 | provider: SourceControlProviderKind.make("gitlab"), |
| 62 | host: "github.com", |
| 63 | }), |
| 64 | 0, |
| 65 | ); |
| 66 | assert.equal( |
| 67 | yield* limits.check({ |
| 68 | provider: SourceControlProviderKind.make("github"), |
| 69 | host: "github.example.com", |
| 70 | }), |
| 71 | 0, |
| 72 | ); |
| 73 | }).pipe(Effect.provide(SourceControlRateLimit.layer)), |
| 74 | ); |
| 75 | |
| 76 | it.effect("does not let an older success clear a concurrent pause", () => |
| 77 | Effect.gen(function* () { |
| 78 | const limits = yield* SourceControlRateLimit.SourceControlRateLimit; |
| 79 | const firstLease = yield* limits.check(github); |
| 80 | const concurrentLease = yield* limits.check(github); |
| 81 | |
| 82 | yield* limits.recordRateLimit({ ...github, lease: firstLease }); |
| 83 | yield* limits.recordSuccess({ ...github, lease: concurrentLease }); |
| 84 | |
| 85 | assert.equal((yield* Effect.flip(limits.check(github))).retryAt, 30_000); |
| 86 | }).pipe(Effect.provide(SourceControlRateLimit.layer)), |
| 87 | ); |
| 88 | |
| 89 | it.effect("keeps a fresh provider reset from an older request", () => |
| 90 | Effect.gen(function* () { |
| 91 | yield* TestClock.setTime(0); |
| 92 | const limits = yield* SourceControlRateLimit.SourceControlRateLimit; |
| 93 | const staleLease = yield* limits.check(github); |
| 94 | yield* limits.recordRateLimit({ ...github, lease: staleLease }); |
| 95 | |
| 96 | yield* TestClock.adjust("31 seconds"); |
| 97 | yield* limits.recordRateLimit({ ...github, lease: staleLease, retryAt: 61_000 }); |
| 98 | |
| 99 | assert.equal((yield* Effect.flip(limits.check(github))).retryAt, 61_000); |
| 100 | }).pipe(Effect.provide(SourceControlRateLimit.layer)), |
| 101 | ); |