Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

5.1 KB
Raw Blame
import { describe, expect, it } from 'vitest';

import {
  remoteContentScriptsUnverified,
  type InstallScriptsTrust,
} from '../installScriptsTrust';

const A = 'a'.repeat(40);
const B = 'b'.repeat(40);
const TIP = 'c'.repeat(40);
const FROM = 'd'.repeat(40);
const REWRITTEN = 'e'.repeat(40);

function trust(
  overrides: Partial<InstallScriptsTrust> & { ancestry?: Array<[string, string]> } = {},
): InstallScriptsTrust {
  const ancestry = new Set((overrides.ancestry ?? []).map(([a, b]) => `${a}>${b}`));
  return {
    unverifiedRemote: overrides.unverifiedRemote ?? (() => []),
    unverifiedRemoteOverflow: overrides.unverifiedRemoteOverflow,
    rewrites: overrides.rewrites ?? (() => []),
    published: overrides.published ?? (() => []),
    contains: overrides.contains ?? (async (a, b) => a === b || ancestry.has(`${a}>${b}`)),
  };
}

describe('remoteContentScriptsUnverified', () => {
  it('runs the scripts of this computer’s own content', async () => {
    await expect(remoteContentScriptsUnverified(A, trust())).resolves.toBe(false);
  });

  it('skips the scripts while a taken-over fork tip is not verified', async () => {
    await expect(
      remoteContentScriptsUnverified(A, {
        ...trust({ ancestry: [[TIP, A]] }),
        unverifiedRemote: () => [TIP],
      }),
    ).resolves.toBe(true);
  });

  it('stays guarded when the trust ledger records an overflow', async () => {
    await expect(
      remoteContentScriptsUnverified(A, {
        ...trust(),
        unverifiedRemoteOverflow: () => true,
      }),
    ).resolves.toBe(true);
  });

  it.each(['exact', 'descendant', 'rewrite'] as const)(
    'allows an overflowed ledger only when a published version covers the whole task base: %s',
    async (coverage) => {
      const facts = trust({
        unverifiedRemoteOverflow: () => true,
        published: () => [coverage === 'exact' ? A : B],
        ancestry: coverage === 'descendant' ? [[A, B]] : [[REWRITTEN, B]],
        rewrites: () => coverage === 'rewrite' ? [{ from: [A], to: REWRITTEN }] : [],
      });
      await expect(remoteContentScriptsUnverified(A, facts)).resolves.toBe(false);
      // The persistent overflow is not cleared: a different, uncovered task base
      // must still skip scripts, even after that generation.
      await expect(remoteContentScriptsUnverified(TIP, facts)).resolves.toBe(true);
      expect(facts.unverifiedRemoteOverflow?.()).toBe(true);
    },
  );

  it('does not use an earlier or invalid published commit to settle an overflow', async () => {
    await expect(remoteContentScriptsUnverified(A, trust({
      unverifiedRemoteOverflow: () => true,
      published: () => [B, 'invalid'],
      ancestry: [[B, A]],
    }))).resolves.toBe(true);
  });

  it('does not settle an overflow when published coverage cannot be read', async () => {
    await expect(remoteContentScriptsUnverified(A, trust({
      unverifiedRemoteOverflow: () => true,
      published: () => [B],
      contains: async () => { throw new Error('git failed'); },
    }))).rejects.toThrow('git failed');
  });

  it('runs the scripts again once a generated personal version covers the tip', async () => {
    await expect(
      remoteContentScriptsUnverified(A, {
        ...trust({ ancestry: [[TIP, A], [TIP, B]] }),
        unverifiedRemote: () => [TIP],
        published: () => [B],
      }),
    ).resolves.toBe(false);
  });

  it('does not block on fork tips this task does not contain', async () => {
    await expect(
      remoteContentScriptsUnverified(A, {
        ...trust(),
        unverifiedRemote: () => [TIP],
      }),
    ).resolves.toBe(false);
  });

  it('keeps the provenance across an adopted rewrite of the taken-over content', async () => {
    // An official update rebased the personal commits: the content survives under
    // new hashes, and the unverified tip travels through the rewrite record.
    const rewritten = {
      ...trust({ ancestry: [[TIP, FROM], [REWRITTEN, A]] }),
      unverifiedRemote: () => [TIP],
      rewrites: () => [{ from: [FROM], to: REWRITTEN }],
    };
    await expect(remoteContentScriptsUnverified(A, rewritten)).resolves.toBe(true);
    // A version published from somewhere else does not verify the rewritten content…
    await expect(
      remoteContentScriptsUnverified(A, { ...rewritten, published: () => [B] }),
    ).resolves.toBe(true);
    // …but a generated personal version that covers it does.
    await expect(
      remoteContentScriptsUnverified(A, {
        ...rewritten,
        published: () => [B],
        contains: async (a, b) =>
          new Set([`${TIP}>${FROM}`, `${REWRITTEN}>${B}`]).has(`${a}>${b}`),
      }),
    ).resolves.toBe(false);
  });

  it('decides conservatively for an unreadable base commit', async () => {
    await expect(remoteContentScriptsUnverified('not-a-commit', trust())).resolves.toBe(true);
  });

  it('surfaces Git failures so the caller can skip the scripts', async () => {
    await expect(
      remoteContentScriptsUnverified(A, {
        ...trust(),
        unverifiedRemote: () => [TIP],
        contains: async () => {
          throw Object.assign(new Error('git failed'), { exitCode: 128 });
        },
      }),
    ).rejects.toThrow('git failed');
  });
});