Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
milaGGL committed Jan 6, 2025
1 parent 7056ba7 commit 22b89e8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
12 changes: 11 additions & 1 deletion dev/src/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,16 @@ function compareVectors(left: ApiMapValue, right: ApiMapValue): number {
return compareArrays(leftArray, rightArray);
}

function stringToUTF8Bytes(str: string): Uint8Array {
return new TextEncoder().encode(str);
}

function compareUTF8bytes(left: string, right: string): number {
const leftBytes = stringToUTF8Bytes(left);
const rightBytes = stringToUTF8Bytes(right);
return Buffer.compare(Buffer.from(leftBytes), Buffer.from(rightBytes));
}

/*!
* @private
* @internal
Expand All @@ -269,7 +279,7 @@ export function compare(left: api.IValue, right: api.IValue): number {
case TypeOrder.BOOLEAN:
return primitiveComparator(left.booleanValue!, right.booleanValue!);
case TypeOrder.STRING:
return primitiveComparator(left.stringValue!, right.stringValue!);
return compareUTF8bytes(left.stringValue!, right.stringValue!);
case TypeOrder.NUMBER:
return compareNumberProtos(left, right);
case TypeOrder.TIMESTAMP:
Expand Down
30 changes: 30 additions & 0 deletions dev/system-test/firestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4084,6 +4084,36 @@ describe('Query class', () => {
docChanges: expectedChanges,
});
});

it('snapshot listener sorts query by unicode strings same way as server', async () => {
const collection = await testCollectionWithDocs({
a: {value: 'Łukasiewicz'},
b: {value: 'Sierpiński'},
c: {value: '岩澤'},
d: {value: '🄟'},
e: {value: 'P'},
f: {value: '︒'},
g: {value: '🐵'},
});

const query = collection.orderBy("value");

Check failure on line 4099 in dev/system-test/firestore.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `"value"` with `'value'`

Check warning on line 4099 in dev/system-test/firestore.ts

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote
const expectedDocs = ['b', 'a', 'c', 'f', 'e', 'd', 'g'];

const getSnapshot = await query.get();
expect(getSnapshot.docs.map(d => d.id)).to.deep.equal(expectedDocs);

const unsubscribe = query.onSnapshot(snapshot =>
currentDeferred.resolve(snapshot)
);

const watchSnapshot = await waitForSnapshot();
// Compare the snapshot (including sort order) of a snapshot
snapshotsEqual(watchSnapshot, {
docs: getSnapshot.docs,
docChanges: getSnapshot.docChanges(),
});
unsubscribe();
});
});

(process.env.FIRESTORE_EMULATOR_HOST === undefined
Expand Down

0 comments on commit 22b89e8

Please sign in to comment.