Files
coder/scripts/auditdocgen/main_test.go
T
Nick Vigilante fcbf45c571 docs: render audit log resources as one table per resource (#29797)
## Summary

The audit logs page now lists each tracked resource in its own section
with a simple two-column table, replacing the nested HTML tables that
rendered poorly and overflowed on phones.

## Before and after

Before, the generated block was a single Markdown table with a malformed
`|<b>Resource</b>||` header.
Each row held the resource name and its actions in one cell, then split
a raw HTML `<table>` of fields across the remaining cells.
The result rendered unevenly across renderers, was far wider than the
page on mobile, and was hard to navigate with a screen reader.

After, each resource gets an `###` heading under **Tracked Events**,
which also gives it a linkable anchor.
Below the heading, an `Actions:` line lists the audited actions as
inline code, followed by a two-column Field and Tracked table with `Yes`
or `No` values.
Every table uses the same fixed column widths (Field 75%, Tracked 25%),
so the Tracked column lines up across all resources.
Two narrow columns fit at phone width without horizontal scrolling.
Resources with no mapped actions skip the `Actions:` line instead of
rendering an empty one.
Resource ordering is unchanged.

## Changes

- `scripts/auditdocgen/main.go` writes one section per resource.
Each fields table is a small HTML table with `width` attributes, because
Markdown tables size columns to their content and cannot align across
tables.
GitHub and the docs site sanitizer both keep `width` but strip inline
`style` and `<colgroup>`.
- The generator no longer panics when its start marker is the last thing
in the file.
- New `scripts/auditdocgen/main_test.go` covers section output, marker
handling, and idempotent regeneration.
- `docs/admin/security/audit-logs.md` is regenerated, and the intro
sentence above the block now explains the actions line and the Tracked
column.

## How it was regenerated

`make docs/admin/security/audit-logs.md`, which builds the generator and
runs `markdownlint-cli2 --fix` and `markdown-table-formatter` over the
output.
Running it a second time produces no diff.

## Testing

- `go test ./scripts/auditdocgen/...` passes.
- `pnpm exec markdownlint-cli2 docs/admin/security/audit-logs.md`
reports 0 errors.

Linear: https://linear.app/codercom/issue/DOCS-894

---
🤖 Built with AI assistance.
2026-10-02 15:05:10 -04:00

69 lines
2.0 KiB
Go

package main
import (
"bytes"
"testing"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/codersdk"
)
func TestUpdateAuditDoc(t *testing.T) {
t.Parallel()
doc := []byte("# Title\n\n" + string(generatorPrefix) + "\n\nstale content\n\n" + string(generatorSuffix) + "\n\n## Next\n")
resources := AuditableResourcesMap{
"Template": {"name": true, "created_at": false},
}
got, err := updateAuditDoc(doc, resources)
require.NoError(t, err)
require.NotContains(t, string(got), "stale content")
require.Contains(t, string(got), "### Template\n")
require.True(t, bytes.HasPrefix(got, []byte("# Title\n\n"+string(generatorPrefix)+"\n\n")))
require.True(t, bytes.HasSuffix(got, []byte("\n\n"+string(generatorSuffix)+"\n\n## Next\n")))
again, err := updateAuditDoc(got, resources)
require.NoError(t, err)
require.Equal(t, string(got), string(again), "regenerating must be idempotent")
}
func TestUpdateAuditDocMissingMarkers(t *testing.T) {
t.Parallel()
_, err := updateAuditDoc([]byte("no markers"), AuditableResourcesMap{})
require.Error(t, err)
_, err = updateAuditDoc(append([]byte{}, generatorPrefix...), AuditableResourcesMap{})
require.Error(t, err)
}
func TestWriteResourceSections(t *testing.T) {
t.Parallel()
resources := AuditableResourcesMap{
"Zeta": {"b": true, "a": false},
"AuditableGroup": {"name": true},
}
actions := map[string][]codersdk.AuditAction{
"Group": {codersdk.AuditActionCreate, codersdk.AuditActionDelete},
}
var buf bytes.Buffer
writeResourceSections(&buf, resources, actions)
want := "### Group\n\n" +
"Actions: `create`, `delete`\n\n" +
fieldsTableHeader +
"<tr><td><code>name</code></td><td>Yes</td></tr>\n" +
"</tbody>\n</table>\n\n" +
"### Zeta\n\n" +
fieldsTableHeader +
"<tr><td><code>a</code></td><td>No</td></tr>\n" +
"<tr><td><code>b</code></td><td>Yes</td></tr>\n" +
"</tbody>\n</table>\n\n"
require.Contains(t, fieldsTableHeader, `<th width="75%">Field</th><th width="25%">Tracked</th>`)
require.Equal(t, want, buf.String())
}