Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/missionmatch / internal/platform/docextract/docextract_test.go |
| Date | Not specified in source |
| Repository | Janady13/missionmatch |
| Revision | 720d47fae476b0d38477ae4401697d372b1ea2bc |
| Branch | feature/opportunity-to-outcome |
| Path or archive member | internal/platform/docextract/docextract_test.go |
| Original SHA-256 | fda9fb8e1a843ac98db44c08be4b51d0f620c3b91861afef7826b4396a094243 |
| Git blob SHA | bfb394d5b4403466dd607f659be1923b186d9e1c |
| Reading-copy SHA-256 | fda9fb8e1a843ac98db44c08be4b51d0f620c3b91861afef7826b4396a094243 |
Every recorded occurrence
These 15 records contain identical original source bytes. Their paths and revisions remain part of the archive.
| Original record | Branch or archive | Revision / identifier | Original location |
|---|---|---|---|
| Janady13/missionmatch / internal/platform/docextract/docextract_test.go | feature/opportunity-to-outcome | 720d47fae476b0d38477ae4401697d372b1ea2bc | Open original |
| Janady13/missionmatch / internal/platform/docextract/docextract_test.go | collision-deconfliction | 4ee5a09972608ed89f8377c6665542d5f450f0a6 | Open original |
| Janady13/missionmatch / internal/platform/docextract/docextract_test.go | m5-price-to-win | 41000ad30fff4129fdaf5ec84e15ba14c27d4890 | Open original |
| Janady13/missionmatch / internal/platform/docextract/docextract_test.go | master | 1e9249840a4302ec82e123b9c9cb6f75badea3e4 | Open original |
| Janady13/missionmatch / internal/platform/docextract/docextract_test.go | ws8-teaching-loop | c823f0fb6bb2aba0f39001eb6de96f4000d9321f | Open original |
| Janady13/missionmatch / internal/platform/docextract/docextract_test.go | mag-substrate | b82c7100a492dec67dfd1eda6b3f53def859be35 | Open original |
| Janady13/missionmatch / internal/platform/docextract/docextract_test.go | time-to-fill-survival | b66e1e505863d4f17741a4b9ede73509e45bd6c6 | Open original |
| Janady13/missionmatch / internal/platform/docextract/docextract_test.go | graphrag-foundation | b0ddeb9f633fd138d09309d1a17b7d13bc3a67f7 | Open original |
| Janady13/missionmatch / internal/platform/docextract/docextract_test.go | ws9-bench-ingestion | f585a9ef32fa554663ce1fba9a414a54b6e49cf0 | Open original |
| Janady13/missionmatch / internal/platform/docextract/docextract_test.go | presentation-readiness | eb574acaa29d71ce4d0f852572e87c275c79fdb7 | Open original |
| Janady13/missionmatch / internal/platform/docextract/docextract_test.go | vendor-intelligence | fe9796a19c261951d3f2565206c0c1b5ac294465 | Open original |
| Janady13/missionmatch / internal/platform/docextract/docextract_test.go | ws7-federal-signal | 6a9f6ce9cfb13345ffb47770d4960e7c2d9c17c2 | Open original |
| Janady13/missionmatch / internal/platform/docextract/docextract_test.go | phase2-connectors | e038d4c687aec16f8bd9bc4d907eea14d91f8704 | Open original |
| Janady13/missionmatch / internal/platform/docextract/docextract_test.go | feature/aioe-upgrade | 2178e7298d0e5d9af83afa12402fd587f0ba6ed8 | Open original |
| Janady13/missionmatch / internal/platform/docextract/docextract_test.go | capture-readiness | bae6af7cb2997e819174a9e9cfff5e16067a1975 | Open original |
Recovered source transcript
Download complete source text ↓235 lines · 8.2 KB
package docextract
import (
"archive/zip"
"bytes"
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
)
// TestMain regenerates the test fixtures from code before the suite runs, so the fixtures are fully
// reproducible (no opaque binary blobs that only a human could recreate). The generated files are also
// committed under testdata so the suite still runs if regeneration were ever skipped. Each generator
// builds a real file of its format: a valid single page PDF, a valid DOCX zip, a valid HTML document,
// and a plain text file.
func TestMain(m *testing.M) {
if err := writeFixtures(); err != nil {
fmt.Fprintf(os.Stderr, "docextract: write fixtures: %v\n", err)
os.Exit(1)
}
os.Exit(m.Run())
}
func writeFixtures() error {
if err := os.MkdirAll("testdata", 0o755); err != nil {
return err
}
if err := os.WriteFile(filepath.Join("testdata", "sample.pdf"), buildMinimalPDF("SIGINT Analyst"), 0o644); err != nil {
return err
}
docx, err := buildMinimalDOCX("ISSO")
if err != nil {
return err
}
if err := os.WriteFile(filepath.Join("testdata", "sample.docx"), docx, 0o644); err != nil {
return err
}
html := "<!doctype html><html><head><title>Roles</title><style>p{color:red}</style></head>" +
"<body><h1>Open Positions</h1><p>COMSEC Custodian</p><script>var x=1;</script></body></html>"
if err := os.WriteFile(filepath.Join("testdata", "sample.html"), []byte(html), 0o644); err != nil {
return err
}
if err := os.WriteFile(filepath.Join("testdata", "sample.txt"), []byte("Cyber Engineer needed for the program."), 0o644); err != nil {
return err
}
return nil
}
// buildMinimalPDF builds a single page PDF whose content stream renders the given text. The cross
// reference table offsets are computed from the running byte length so the file is structurally valid
// and a pure Go reader can parse it.
func buildMinimalPDF(text string) []byte {
var buf bytes.Buffer
var offsets []int
mark := func() { offsets = append(offsets, buf.Len()) }
buf.WriteString("%PDF-1.4\n")
mark()
buf.WriteString("1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n")
mark()
buf.WriteString("2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n")
mark()
buf.WriteString("3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Resources << /Font << /F1 5 0 R >> >> /Contents 4 0 R >>\nendobj\n")
content := fmt.Sprintf("BT /F1 24 Tf 72 700 Td (%s) Tj ET", text)
mark()
buf.WriteString(fmt.Sprintf("4 0 obj\n<< /Length %d >>\nstream\n%s\nendstream\nendobj\n", len(content), content))
mark()
buf.WriteString("5 0 obj\n<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>\nendobj\n")
xrefStart := buf.Len()
buf.WriteString("xref\n0 6\n0000000000 65535 f \n")
for _, off := range offsets {
buf.WriteString(fmt.Sprintf("%010d 00000 n \n", off))
}
buf.WriteString("trailer\n<< /Size 6 /Root 1 0 R >>\n")
buf.WriteString(fmt.Sprintf("startxref\n%d\n", xrefStart))
buf.WriteString("%%EOF\n")
return buf.Bytes()
}
// buildMinimalDOCX builds a real minimal Office Open XML document (a zip with the three parts a reader
// requires: the content types map, the package relationships, and the main document part) whose single
// paragraph carries the given text.
func buildMinimalDOCX(text string) ([]byte, error) {
var buf bytes.Buffer
zw := zip.NewWriter(&buf)
parts := []struct {
name, body string
}{
{
"[Content_Types].xml",
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>` +
`<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">` +
`<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>` +
`<Default Extension="xml" ContentType="application/xml"/>` +
`<Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>` +
`</Types>`,
},
{
"_rels/.rels",
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>` +
`<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">` +
`<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>` +
`</Relationships>`,
},
{
"word/document.xml",
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>` +
`<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">` +
`<w:body><w:p><w:r><w:t>` + text + ` Officer</w:t></w:r></w:p>` +
`<w:p><w:r><w:t>and a second paragraph</w:t></w:r></w:p></w:body>` +
`</w:document>`,
},
}
for _, p := range parts {
w, err := zw.Create(p.name)
if err != nil {
return nil, err
}
if _, err := w.Write([]byte(p.body)); err != nil {
return nil, err
}
}
if err := zw.Close(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func TestExtractText(t *testing.T) {
for _, tc := range []struct{ file, want string }{
{"testdata/sample.pdf", "SIGINT Analyst"},
{"testdata/sample.docx", "ISSO"},
{"testdata/sample.html", "COMSEC Custodian"},
{"testdata/sample.txt", "Cyber Engineer"},
} {
b, err := os.ReadFile(tc.file)
if err != nil {
t.Fatalf("read %s: %v", tc.file, err)
}
got, err := ExtractText(context.Background(), tc.file, b)
if err != nil {
t.Fatalf("ExtractText(%s): %v", tc.file, err)
}
if !strings.Contains(got, tc.want) {
t.Fatalf("ExtractText(%s) missing %q in %q", tc.file, tc.want, got)
}
}
}
// TestExtractTextDOCXJoinsRuns proves the DOCX path joins separate text runs with a space so adjacent
// paragraphs do not collide into one word.
func TestExtractTextDOCXJoinsRuns(t *testing.T) {
b, err := os.ReadFile("testdata/sample.docx")
if err != nil {
t.Fatalf("read docx: %v", err)
}
got, err := ExtractText(context.Background(), "sample.docx", b)
if err != nil {
t.Fatalf("ExtractText docx: %v", err)
}
if !strings.Contains(got, "ISSO Officer") {
t.Fatalf("docx runs not joined as expected: %q", got)
}
if !strings.Contains(got, "second paragraph") {
t.Fatalf("docx second paragraph missing: %q", got)
}
}
// TestExtractTextHTMLDropsScriptAndStyle proves the HTML path returns visible text only, never the
// content of script or style elements.
func TestExtractTextHTMLDropsScriptAndStyle(t *testing.T) {
html := []byte(`<html><head><style>body{color:red}</style></head><body><p>Visible Role</p><script>secret()</script></body></html>`)
got, err := ExtractText(context.Background(), "x.html", html)
if err != nil {
t.Fatalf("ExtractText html: %v", err)
}
if !strings.Contains(got, "Visible Role") {
t.Fatalf("visible text missing: %q", got)
}
if strings.Contains(got, "secret()") || strings.Contains(got, "color:red") {
t.Fatalf("script or style content leaked into output: %q", got)
}
}
// TestExtractTextUnknownIsRaw proves an unknown extension returns the raw bytes as a string.
func TestExtractTextUnknownIsRaw(t *testing.T) {
got, err := ExtractText(context.Background(), "notes.dat", []byte("raw payload"))
if err != nil {
t.Fatalf("ExtractText unknown: %v", err)
}
if got != "raw payload" {
t.Fatalf("unknown extension should return raw bytes, got %q", got)
}
}
// TestExtractTextCorruptPDF proves a corrupt PDF returns a wrapped parse error, not a panic.
func TestExtractTextCorruptPDF(t *testing.T) {
_, err := ExtractText(context.Background(), "broken.pdf", []byte("%PDF-1.4 this is not a real pdf body"))
if err == nil {
t.Fatal("expected an error on a corrupt pdf")
}
if !errors.Is(err, ErrParse) {
t.Fatalf("corrupt pdf error should wrap ErrParse, got %v", err)
}
}
// TestExtractTextCorruptDOCX proves a non zip DOCX returns a wrapped parse error.
func TestExtractTextCorruptDOCX(t *testing.T) {
_, err := ExtractText(context.Background(), "broken.docx", []byte("not a zip at all"))
if err == nil {
t.Fatal("expected an error on a corrupt docx")
}
if !errors.Is(err, ErrParse) {
t.Fatalf("corrupt docx error should wrap ErrParse, got %v", err)
}
}
// TestExtractTextCap proves the output is capped at maxTextBytes.
func TestExtractTextCap(t *testing.T) {
big := bytes.Repeat([]byte("A"), maxTextBytes+5000)
got, err := ExtractText(context.Background(), "big.txt", big)
if err != nil {
t.Fatalf("ExtractText big txt: %v", err)
}
if len(got) > maxTextBytes {
t.Fatalf("output not capped: got %d bytes, cap %d", len(got), maxTextBytes)
}
}