mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-25 03:34:22 +01:00
fix: more durable @ references for commands (#2386)
This commit is contained in:
@@ -1,12 +1,8 @@
|
||||
import { describe, expect, test, beforeAll, mock } from "bun:test"
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Session } from "../../src/session/index"
|
||||
|
||||
describe("processFileReferences", () => {
|
||||
let result: any
|
||||
|
||||
beforeAll(async () => {
|
||||
mock.module("os", () => ({ default: { homedir: () => "/home/fake-user" } }))
|
||||
const { processFileReferences } = await import("../../src/session/file-reference")
|
||||
const template = `This is a @valid/path/to/a/file and it should also match at
|
||||
describe("fileRegex", () => {
|
||||
const template = `This is a @valid/path/to/a/file and it should also match at
|
||||
the beginning of a line:
|
||||
|
||||
@another-valid/path/to/a/file
|
||||
@@ -26,77 +22,70 @@ Also shouldn't forget @/absolute/paths.txt with and @/without/extensions,
|
||||
as well as @~/home-files and @~/paths/under/home.txt.
|
||||
|
||||
If the reference is \`@quoted/in/backticks\` then it shouldn't match at all.`
|
||||
result = processFileReferences(template, "/base")
|
||||
})
|
||||
|
||||
const matches = Array.from(template.matchAll(Session.fileRegex))
|
||||
|
||||
test("should extract exactly 12 file references", () => {
|
||||
expect(result.length).toBe(12)
|
||||
})
|
||||
|
||||
test("all files should have correct type and mime", () => {
|
||||
result.forEach((file: any) => {
|
||||
expect(file.type).toBe("file")
|
||||
expect(file.mime).toBe("text/plain")
|
||||
})
|
||||
expect(matches.length).toBe(12)
|
||||
})
|
||||
|
||||
test("should extract valid/path/to/a/file", () => {
|
||||
expect(result[0].filename).toBe("valid/path/to/a/file")
|
||||
expect(result[0].url).toBe("file:///base/valid/path/to/a/file")
|
||||
expect(matches[0][1]).toBe("valid/path/to/a/file")
|
||||
})
|
||||
|
||||
test("should extract another-valid/path/to/a/file", () => {
|
||||
expect(result[1].filename).toBe("another-valid/path/to/a/file")
|
||||
expect(result[1].url).toBe("file:///base/another-valid/path/to/a/file")
|
||||
expect(matches[1][1]).toBe("another-valid/path/to/a/file")
|
||||
})
|
||||
|
||||
test("should extract paths ignoring comma after", () => {
|
||||
expect(result[2].filename).toBe("commas")
|
||||
expect(result[2].url).toBe("file:///base/commas")
|
||||
expect(matches[2][1]).toBe("commas")
|
||||
})
|
||||
|
||||
test("should extract a path with a file extension and comma after", () => {
|
||||
expect(result[3].filename).toBe("file-extensions.md")
|
||||
expect(result[3].url).toBe("file:///base/file-extensions.md")
|
||||
expect(matches[3][1]).toBe("file-extensions.md")
|
||||
})
|
||||
|
||||
test("should extract a path with multiple dots and comma after", () => {
|
||||
expect(result[4].filename).toBe("multiple.extensions.bak")
|
||||
expect(result[4].url).toBe("file:///base/multiple.extensions.bak")
|
||||
expect(matches[4][1]).toBe("multiple.extensions.bak")
|
||||
})
|
||||
|
||||
test("should extract hidden directory", () => {
|
||||
expect(result[5].filename).toBe(".config/")
|
||||
expect(result[5].url).toBe("file:///base/.config")
|
||||
expect(matches[5][1]).toBe(".config/")
|
||||
})
|
||||
|
||||
test("should extract hidden file", () => {
|
||||
expect(result[6].filename).toBe(".bashrc")
|
||||
expect(result[6].url).toBe("file:///base/.bashrc")
|
||||
expect(matches[6][1]).toBe(".bashrc")
|
||||
})
|
||||
|
||||
test("should extract a file ignoring period at end of sentence", () => {
|
||||
expect(result[7].filename).toBe("foo.md")
|
||||
expect(result[7].url).toBe("file:///base/foo.md")
|
||||
expect(matches[7][1]).toBe("foo.md")
|
||||
})
|
||||
|
||||
test("should extract an absolute path with an extension", () => {
|
||||
expect(result[8].filename).toBe("/absolute/paths.txt")
|
||||
expect(result[8].url).toBe("file:///absolute/paths.txt")
|
||||
expect(matches[8][1]).toBe("/absolute/paths.txt")
|
||||
})
|
||||
|
||||
test("should extract an absolute path without an extension", () => {
|
||||
expect(result[9].filename).toBe("/without/extensions")
|
||||
expect(result[9].url).toBe("file:///without/extensions")
|
||||
expect(matches[9][1]).toBe("/without/extensions")
|
||||
})
|
||||
|
||||
test("should extract an absolute path in home directory", () => {
|
||||
expect(result[10].filename).toBe("~/home-files")
|
||||
expect(result[10].url).toBe("file:///home/fake-user/home-files")
|
||||
expect(matches[10][1]).toBe("~/home-files")
|
||||
})
|
||||
|
||||
test("should extract an absolute path under home directory", () => {
|
||||
expect(result[11].filename).toBe("~/paths/under/home.txt")
|
||||
expect(result[11].url).toBe("file:///home/fake-user/paths/under/home.txt")
|
||||
expect(matches[11][1]).toBe("~/paths/under/home.txt")
|
||||
})
|
||||
|
||||
test("should not match when preceded by backtick", () => {
|
||||
const backtickTest = "This `@should/not/match` should be ignored"
|
||||
const backtickMatches = Array.from(backtickTest.matchAll(Session.fileRegex))
|
||||
expect(backtickMatches.length).toBe(0)
|
||||
})
|
||||
|
||||
test("should not match email addresses", () => {
|
||||
const emailTest = "Contact user@example.com for help"
|
||||
const emailMatches = Array.from(emailTest.matchAll(Session.fileRegex))
|
||||
expect(emailMatches.length).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user