trust-selector.test.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { setKeybindings } from "@earendil-works/pi-tui";
  2. import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
  3. import { KeybindingsManager } from "../src/core/keybindings.ts";
  4. import { TrustSelectorComponent } from "../src/modes/interactive/components/trust-selector.ts";
  5. import { initTheme } from "../src/modes/interactive/theme/theme.ts";
  6. import { stripAnsi } from "../src/utils/ansi.ts";
  7. describe("TrustSelectorComponent", () => {
  8. beforeAll(() => {
  9. initTheme("dark");
  10. });
  11. beforeEach(() => {
  12. setKeybindings(new KeybindingsManager());
  13. });
  14. it("marks the saved trusted decision", () => {
  15. const selector = new TrustSelectorComponent({
  16. cwd: "/project",
  17. savedDecision: { path: "/project", decision: true },
  18. projectTrusted: true,
  19. onSelect: () => {},
  20. onCancel: () => {},
  21. });
  22. const output = stripAnsi(selector.render(120).join("\n"));
  23. expect(output).toContain("Saved decision: trusted (/project)");
  24. expect(output).toContain("Current session: trusted");
  25. expect(output).toContain("Trust ✓");
  26. expect(output).not.toContain("Do not trust ✓");
  27. });
  28. it("selects a trust decision", () => {
  29. const onSelect = vi.fn();
  30. const selector = new TrustSelectorComponent({
  31. cwd: "/project",
  32. savedDecision: null,
  33. projectTrusted: false,
  34. onSelect,
  35. onCancel: () => {},
  36. });
  37. selector.handleInput("\n");
  38. expect(onSelect).toHaveBeenCalledWith({ trusted: true, updates: [{ path: "/project", decision: true }] });
  39. });
  40. it("labels saved ancestor decisions as inherited", () => {
  41. const selector = new TrustSelectorComponent({
  42. cwd: "/parent/project/nested",
  43. savedDecision: { path: "/parent", decision: true },
  44. projectTrusted: true,
  45. onSelect: () => {},
  46. onCancel: () => {},
  47. });
  48. const output = stripAnsi(selector.render(120).join("\n"));
  49. expect(output).toContain("Saved decision: trusted (inherited from /parent)");
  50. });
  51. it("adds a trust parent option", () => {
  52. const onSelect = vi.fn();
  53. const selector = new TrustSelectorComponent({
  54. cwd: "/parent/project",
  55. savedDecision: { path: "/parent", decision: true },
  56. projectTrusted: true,
  57. onSelect,
  58. onCancel: () => {},
  59. });
  60. const output = stripAnsi(selector.render(120).join("\n"));
  61. expect(output).toContain("Saved decision: trusted (inherited from /parent)");
  62. expect(output).toContain("Trust parent folder (/parent) ✓");
  63. selector.handleInput("\n");
  64. expect(onSelect).toHaveBeenCalledWith({
  65. trusted: true,
  66. updates: [
  67. { path: "/parent", decision: true },
  68. { path: "/parent/project", decision: null },
  69. ],
  70. });
  71. });
  72. });