Skip to content

Commit

Permalink
Fix tag validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
icehaunter committed Dec 10, 2024
1 parent 89e0f99 commit 1a28bea
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions packages/typescript-client/test/integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { parse } from 'cache-control-parser'
import { setTimeout as sleep } from 'node:timers/promises'
import { v4 as uuidv4 } from 'uuid'
import { describe, expect, inject, vi } from 'vitest'
import { assert, describe, expect, inject, vi } from 'vitest'
import { FetchError, Shape, ShapeStream } from '../src'
import { Message, Offset } from '../src/types'
import { isChangeMessage, isUpToDateMessage } from '../src/helpers'
Expand Down Expand Up @@ -307,10 +307,9 @@ describe(`HTTP Sync`, () => {
]
)

await vi.waitFor(async () => {
await sleep(100)
return client.isUpToDate
})
await vi.waitFor(() =>
assert(client.isUpToDate && !client.lastOffset.startsWith('0_'))

Check failure on line 311 in packages/typescript-client/test/integration.test.ts

View workflow job for this annotation

GitHub Actions / Check packages/typescript-client

Strings must use backtick
)
const updatedData = await client.rows

expect(updatedData).toMatchObject([
Expand Down Expand Up @@ -528,10 +527,7 @@ describe(`HTTP Sync`, () => {
)

// And wait until it's definitely seen
await vi.waitFor(async () => {
await sleep(50)
return issueStream.isUpToDate
})
await sleep(100)

let catchupOpsCount = 0
const newAborter = new AbortController()
Expand All @@ -557,9 +553,7 @@ describe(`HTTP Sync`, () => {
expect(catchupOpsCount).toBe(9)
})

// This test doesn't work anymore because server sends initial snapshot as a complete
// stable chunk, so e-tag is the same between requests.
it.skip(`should return correct caching headers`, async ({
it(`should return correct caching headers`, async ({
issuesTableUrl,
insertIssues,
}) => {
Expand Down Expand Up @@ -596,17 +590,24 @@ describe(`HTTP Sync`, () => {
)
const etag2Header = res2.headers.get(`etag`)
expect(etag2Header, `Response should have etag header`).not.toEqual(null)
expect(etagHeader).not.toEqual(etag2Header)
expect(etagHeader).toEqual(etag2Header)

// Second chunk is not yet full, so no e-tag yet
const res3 = await fetch(
`${BASE_URL}/v1/shape?table=${issuesTableUrl}&offset=${res2.headers.get(`electric-offset`)}&handle=${res2.headers.get(`electric-handle`)}`
)
const etag3Header = res3.headers.get(`etag`)
expect(etag3Header, `Response should have etag header`).not.toEqual(null)
expect(etagHeader).not.toEqual(etag3Header)
})

// This test doesn't work anymore because server sends initial snapshot as a complete
// stable chunk, so e-tag is the same between requests.
it.skip(`should revalidate etags`, async ({
issuesTableUrl,
insertIssues,
}) => {
it(`should revalidate etags`, async ({ issuesTableUrl, insertIssues }) => {
// Start the shape
await fetch(`${BASE_URL}/v1/shape?table=${issuesTableUrl}&offset=-1`, {})
const baseRes = await fetch(
`${BASE_URL}/v1/shape?table=${issuesTableUrl}&offset=-1`,
{}
)
const handle = baseRes.headers.get(`electric-handle`)
// Fill it up in separate transactions
for (const i of [1, 2, 3, 4, 5, 6, 7, 8, 9]) {
await insertIssues({ title: `foo${i}` })
Expand All @@ -615,11 +616,11 @@ describe(`HTTP Sync`, () => {
await sleep(100)

const res = await fetch(
`${BASE_URL}/v1/shape?table=${issuesTableUrl}&offset=-1`,
`${BASE_URL}/v1/shape?table=${issuesTableUrl}&offset=0_0&handle=${handle}`,
{}
)
const messages = (await res.json()) as Message[]
expect(messages.length).toEqual(9) // 9 inserts
expect(messages.length).toEqual(10) // 9 inserts + up-to-date
const midMessage = messages.slice(-6)[0]
expect(midMessage).to.have.own.property(`offset`)
const midOffset = (midMessage as { offset: string }).offset
Expand All @@ -628,7 +629,7 @@ describe(`HTTP Sync`, () => {
expect(etag, `Response should have etag header`).not.toBe(null)

const etagValidation = await fetch(
`${BASE_URL}/v1/shape?table=${issuesTableUrl}&offset=-1`,
`${BASE_URL}/v1/shape?table=${issuesTableUrl}&offset=0_0&handle=${handle}`,
{
headers: { 'If-None-Match': etag! },
}
Expand Down

0 comments on commit 1a28bea

Please sign in to comment.