|
1 | | -import { test, expect } from "bun:test" |
| 1 | +import { test, expect, describe } from "bun:test" |
2 | 2 | import { Config } from "../../src/config/config" |
3 | 3 | import { Instance } from "../../src/project/instance" |
4 | 4 | import { tmpdir } from "../fixture/fixture" |
5 | 5 | import path from "path" |
6 | 6 | import fs from "fs/promises" |
7 | 7 | import { pathToFileURL } from "url" |
| 8 | +import os from "os" |
8 | 9 |
|
9 | 10 | test("loads config with defaults when no files exist", async () => { |
10 | 11 | await using tmp = await tmpdir() |
@@ -501,3 +502,128 @@ test("deduplicates duplicate plugins from global and local configs", async () => |
501 | 502 | }, |
502 | 503 | }) |
503 | 504 | }) |
| 505 | + |
| 506 | +// Unit tests for pattern-based external directory permission helpers |
| 507 | +describe("getExternalDirectoryReadForPath", () => { |
| 508 | + test("returns permission when string", () => { |
| 509 | + expect(Config.getExternalDirectoryReadForPath("allow", "/any/path")).toBe("allow") |
| 510 | + expect(Config.getExternalDirectoryReadForPath("deny", "/any/path")).toBe("deny") |
| 511 | + expect(Config.getExternalDirectoryReadForPath("ask", "/any/path")).toBe("ask") |
| 512 | + }) |
| 513 | + |
| 514 | + test("returns undefined when permission is undefined", () => { |
| 515 | + expect(Config.getExternalDirectoryReadForPath(undefined, "/any/path")).toBeUndefined() |
| 516 | + }) |
| 517 | + |
| 518 | + test("returns permission from pattern map when path matches", () => { |
| 519 | + const permission = { |
| 520 | + read: { |
| 521 | + "/etc/**": "allow" as const, |
| 522 | + "*": "deny" as const, |
| 523 | + }, |
| 524 | + } |
| 525 | + expect(Config.getExternalDirectoryReadForPath(permission, "/etc/hosts")).toBe("allow") |
| 526 | + expect(Config.getExternalDirectoryReadForPath(permission, "/etc/subdir/file")).toBe("allow") |
| 527 | + expect(Config.getExternalDirectoryReadForPath(permission, "/other/path")).toBe("deny") |
| 528 | + }) |
| 529 | + |
| 530 | + test("returns catch-all (*) when no pattern matches", () => { |
| 531 | + const permission = { |
| 532 | + read: { |
| 533 | + "/nonexistent/**": "allow" as const, |
| 534 | + "*": "ask" as const, |
| 535 | + }, |
| 536 | + } |
| 537 | + expect(Config.getExternalDirectoryReadForPath(permission, "/etc/hosts")).toBe("ask") |
| 538 | + }) |
| 539 | + |
| 540 | + test("returns undefined when no pattern matches and no catch-all", () => { |
| 541 | + const permission = { |
| 542 | + read: { |
| 543 | + "/nonexistent/**": "deny" as const, |
| 544 | + }, |
| 545 | + } |
| 546 | + expect(Config.getExternalDirectoryReadForPath(permission, "/etc/hosts")).toBeUndefined() |
| 547 | + }) |
| 548 | + |
| 549 | + test("first matching pattern takes precedence", () => { |
| 550 | + const permission = { |
| 551 | + read: { |
| 552 | + "/etc/hosts": "allow" as const, |
| 553 | + "/etc/**": "deny" as const, |
| 554 | + "*": "ask" as const, |
| 555 | + }, |
| 556 | + } |
| 557 | + expect(Config.getExternalDirectoryReadForPath(permission, "/etc/hosts")).toBe("allow") |
| 558 | + }) |
| 559 | + |
| 560 | + test("expands ~ to home directory in patterns", () => { |
| 561 | + const homeDir = os.homedir() |
| 562 | + const permission = { |
| 563 | + read: { |
| 564 | + "~/.ssh/**": "deny" as const, |
| 565 | + "~/reference/**": "allow" as const, |
| 566 | + "*": "ask" as const, |
| 567 | + }, |
| 568 | + } |
| 569 | + expect(Config.getExternalDirectoryReadForPath(permission, `${homeDir}/.ssh/id_rsa`)).toBe("deny") |
| 570 | + expect(Config.getExternalDirectoryReadForPath(permission, `${homeDir}/reference/doc.txt`)).toBe("allow") |
| 571 | + expect(Config.getExternalDirectoryReadForPath(permission, `${homeDir}/other/file.txt`)).toBe("ask") |
| 572 | + }) |
| 573 | + |
| 574 | + test("returns simple read permission when read is string", () => { |
| 575 | + const permission = { |
| 576 | + read: "allow" as const, |
| 577 | + write: "deny" as const, |
| 578 | + } |
| 579 | + expect(Config.getExternalDirectoryReadForPath(permission, "/any/path")).toBe("allow") |
| 580 | + }) |
| 581 | +}) |
| 582 | + |
| 583 | +describe("getExternalDirectoryWriteForPath", () => { |
| 584 | + test("returns permission when string", () => { |
| 585 | + expect(Config.getExternalDirectoryWriteForPath("allow", "/any/path")).toBe("allow") |
| 586 | + expect(Config.getExternalDirectoryWriteForPath("deny", "/any/path")).toBe("deny") |
| 587 | + expect(Config.getExternalDirectoryWriteForPath("ask", "/any/path")).toBe("ask") |
| 588 | + }) |
| 589 | + |
| 590 | + test("returns undefined when permission is undefined", () => { |
| 591 | + expect(Config.getExternalDirectoryWriteForPath(undefined, "/any/path")).toBeUndefined() |
| 592 | + }) |
| 593 | + |
| 594 | + test("returns permission from pattern map when path matches", () => { |
| 595 | + const permission = { |
| 596 | + write: { |
| 597 | + "/tmp/**": "allow" as const, |
| 598 | + "*": "deny" as const, |
| 599 | + }, |
| 600 | + } |
| 601 | + expect(Config.getExternalDirectoryWriteForPath(permission, "/tmp/file.txt")).toBe("allow") |
| 602 | + expect(Config.getExternalDirectoryWriteForPath(permission, "/tmp/subdir/file")).toBe("allow") |
| 603 | + expect(Config.getExternalDirectoryWriteForPath(permission, "/other/path")).toBe("deny") |
| 604 | + }) |
| 605 | + |
| 606 | + test("expands ~ to home directory in patterns", () => { |
| 607 | + const homeDir = os.homedir() |
| 608 | + const permission = { |
| 609 | + write: { |
| 610 | + "~/temp/**": "allow" as const, |
| 611 | + "*": "deny" as const, |
| 612 | + }, |
| 613 | + } |
| 614 | + expect(Config.getExternalDirectoryWriteForPath(permission, `${homeDir}/temp/file.txt`)).toBe("allow") |
| 615 | + expect(Config.getExternalDirectoryWriteForPath(permission, `${homeDir}/other/file.txt`)).toBe("deny") |
| 616 | + }) |
| 617 | + |
| 618 | + test("mixed config: pattern map for write, simple value for read", () => { |
| 619 | + const permission = { |
| 620 | + read: "allow" as const, |
| 621 | + write: { |
| 622 | + "/protected/**": "deny" as const, |
| 623 | + "*": "ask" as const, |
| 624 | + }, |
| 625 | + } |
| 626 | + expect(Config.getExternalDirectoryWriteForPath(permission, "/protected/file.txt")).toBe("deny") |
| 627 | + expect(Config.getExternalDirectoryWriteForPath(permission, "/other/file.txt")).toBe("ask") |
| 628 | + }) |
| 629 | +}) |
0 commit comments