sourcebot/packages/backend/src/utils.test.ts

72 lines
2.5 KiB
TypeScript
Raw Normal View History

import { expect, test } from 'vitest';
fix(search-contexts): Fix issue where a repository would not appear in a search context if it was created after the search context was created (#354) ## Problem If a repository is added **after** a search context (e.g., a new repository is synced from the code host), then it will never be added to the context even if it should be included. The workaround is to restart the instance. ## Solution This PR adds a call to re-sync all search contexts whenever a connection is successfully synced. This PR adds the `@sourcebot/shared` package that contains `syncSearchContexts.ts` (previously in web) and it's dependencies (namely the entitlements system). ## Why another package? Because the `syncSearchContexts` call is now called from: 1. `initialize.ts` in **web** - handles syncing search contexts on startup and whenever the config is modified in watch mode. This is the same as before. 2. `connectionManager.ts` in **backend** - syncs the search contexts whenever a connection is successfully synced. ## Follow-up devex work Two things: 1. We have several very thin shared packages (i.e., `crypto`, `error`, and `logger`) that we can probably fold into this "general" shared package. `schemas` and `db` _feels_ like they should remain separate (mostly because they are "code-gen" packages). 2. When running `yarn dev`, any changes made to the shared package will only get picked if you `ctrl+c` and restart the instance. Would be nice if we have watch mode work across package dependencies in the monorepo.
2025-06-17 21:04:25 +00:00
import { arraysEqualShallow } from './utils';
import { isRemotePath } from '@sourcebot/shared';
test('should return true for identical arrays', () => {
expect(arraysEqualShallow([1, 2, 3], [1, 2, 3])).toBe(true);
});
test('should return true for empty arrays', () => {
expect(arraysEqualShallow([], [])).toBe(true);
});
test('should return true for same array reference', () => {
const arr = [1, 2, 3];
expect(arraysEqualShallow(arr, arr)).toBe(true);
});
test('should return false when one array is undefined', () => {
expect(arraysEqualShallow([1, 2, 3], undefined)).toBe(false);
expect(arraysEqualShallow(undefined, [1, 2, 3])).toBe(false);
});
test('should return false for arrays with different lengths', () => {
expect(arraysEqualShallow([1, 2], [1, 2, 3])).toBe(false);
});
test('should return true for arrays with same elements in different order', () => {
expect(arraysEqualShallow([1, 2, 3], [3, 2, 1])).toBe(true);
});
test('should return false for arrays with different elements', () => {
expect(arraysEqualShallow([1, 2, 3], [1, 2, 4])).toBe(false);
});
test('should handle arrays with string elements', () => {
expect(arraysEqualShallow(['a', 'b'], ['b', 'a'])).toBe(true);
expect(arraysEqualShallow(['a', 'b'], ['a', 'c'])).toBe(false);
});
test('should handle arrays with duplicate elements', () => {
expect(arraysEqualShallow([1, 1, 2], [1, 2, 1])).toBe(true);
expect(arraysEqualShallow([1, 1], [1])).toBe(false);
expect(arraysEqualShallow([1, 2, 2], [1, 1, 2])).toBe(false);
});
test('should not mutate the array', () => {
const a = [1, 2, 3];
const b = [3, 2, 1];
expect(arraysEqualShallow(a, b)).toBe(true);
expect(a[0]).toBe(1);
expect(a[1]).toBe(2);
expect(a[2]).toBe(3);
expect(b[0]).toBe(3);
expect(b[1]).toBe(2);
expect(b[2]).toBe(1);
});
test('isRemotePath should return true for HTTP or HTTPS URLs', () => {
expect(isRemotePath('https://example.com')).toBe(true);
expect(isRemotePath('https://github.com/repo')).toBe(true);
expect(isRemotePath('http://example.com')).toBe(true);
expect(isRemotePath('http://localhost:3000')).toBe(true);
});
test('isRemotePath should return false for non HTTP paths', () => {
expect(isRemotePath('/usr/local/bin')).toBe(false);
expect(isRemotePath('./relative/path')).toBe(false);
expect(isRemotePath('C:\\Windows\\System32')).toBe(false);
expect(isRemotePath('')).toBe(false);
expect(isRemotePath(' ')).toBe(false);
});