π€― LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
test(database): raise coverage back to 100% (#20794)
Arvin Xu4c1cc23810e3
diff --git a/packages/database/src/models/__tests__/environmentInstance.test.ts b/packages/database/src/models/__tests__/environmentInstance.test.tsnew file mode 100644index 00000000000..86ed31b0675--- /dev/null+++ b/packages/database/src/models/__tests__/environmentInstance.test.ts@@ -0,0 +1,221 @@+// @vitest-environment node+import { randomUUID } from 'node:crypto';++import { afterEach, beforeEach, describe, expect, it } from 'vitest';++import { getTestDB } from '../../core/getTestDB';+import { devices, environmentInstances, environments, users, workspaces } from '../../schemas';+import type { LobeChatDatabase } from '../../type';+import { EnvironmentInstanceModel, InstanceDirectoryOverlapError } from '../environmentInstance';++const db: LobeChatDatabase = await getTestDB();++const userId = 'env-instance-user';+const otherUserId = 'env-instance-other';+const workspaceId = 'env-instance-ws';++const model = new EnvironmentInstanceModel(db, userId);+const otherModel = new EnvironmentInstanceModel(db, otherUserId);++/** Every environment needs a unique (userId, workspaceId, name) β randomise it. */+const makeEnv = async (+ overrides: { visibility?: 'private' | 'public'; workspaceId?: string } = {},+) => {+ const [row] = await db+ .insert(environments)+ .values({+ configuration: { sources: [] },+ name: `env-${randomUUID()}`,+ userId,+ ...overrides,+ })+ .returning();+ return row;+};++const makeDevice = async () => {+ const [row] = await db+ .insert(devices)+ .values({ deviceId: `machine-${randomUUID()}`, identitySource: 'machine-id', userId })+ .returning();+ return row;+};++/** Provider-bound instance input; `providerResourceId` scopes the storage. */+const sandbox = (environmentId: string, name: string, dir: string, resource = 'r') => ({+ environmentId,+ kind: 'sandbox' as const,+ name,+ provider: 'e2b',+ providerResourceId: resource,+ providerScope: 'scope',+ workingDirectory: dir,+});++beforeEach(async () => {+ await db.delete(users);+ await db.insert(users).values([{ id: userId }, { id: otherUserId }]);+ await db+ .insert(workspaces)+ .values({ id: workspaceId, name: 'ws', primaryOwnerId: userId, slug: workspaceId });+});++afterEach(async () => {+ await db.delete(environmentInstances);+ await db.delete(environments);+ await db.delete(devices);+ await db.delete(users);+});++describe('EnvironmentInstanceModel', () => {+ it('lists visible instances and narrows by environment id', async () => {+ const envA = await makeEnv();+ const envB = await makeEnv();+ const a = (await model.create(sandbox(envA.id, 'a', '/work/a', 'ra')))!;+ const b = (await model.create(sandbox(envB.id, 'b', '/work/b', 'rb')))!;++ const all = await model.query();+ expect(all.map((r) => r.id).sort()).toEqual([a.id, b.id].sort());++ const onlyA = await model.query({ environmentId: envA.id });+ expect(onlyA.map((r) => r.id)).toEqual([a.id]);+ });++ it('resolves instances through visibility and ownership', async () => {+ const env = await makeEnv();+ const row = (await model.create(sandbox(env.id, 'x', '/work/x')))!;++ await expect(model.findById(row.id)).resolves.toMatchObject({ id: row.id });+ await expect(model.findOwnedById(row.id)).resolves.toMatchObject({ id: row.id });+ await expect(model.findByWorkingDirectory('/work/x')).resolves.toMatchObject({ id: row.id });+ await expect(model.findByWorkingDirectory('/work/missing')).resolves.toBeUndefined();+ // No device binding β the device+directory lookup finds nothing.+ await expect(+ model.findByDeviceAndDirectory('00000000-0000-0000-0000-000000000000', '/work/x'),+ ).resolves.toBeUndefined();++ // A personal environment's instance is invisible to (and not owned by) another user.+ await expect(otherModel.findById(row.id)).resolves.toBeUndefined();+ await expect(otherModel.findOwnedById(row.id)).resolves.toBeUndefined();+ });++ it('refuses to create an instance in an environment the caller does not own', async () => {+ const env = await makeEnv({ visibility: 'public', workspaceId });+ const created = await otherModel.create(sandbox(env.id, 'nope', '/work/nope'));++ expect(created).toBeUndefined();+ });++ it('creates device-bound instances and resolves them by device and directory', async () => {+ const device = await makeDevice();+ const env = await makeEnv();++ const row = (await model.create({+ deviceId: device.id,+ environmentId: env.id,+ kind: 'device',+ name: 'd',+ workingDirectory: '/work/d',+ }))!;+ expect(row.kind).toBe('device');++ await expect(model.findByDeviceAndDirectory(device.id, '/work/d')).resolves.toMatchObject({+ id: row.id,+ });++ const direct = await model.createForDeviceBinding({+ configurationSnapshot: { sources: [] },+ deviceId: device.id,+ environmentId: env.id,+ name: 'd2',+ workingDirectory: '/work/d2',+ });+ expect(direct).toMatchObject({ kind: 'device', workingDirectory: '/work/d2' });+ });++ it('rejects a directory that overlaps an existing instance on the same storage', async () => {+ const env = await makeEnv();+ await model.create(sandbox(env.id, 'app', '/work/app'));++ await expect(model.create(sandbox(env.id, 'nested', '/work/app/sub'))).rejects.toBeInstanceOf(+ InstanceDirectoryOverlapError,+ );+ });++ it('rejects a binding that declares neither a device nor a provider resource', async () => {+ const env = await makeEnv();+ // With no device the binding falls back to the provider triple; all three are+ // empty here, so the row fails the `environment_instances_binding` check.+ await expect(+ model.create({+ environmentId: env.id,+ kind: 'device',+ name: 'malformed',+ workingDirectory: '/work/malformed',+ }),+ ).rejects.toThrow();+ });++ it('snapshots the environment configuration unless one is supplied', async () => {+ const configuration = {+ sources: [{ kind: 'git' as const, url: 'https://github.com/lobehub/lobehub' }],+ };+ const [env] = await db+ .insert(environments)+ .values({ configuration, name: `env-${randomUUID()}`, userId })+ .returning();++ const defaulted = (await model.create(sandbox(env.id, 's1', '/work/s1', 'r1')))!;+ expect(defaulted.configurationSnapshot).toEqual(configuration);++ const explicit = (await model.create({+ ...sandbox(env.id, 's2', '/work/s2', 'r2'),+ configurationSnapshot: { sources: [] },+ }))!;+ expect(explicit.configurationSnapshot).toEqual({ sources: [] });+ });++ it('records a build verdict only for the matching build id', async () => {+ const env = await makeEnv();+ const row = (await model.create(sandbox(env.id, 'b', '/work/b')))!;+ await model.update(row.id, { buildId: 'build-1' });++ await model.recordBuildResult(row.id, 'stale-build', { buildError: null, status: 'ready' });+ let reread = (await model.findById(row.id))!;+ expect(reread.status).toBe('pending');+ expect(reread.buildId).toBe('build-1');++ await model.recordBuildResult(row.id, 'build-1', { buildError: 'boom', status: 'error' });+ reread = (await model.findById(row.id))!;+ expect(reread).toMatchObject({ buildError: 'boom', buildId: null, status: 'error' });+ });++ it('scopes updates and deletes to the owner of the environment', async () => {+ const env = await makeEnv();+ const row = (await model.create(sandbox(env.id, 'u', '/work/u')))!;++ await expect(otherModel.update(row.id, { name: 'hijack' })).resolves.toBeUndefined();+ await expect(otherModel.delete(row.id)).resolves.toBeUndefined();++ await expect(model.update(row.id, { enabled: false })).resolves.toMatchObject({ id: row.id });+ await expect(model.delete(row.id)).resolves.toMatchObject({ id: row.id });+ });++ it('shows a published workspace environment to members while keeping a public agent out of a private one', async () => {+ const publicEnv = await makeEnv({ visibility: 'public', workspaceId });+ const privateEnv = await makeEnv({ visibility: 'private', workspaceId });+ const owner = new EnvironmentInstanceModel(db, userId, workspaceId);++ const pubRow = (await owner.create(sandbox(publicEnv.id, 'pub', '/work/pub', 'r1')))!;+ const privRow = (await owner.create(sandbox(privateEnv.id, 'priv', '/work/priv', 'r2')))!;++ const member = new EnvironmentInstanceModel(db, otherUserId, workspaceId);+ await expect(member.findById(pubRow.id)).resolves.toMatchObject({ id: pubRow.id });+ await expect(member.findById(privRow.id)).resolves.toBeUndefined();++ // A workspace-public agent may read published rows only β never a private one.+ const publicAgent = new EnvironmentInstanceModel(db, userId, workspaceId, 'public');+ await expect(publicAgent.findById(pubRow.id)).resolves.toMatchObject({ id: pubRow.id });+ await expect(publicAgent.findById(privRow.id)).resolves.toBeUndefined();+ });+});diff --git a/packages/database/src/models/scm/__tests__/installation.test.ts b/packages/database/src/models/scm/__tests__/installation.test.tsnew file mode 100644index 00000000000..aadc4bd69b8--- /dev/null+++ b/packages/database/src/models/scm/__tests__/installation.test.ts@@ -0,0 +1,257 @@+// @vitest-environment node+import { eq } from 'drizzle-orm';+import { afterEach, beforeEach, describe, expect, it } from 'vitest';++import { getTestDB } from '../../../core/getTestDB';+import { scmInstallations, users, workspaces } from '../../../schemas';+import type { LobeChatDatabase } from '../../../type';+import { ScmInstallationModel } from '../installation';++const db: LobeChatDatabase = await getTestDB();++const userId = 'scm-install-user';+const otherUserId = 'scm-install-other';+const workspaceId = 'scm-install-ws';++/** Snapshot shared by the fixtures; individual tests override the parts they exercise. */+const account = {+ accountExternalId: 'ext-1',+ accountLogin: 'lobehub',+ accountType: 'organization' as const,+};++beforeEach(async () => {+ await db.delete(scmInstallations);+ await db.delete(users);+ await db.insert(users).values([{ id: userId }, { id: otherUserId }]);+ await db+ .insert(workspaces)+ .values({ id: workspaceId, name: 'ws', primaryOwnerId: userId, slug: workspaceId });+});++afterEach(async () => {+ await db.delete(scmInstallations);+ await db.delete(users);+});++describe('ScmInstallationModel', () => {+ it('binds a new installation with defaults and reads it back by id and provider id', async () => {+ const row = await ScmInstallationModel.bind(db, {+ ...account,+ installationId: '1',+ provider: 'github',+ repositorySelection: 'all',+ userId,+ });++ // Optional fields default rather than persist as null/undefined.+ expect(row).toMatchObject({+ accountLogin: 'lobehub',+ metadata: {},+ repositories: [],+ revokedAt: null,+ suspendedAt: null,+ workspaceId: null,+ });++ await expect(ScmInstallationModel.findById(db, row.id)).resolves.toMatchObject({ id: row.id });+ await expect(+ ScmInstallationModel.findByProviderInstallationId(db, 'github', '1'),+ ).resolves.toMatchObject({ id: row.id });+ });++ it('returns null for an unknown id or provider installation', async () => {+ await expect(+ ScmInstallationModel.findById(db, '00000000-0000-0000-0000-000000000000'),+ ).resolves.toBeNull();+ await expect(+ ScmInstallationModel.findByProviderInstallationId(db, 'github', 'missing'),+ ).resolves.toBeNull();+ });++ it('re-binds an existing installation id in place, moving scope and clearing revocation', async () => {+ const first = await ScmInstallationModel.bind(db, {+ ...account,+ installationId: '9',+ provider: 'github',+ repositorySelection: 'all',+ userId,+ });+ await ScmInstallationModel.markRevoked(db, first.id);++ const again = await ScmInstallationModel.bind(db, {+ ...account,+ installationId: '9',+ installedByExternalLogin: 'octocat',+ installedByExternalUserId: '5',+ metadata: { events: ['push'] },+ provider: 'github',+ repositories: [{ externalId: 'r1', fullName: 'a/b' }],+ repositorySelection: 'selected',+ suspendedAt: new Date('2026-01-01T00:00:00.000Z'),+ userId: otherUserId,+ workspaceId,+ });++ expect(again.id).toBe(first.id);+ expect(again).toMatchObject({+ installedByExternalLogin: 'octocat',+ installedByExternalUserId: '5',+ metadata: { events: ['push'] },+ repositories: [{ externalId: 'r1', fullName: 'a/b' }],+ repositorySelection: 'selected',+ revokedAt: null,+ userId: otherUserId,+ workspaceId,+ });+ expect(again.suspendedAt).toBeInstanceOf(Date);+ });++ it('retires any other live row for the same provider account when binding under a new id', async () => {+ const stale = await ScmInstallationModel.bind(db, {+ ...account,+ installationId: 'old',+ provider: 'github',+ repositorySelection: 'all',+ userId,+ });++ await ScmInstallationModel.bind(db, {+ ...account,+ installationId: 'new',+ provider: 'github',+ repositorySelection: 'all',+ userId,+ });++ const [reread] = await db+ .select()+ .from(scmInstallations)+ .where(eq(scmInstallations.id, stale.id));+ expect(reread.revokedAt).toBeInstanceOf(Date);+ });++ it('lists only active personal installations for a personal scope', async () => {+ const active = await ScmInstallationModel.bind(db, {+ ...account,+ installationId: 'p1',+ provider: 'github',+ repositorySelection: 'all',+ userId,+ });+ const revoked = await ScmInstallationModel.bind(db, {+ accountExternalId: 'ext-2',+ accountLogin: 'other',+ accountType: 'user',+ installationId: 'p2',+ provider: 'github',+ repositorySelection: 'all',+ userId,+ });+ await ScmInstallationModel.markRevoked(db, revoked.id);+ // A workspace-scoped row must not leak into the personal list. It carries a+ // distinct provider account so `bind`'s retire step does not revoke `active`.+ await ScmInstallationModel.bind(db, {+ accountExternalId: 'ext-3',+ accountLogin: 'workspace-account',+ accountType: 'organization',+ installationId: 'w1',+ provider: 'github',+ repositorySelection: 'all',+ userId,+ workspaceId,+ });++ const list = await ScmInstallationModel.listByScope(db, { userId });+ expect(list.map((r) => r.id)).toEqual([active.id]);+ });++ it('lists active installations for a workspace scope', async () => {+ const scoped = await ScmInstallationModel.bind(db, {+ ...account,+ installationId: 'w1',+ provider: 'github',+ repositorySelection: 'all',+ userId,+ workspaceId,+ });+ await ScmInstallationModel.bind(db, {+ accountExternalId: 'ext-2',+ accountLogin: 'other',+ accountType: 'user',+ installationId: 'p2',+ provider: 'github',+ repositorySelection: 'all',+ userId,+ });++ const list = await ScmInstallationModel.listByScope(db, { userId, workspaceId });+ expect(list.map((r) => r.id)).toEqual([scoped.id]);+ });++ it('refreshes the snapshot, replaces repositories and toggles suspension and revocation', async () => {+ const row = await ScmInstallationModel.bind(db, {+ ...account,+ installationId: 'm1',+ provider: 'github',+ repositorySelection: 'all',+ userId,+ });++ await ScmInstallationModel.refreshSnapshot(db, row.id, { accountLogin: 'renamed' });+ await ScmInstallationModel.setRepositories(db, row.id, [{ externalId: 'r1', fullName: 'a/b' }]);+ await ScmInstallationModel.setSuspended(db, row.id, true);++ let reread = await ScmInstallationModel.findById(db, row.id);+ expect(reread).toMatchObject({+ accountLogin: 'renamed',+ repositories: [{ externalId: 'r1', fullName: 'a/b' }],+ });+ expect(reread!.suspendedAt).toBeInstanceOf(Date);++ await ScmInstallationModel.setSuspended(db, row.id, false);+ reread = await ScmInstallationModel.findById(db, row.id);+ expect(reread!.suspendedAt).toBeNull();++ await ScmInstallationModel.markRevoked(db, row.id);+ reread = await ScmInstallationModel.findById(db, row.id);+ expect(reread!.revokedAt).toBeInstanceOf(Date);+ });++ it('merges a repository change under a row lock, deduping adds and drops', async () => {+ const row = await ScmInstallationModel.bind(db, {+ ...account,+ installationId: 'c1',+ provider: 'github',+ repositories: [+ { externalId: 'r1', fullName: 'a/1' },+ { externalId: 'r2', fullName: 'a/2' },+ ],+ repositorySelection: 'selected',+ userId,+ });++ const next = await ScmInstallationModel.applyRepositoryChange(db, row.id, {+ // r2 is already granted β the add is dropped; rX is not granted β the removal is a no-op.+ added: [+ { externalId: 'r3', fullName: 'a/3' },+ { externalId: 'r2', fullName: 'a/2' },+ ],+ removed: [+ { externalId: 'r1', fullName: 'a/1' },+ { externalId: 'rX', fullName: 'a/x' },+ ],+ });++ expect(next.map((r) => r.externalId)).toEqual(['r2', 'r3']);+ });++ it('returns an empty list when applying a repository change to a missing installation', async () => {+ const result = await ScmInstallationModel.applyRepositoryChange(+ db,+ '00000000-0000-0000-0000-000000000000',+ { added: [], removed: [] },+ );+ expect(result).toEqual([]);+ });+});