Skip to content
Open
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
271 changes: 271 additions & 0 deletions spec/ParseLiveQueryQuery.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
'use strict';

const Parse = require('parse/node');

describe('ParseLiveQuery query operation', function () {
beforeEach(function (done) {
Parse.CoreManager.getLiveQueryController().setDefaultLiveQueryClient(null);
// Mock ParseWebSocketServer
const mockParseWebSocketServer = jasmine.createSpy('ParseWebSocketServer');
jasmine.mockLibrary(
'../lib/LiveQuery/ParseWebSocketServer',
'ParseWebSocketServer',
mockParseWebSocketServer
);
// Mock Client pushError
const Client = require('../lib/LiveQuery/Client').Client;
Client.pushError = jasmine.createSpy('pushError');
done();
});

afterEach(async function () {
const client = await Parse.CoreManager.getLiveQueryController().getDefaultLiveQueryClient();
if (client) {
await client.close();
}
jasmine.restoreLibrary('../lib/LiveQuery/ParseWebSocketServer', 'ParseWebSocketServer');
});

function addMockClient(parseLiveQueryServer, clientId) {
const Client = require('../lib/LiveQuery/Client').Client;
const client = new Client(clientId, {});
client.pushResult = jasmine.createSpy('pushResult');
parseLiveQueryServer.clients.set(clientId, client);
return client;
}

function addMockSubscription(parseLiveQueryServer, clientId, requestId, parseWebSocket, query = {}) {
const Subscription = require('../lib/LiveQuery/Subscription').Subscription;
const subscription = new Subscription(
query.className || 'TestObject',
query.where || {},
'hash'
);

// Add to server subscriptions
if (!parseLiveQueryServer.subscriptions.has(subscription.className)) {
parseLiveQueryServer.subscriptions.set(subscription.className, new Map());
}
const classSubscriptions = parseLiveQueryServer.subscriptions.get(subscription.className);
classSubscriptions.set('hash', subscription);

// Add to client
const client = parseLiveQueryServer.clients.get(clientId);
const subscriptionInfo = {
subscription: subscription,
keys: query.keys,
};
if (parseWebSocket.sessionToken) {
subscriptionInfo.sessionToken = parseWebSocket.sessionToken;
}
client.subscriptionInfos.set(requestId, subscriptionInfo);

return subscription;
}

it('can handle query command with existing subscription', async () => {
await reconfigureServer({
liveQuery: {
classNames: ['TestObject'],
},
startLiveQueryServer: true,
verbose: false,
silent: true,
});

const { ParseLiveQueryServer } = require('../lib/LiveQuery/ParseLiveQueryServer');
const parseLiveQueryServer = new ParseLiveQueryServer({
appId: 'test',
masterKey: 'test',
serverURL: 'http://localhost:1337/parse'
});
Comment on lines +76 to +81
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

Verify serverURL configuration matches test setup.

The ParseLiveQueryServer is configured with serverURL: 'http://localhost:1337/parse', but the test uses reconfigureServer which typically runs on a different port. If the query operation makes REST calls to Parse Server, this mismatch could cause connection failures.

Check if the serverURL should use the same port as the reconfigured server:


🏁 Script executed:

#!/bin/bash
# Find how reconfigureServer sets the port for Parse Server
rg -nP -A5 'reconfigureServer.*=.*async' spec/helper.js

# Check if _handleQuery makes REST calls using serverURL
ast-grep --pattern '_handleQuery($$$) {
  $$$
}'

Length of output: 279


🏁 Script executed:

#!/bin/bash
# Locate the ParseLiveQueryServer class definition
rg -nP -C3 'class\s+ParseLiveQueryServer' --glob '*.js'

# Inspect the _handleQuery method implementation
rg -nP -C3 '_handleQuery' --glob '*.js'

# Look for usage of serverURL within the file
rg -nP 'serverURL' -C3 --glob '*.js'

Length of output: 112115


🏁 Script executed:

#!/bin/bash
# Locate the ParseLiveQueryServer source
grep -R "class ParseLiveQueryServer" -n lib/LiveQuery/ParseLiveQueryServer.js

# Inspect constructor for serverURL usage
grep -R "constructor" -n lib/LiveQuery/ParseLiveQueryServer.js -A5

# Inspect query handler implementation
grep -R "_handleQuery" -n lib/LiveQuery/ParseLiveQueryServer.js -A5

# Check for directAccess default and serverURL fallback
grep -R "directAccess" -n lib/LiveQuery/ParseLiveQueryServer.js -A3

Length of output: 283


🏁 Script executed:

#!/bin/bash
# List top-level directories to locate code structure
ls -1

# Search for ParseLiveQueryServer definition anywhere in the repo
rg -nP 'class\s+ParseLiveQueryServer' -g '*.js'

# If found, inspect the constructor and _handleQuery methods
rg -nP --context 5 'constructor\s*\(' -g '*ParseLiveQueryServer.js'
rg -nP --context 5 '_handleQuery\s*\(' -g '*ParseLiveQueryServer.js'

Length of output: 650


#!/bin/bash
# Dynamically locate the ParseLiveQueryServer implementation
file=$(grep -R "class ParseLiveQueryServer" -l src | head -1)
echo "Inspecting $file"
# Show constructor to see how serverURL is stored
grep -nE 'constructor' -C3 "$file"
# Show how _handleQuery uses serverURL or directAccess
grep -nE '_handleQuery' -C3 "$file"
# Show any references to serverURL in this class
grep -nE 'serverURL' -C3 "$file"

🏁 Script executed:

#!/bin/bash
# Search for ParseLiveQueryServer implementation in TypeScript sources
rg -nP 'class\s+ParseLiveQueryServer' -g 'src/**/*.ts'

# Print the file path(s)
rg -lP 'class\s+ParseLiveQueryServer' -g 'src/**/*.ts'

# Inspect the found file
FILE=$(rg -lP 'class\s+ParseLiveQueryServer' -g 'src/**/*.ts' | head -1)
echo "Inspecting $FILE"
grep -nE 'constructor' -C3 "$FILE"
grep -nE '_handleQuery' -C3 "$FILE"
grep -nE 'serverURL' -C3 "$FILE"

Length of output: 1789


🏁 Script executed:

#!/bin/bash
sed -n '1000,1100p' src/LiveQuery/ParseLiveQueryServer.ts

Length of output: 3677


🏁 Script executed:

#!/bin/bash
sed -n '1,200p' src/LiveQuery/ParseLiveQueryServer.ts

Length of output: 7315


Replace hard-coded serverURL in query tests
In spec/ParseLiveQueryQuery.spec.js (around line 76), you’re instantiating ParseLiveQueryServer with serverURL: 'http://localhost:1337/parse', but the test helper reconfigures Parse Server on port 8378 with mount /1. This mismatch causes _handleQuery to point at the wrong endpoint. Use the helper’s serverURL (or Parse.serverURL) instead so LiveQueryServer and the test Parse Server align.

🤖 Prompt for AI Agents
In spec/ParseLiveQueryQuery.spec.js around lines 76 to 81, the
ParseLiveQueryServer is instantiated with a hard-coded serverURL
'http://localhost:1337/parse' which mismatches the test helper’s Parse Server
(port 8378, mount '/1'), causing _handleQuery to target the wrong endpoint;
replace the hard-coded URL with the helper-provided server URL (e.g., use the
helper’s serverURL variable or Parse.serverURL) so the ParseLiveQueryServer and
the test Parse Server use the same base URL and mount.


// Create test objects
const TestObject = Parse.Object.extend('TestObject');
const obj1 = new TestObject();
obj1.set('name', 'object1');
await obj1.save();

const obj2 = new TestObject();
obj2.set('name', 'object2');
await obj2.save();

// Add mock client
const clientId = 1;
const client = addMockClient(parseLiveQueryServer, clientId);
client.hasMasterKey = true;

// Add mock subscription
const parseWebSocket = { clientId: 1 };
const requestId = 2;
const query = {
className: 'TestObject',
where: {},
};
addMockSubscription(parseLiveQueryServer, clientId, requestId, parseWebSocket, query);

// Handle query command
const request = {
op: 'query',
requestId: requestId,
};

await parseLiveQueryServer._handleQuery(parseWebSocket, request);

// Verify pushResult was called
expect(client.pushResult).toHaveBeenCalled();
const results = client.pushResult.calls.mostRecent().args[1];
expect(Array.isArray(results)).toBe(true);
expect(results.length).toBe(2);
expect(results.some(r => r.name === 'object1')).toBe(true);
expect(results.some(r => r.name === 'object2')).toBe(true);
});

it('can handle query command without clientId', async () => {
const { ParseLiveQueryServer } = require('../lib/LiveQuery/ParseLiveQueryServer');
const parseLiveQueryServer = new ParseLiveQueryServer({});
const incompleteParseConn = {};
await parseLiveQueryServer._handleQuery(incompleteParseConn, {});

const Client = require('../lib/LiveQuery/Client').Client;
expect(Client.pushError).toHaveBeenCalled();
});

it('can handle query command without subscription', async () => {
const { ParseLiveQueryServer } = require('../lib/LiveQuery/ParseLiveQueryServer');
const parseLiveQueryServer = new ParseLiveQueryServer({});
const clientId = 1;
addMockClient(parseLiveQueryServer, clientId);

const parseWebSocket = { clientId: 1 };
const request = {
op: 'query',
requestId: 999, // Non-existent subscription
};

await parseLiveQueryServer._handleQuery(parseWebSocket, request);

const Client = require('../lib/LiveQuery/Client').Client;
expect(Client.pushError).toHaveBeenCalled();
});

it('respects field filtering (keys) when executing query', async () => {
await reconfigureServer({
liveQuery: {
classNames: ['TestObject'],
},
startLiveQueryServer: true,
verbose: false,
silent: true,
});

const { ParseLiveQueryServer } = require('../lib/LiveQuery/ParseLiveQueryServer');
const parseLiveQueryServer = new ParseLiveQueryServer({
appId: 'test',
masterKey: 'test',
serverURL: 'http://localhost:1337/parse'
});

// Create test object with multiple fields
const TestObject = Parse.Object.extend('TestObject');
const obj = new TestObject();
obj.set('name', 'test');
obj.set('color', 'blue');
obj.set('size', 'large');
await obj.save();

// Add mock client
const clientId = 1;
const client = addMockClient(parseLiveQueryServer, clientId);
client.hasMasterKey = true;

// Add mock subscription with keys
const parseWebSocket = { clientId: 1 };
const requestId = 2;
const query = {
className: 'TestObject',
where: {},
keys: ['name', 'color'], // Only these fields
};
addMockSubscription(parseLiveQueryServer, clientId, requestId, parseWebSocket, query);

// Handle query command
const request = {
op: 'query',
requestId: requestId,
};

await parseLiveQueryServer._handleQuery(parseWebSocket, request);

// Verify results
expect(client.pushResult).toHaveBeenCalled();
const results = client.pushResult.calls.mostRecent().args[1];
expect(results.length).toBe(1);

// Results should include selected fields
expect(results[0].name).toBe('test');
expect(results[0].color).toBe('blue');

// Results should NOT include size
expect(results[0].size).toBeUndefined();
});

it('handles query with where constraints', async () => {
await reconfigureServer({
liveQuery: {
classNames: ['TestObject'],
},
startLiveQueryServer: true,
verbose: false,
silent: true,
});

const { ParseLiveQueryServer } = require('../lib/LiveQuery/ParseLiveQueryServer');
const parseLiveQueryServer = new ParseLiveQueryServer({
appId: 'test',
masterKey: 'test',
serverURL: 'http://localhost:1337/parse'
});

// Create test objects
const TestObject = Parse.Object.extend('TestObject');
const obj1 = new TestObject();
obj1.set('name', 'match');
obj1.set('status', 'active');
await obj1.save();

const obj2 = new TestObject();
obj2.set('name', 'nomatch');
obj2.set('status', 'inactive');
await obj2.save();

// Add mock client
const clientId = 1;
const client = addMockClient(parseLiveQueryServer, clientId);
client.hasMasterKey = true;

// Add mock subscription with where clause
const parseWebSocket = { clientId: 1 };
const requestId = 2;
const query = {
className: 'TestObject',
where: { status: 'active' }, // Only active objects
};
addMockSubscription(parseLiveQueryServer, clientId, requestId, parseWebSocket, query);

// Handle query command
const request = {
op: 'query',
requestId: requestId,
};

await parseLiveQueryServer._handleQuery(parseWebSocket, request);

// Verify results
expect(client.pushResult).toHaveBeenCalled();
const results = client.pushResult.calls.mostRecent().args[1];
expect(results.length).toBe(1);
expect(results[0].name).toBe('match');
expect(results[0].status).toBe('active');
});
});
23 changes: 23 additions & 0 deletions src/LiveQuery/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Client {
pushUpdate: Function;
pushDelete: Function;
pushLeave: Function;
pushResult: Function;

constructor(
id: number,
Expand All @@ -45,6 +46,7 @@ class Client {
this.pushUpdate = this._pushEvent('update');
this.pushDelete = this._pushEvent('delete');
this.pushLeave = this._pushEvent('leave');
this.pushResult = this._pushQueryResult.bind(this);
}

static pushResponse(parseWebSocket: any, message: Message): void {
Expand Down Expand Up @@ -126,6 +128,27 @@ class Client {
}
return limitedParseObject;
}

_pushQueryResult(subscriptionId: number, results: any[]): void {
const response: Message = {
op: 'result',
clientId: this.id,
installationId: this.installationId,
requestId: subscriptionId,
};

if (results && Array.isArray(results)) {
let keys;
if (this.subscriptionInfos.has(subscriptionId)) {
keys = this.subscriptionInfos.get(subscriptionId).keys;
}
response['results'] = results.map(obj => this._toJSONWithFields(obj, keys));
} else {
response['results'] = [];
}

Client.pushResponse(this.parseWebSocket, JSON.stringify(response));
}
}

export { Client };
Loading
Loading