Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changeset/user-subscription-spec-timezone.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
14 changes: 9 additions & 5 deletions test/unit/repositories/user-subscription-repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ describe('UserSubscriptionRepository', () => {
const real = knex({ client: 'pg' })
let sql: string[]

// How the driver prints a date in a statement. It prints it in the local time zone of the machine, so what a test
// expects is the same date printed the same way, not a literal that holds in one time zone only.
const printed = (date: Date): string => real.raw('?', [date]).toString()

// A client that builds real knex queries, records the SQL they would run and answers with `result`.
const clientReturning = (result: unknown): DatabaseClient =>
((table: string) => {
Expand Down Expand Up @@ -142,7 +146,7 @@ describe('UserSubscriptionRepository', () => {
expect(sql[0]).to.include(`"pubkey" = X'${pubkeyHex}'`)
expect(sql[0]).to.include(`"status" = 'active'`)
expect(sql[0]).to.include(`"plan_id" = 'basic'`)
expect(sql[0]).to.include(`current_period_end IS NOT DISTINCT FROM '2026-01-31`)
expect(sql[0]).to.include(`current_period_end IS NOT DISTINCT FROM ${printed(end)}`)
expect(sql[0]).to.include('grace_ends_at IS NOT DISTINCT FROM NULL')
})

Expand All @@ -156,7 +160,7 @@ describe('UserSubscriptionRepository', () => {
)

expect(sql[0]).to.include(`"status" = 'past_due'`)
expect(sql[0]).to.include(`grace_ends_at IS NOT DISTINCT FROM '2026-02-03`)
expect(sql[0]).to.include(`grace_ends_at IS NOT DISTINCT FROM ${printed(graceEndsAt)}`)
expect(sql[0]).to.include('"grace_ends_at" = NULL')
})

Expand Down Expand Up @@ -187,7 +191,7 @@ describe('UserSubscriptionRepository', () => {
expect(sql[0]).to.include(`"status" = 'active'`)
expect(sql[0]).to.include('"cancel_at_period_end" = false')
expect(sql[0]).to.include('"current_period_end" is not null')
expect(sql[0]).to.include(`"current_period_end" <= '2026-01-31`)
expect(sql[0]).to.include(`"current_period_end" <= ${printed(end)}`)
expect(sql[0]).to.include('order by "current_period_end" asc')
expect(sql[0]).to.include('limit 10')
})
Expand Down Expand Up @@ -217,7 +221,7 @@ describe('UserSubscriptionRepository', () => {

expect(sql[0]).to.include(`"status" in ('active', 'renewal_pending')`)
expect(sql[0]).to.include('"current_period_end" is not null')
expect(sql[0]).to.include(`"current_period_end" <= '2026-01-31`)
expect(sql[0]).to.include(`"current_period_end" <= ${printed(end)}`)
expect(sql[0]).to.include('limit 10')
})

Expand All @@ -239,7 +243,7 @@ describe('UserSubscriptionRepository', () => {

expect(sql[0]).to.include(`"status" = 'past_due'`)
expect(sql[0]).to.include('"grace_ends_at" is not null')
expect(sql[0]).to.include(`"grace_ends_at" <= '2026-01-31`)
expect(sql[0]).to.include(`"grace_ends_at" <= ${printed(end)}`)
expect(sql[0]).to.include('order by "grace_ends_at" asc')
})

Expand Down
Loading