Skip to content

Commit 4a83028

Browse files
Add basic tests
1 parent 2c816c2 commit 4a83028

File tree

6 files changed

+547
-13
lines changed

6 files changed

+547
-13
lines changed

packages/lite-sdk/src/client/sync/SyncClient.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { BucketRequest, StreamingSyncLine } from '@powersync/service-core';
12
import { BaseListener, BaseObserverInterface, Disposable } from '../../utils/BaseObserver.js';
23
import type { BucketStorage } from '../storage/BucketStorage.js';
34
import type { SystemDependencies } from '../system/SystemDependencies.js';
@@ -79,6 +80,20 @@ export interface SyncClient extends BaseObserverInterface<SyncClientListener>, D
7980
disconnect: () => void;
8081
}
8182

83+
export type StreamOptions = {
84+
endpoint: string;
85+
token: string;
86+
clientId: string | undefined;
87+
signal: AbortSignal | undefined;
88+
bucketPositions: BucketRequest[];
89+
systemDependencies: SystemDependencies;
90+
};
91+
92+
/**
93+
* Function type for opening a sync stream. Can be overridden in tests.
94+
*/
95+
export type StreamOpener = (options: StreamOptions) => Promise<ReadableStream<StreamingSyncLine>>;
96+
8297
/**
8398
* Configuration options for creating a SyncClient instance.
8499
*/
@@ -93,4 +108,6 @@ export type SyncClientOptions = {
93108
storage: BucketStorage;
94109
/** System-level dependencies (HTTP client, timers, etc.) required for sync operations. */
95110
systemDependencies: SystemDependencies;
111+
/** Optional function to open a sync stream. Defaults to openHttpStream. Used for testing. */
112+
streamOpener?: StreamOpener;
96113
};

packages/lite-sdk/src/client/sync/SyncClientImpl.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { BucketStorage } from '../storage/BucketStorage.js';
44
import type {
55
Connector,
66
PowerSyncCredentials,
7+
StreamOpener,
78
SyncClient,
89
SyncClientListener,
910
SyncClientOptions,
@@ -34,6 +35,7 @@ export class SyncClientImpl extends BaseObserver<SyncClientListener> implements
3435

3536
protected cachedCredentials: PowerSyncCredentials | null;
3637
protected abortController: AbortController;
38+
protected openStreamFn: StreamOpener;
3739

3840
constructor(protected options: SyncClientOptions) {
3941
super();
@@ -47,6 +49,7 @@ export class SyncClientImpl extends BaseObserver<SyncClientListener> implements
4749
downloading: false,
4850
downloadError: null
4951
};
52+
this.openStreamFn = options.streamOpener ?? openHttpStream;
5053
}
5154

5255
protected get bucketStorage(): BucketStorage {
@@ -129,7 +132,7 @@ export class SyncClientImpl extends BaseObserver<SyncClientListener> implements
129132

130133
const [bucketRequests, initBucketMap] = await this.collectLocalBucketState();
131134

132-
const stream = await openHttpStream({
135+
const stream = await this.openStreamFn({
133136
endpoint: credentials.endpoint,
134137
token: credentials.token,
135138
signal: signal,

packages/lite-sdk/src/client/sync/open-stream.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { StreamingSyncLine, StreamingSyncRequest } from '@powersync/service-core';
2-
import type { SystemDependencies } from '../system/SystemDependencies.js';
2+
import type { StreamOptions } from './SyncClient.js';
33
import { ndjsonStream } from './ndjson.js';
44

55
export class AuthorizationError extends Error {
@@ -24,17 +24,8 @@ export interface BucketRequest {
2424
after: string;
2525
}
2626

27-
export type SyncOptions = {
28-
endpoint: string;
29-
token: string;
30-
clientId: string | undefined;
31-
signal: AbortSignal | undefined;
32-
bucketPositions: BucketRequest[];
33-
systemDependencies: SystemDependencies;
34-
};
35-
3627
// TODO This currently uses NDJSON streaming. We should add binary streaming also
37-
export async function openHttpStream(options: SyncOptions): Promise<ReadableStream<StreamingSyncLine>> {
28+
export async function openHttpStream(options: StreamOptions): Promise<ReadableStream<StreamingSyncLine>> {
3829
const streamRequest: StreamingSyncRequest = {
3930
raw_data: true,
4031
client_id: options.clientId,

packages/lite-sdk/tests/powersync-lite.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { DEFAULT_SYSTEM_DEPENDENCIES } from '../src/client/system/SystemDependen
77

88
describe(`PowerSync Lite`, { timeout: Infinity }, () => {
99
describe(`Connection`, () => {
10-
it(`should connect to a PowerSync server`, async () => {
10+
it.skip(`should connect to a PowerSync server`, async () => {
1111
const connector = {
1212
fetchCredentials: async () => {
1313
const tokenResponse = await fetch(`http://localhost:6060/api/auth/token`, {

0 commit comments

Comments
 (0)